2016-03-31 45 views
1

我在Camel中有一个很长的运行进程,它由HTTP-Request触发。我想写状态更新到输出流,但是我没有在客户端得到响应。Camel Jetty组件写入输出流

我尝试使用以下内容:

骆驼路线:

<from uri="jetty:http://localhost:12345/myservice"/> 
<process ref="test" /> 

的处理器测试:

public void process(Exchange arg0) throws Exception { 
    System.out.println("TestProcessor"); 
    HttpServletResponse response = (HttpServletResponse) arg0.getIn().getHeader(Exchange.HTTP_SERVLET_RESPONSE); 

    OutputStreamWriter wr = new OutputStreamWriter(response.getOutputStream()); 
    BufferedWriter w = new BufferedWriter(wr); 
    for(int x = 0; x < 10; x++){ 
     w.write("Zeile: " + x + "\n"); 
     w.newLine(); 
    } 
//  arg0.getIn().setBody("This might also be a response"); 
} 

,并且调用代码:

final HttpURLConnection conn = (HttpURLConnection) url.openConnection();   
      conn.setDoOutput(true); 
      conn.setInstanceFollowRedirects(false); 
      conn.setRequestMethod("GET"); 
      conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
      conn.setRequestProperty("charset", "utf-8"); 
      conn.setRequestProperty("Content-Length", Integer.toString(postDataLength)); 
      conn.setUseCaches(false); 
      new Thread(new Runnable(){ 

       @Override 
       public void run() { 
        try { 
         if(!urlParameters.isEmpty()){ 
          try(DataOutputStream wr = new DataOutputStream(conn.getOutputStream())) { 
           wr.write(postData); 
           wr.close(); 
          } 
         } 
         InputStream s = conn.getInputStream(); 
         System.out.println("got InputStream"); 
         InputStreamReader is = new InputStreamReader(s); 
         BufferedReader br = new BufferedReader(is); 
         String line; 
         while((line = br.readLine()) != null){ 
          System.out.println("ReadLine: " + line); 
         } 

         conn.disconnect(); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 
       } 

      }).start(); 

但我只有当我得到回应在处理器中设置主体(注释行)。有没有办法保持骆驼连接并继续写入?

回答

0

您应该使用out消息与HTTP组件发送答案:

arg0.getOut().setBody("This might also be a response"); 

的HTTP组件使用HttpBindingHttpServletRequest转换为Exchange,而相反,从Exchange填补HttpServletResponse 。你可以看到默认实现here或提供你自己的。