Camel Jetty组件写入Outputstream

时间:2016-03-31 15:19:05

标签: http apache-camel jetty httpconnection

我在Camel中有一个长时间运行的进程,它由HTTP-Request触发。我想将Status-Updates写入Outputstream,但我没有在客户端获得响应。

我尝试使用以下内容:

骆驼路线:

<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();

但是当我在处理器(注释行)中设置主体时,我只得到一个响应。有没有办法保持连接在驼峰并继续写入它?

1 个答案:

答案 0 :(得分:0)

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

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

HTTP组件使用HttpBindingHttpServletRequest转换为Exchange,相反,使用HttpServletResponse填充Exchange。您可以查看默认实现here或提供自己的实现。