gpt4 book ai didi

http - Camel Jetty 组件写入输出流

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:27:15 29 4
gpt4 key购买 nike

我在 Camel 中有一个长时间运行的进程,它是由 HTTP 请求触发的。我想将状态更新写入输出流,但我没有在客户端收到响应。

我尝试使用以下方法:

Camel 路线:

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

但是当我在处理器中设置主体时(注释行),我只会得到响应。有什么方法可以保持 Camel 的连接并继续写入它吗?

最佳答案

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

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

HTTP 组件使用 HttpBinding转换 HttpServletRequestExchange ,反之,填一个HttpServletResponse来自 Exchange .可以看到默认实现here或提供您自己的。

关于http - Camel Jetty 组件写入输出流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36337475/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com