如何从netty中的HTTP服务器请求处理程序正确调用HTTP客户端? [英] How to call properly HTTP client from HTTP server request handler in netty?
问题描述
我正在使用netty 3.3.1开发自定义HTTP服务器.
I am developing custom HTTP server with netty 3.3.1.
我需要实现类似这样的东西
I need to implement something like this
- HTTP Server收到请求
- HTTP Server对其进行解析并作为其他计算机的客户端调用HTTP请求
- HTTP Server等待(2)中发送的请求的响应
- HTTP服务器根据(3)中收到的内容发送对(1)的请求的响应
这意味着客户端请求(2)必须表现为同步.
It means that client request (2) has to behave as synchronous.
我写的内容基于 HttpSnoopClient示例,但是它不起作用,因为我收到了
What I wrote is based on HttpSnoopClient example but it does not work, because I receive
java.lang.IllegalStateException:
await*() in I/O thread causes a dead lock or sudden performance drop. Use addListener() instead or call await*() from a different thread.
我重构了代码从上面提到的示例开始,现在看起来更像这样了(从HttpSnoopClient的第7f行开始):
I've refactored the code from the example mentioned above and now it looks more less like this (starting from line 7f of HttpSnoopClient):
ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));
future.addListener(new ChannelFutureListener() {
public void operationComplete(ChannelFuture future) {
if (!future.isSuccess()) {
System.err.println("Cannot connect");
future.getCause().printStackTrace();
bootstrap.releaseExternalResources();
return;
}
System.err.println("Connected");
Channel channel = future.getChannel();
// Send the HTTP request.
channel.write(request);
channel.close();
// Wait for the server to close the connection.
channel.getCloseFuture().addListener(new ChannelFutureListener() {
public void operationComplete(ChannelFuture future) {
System.err.println("Disconnected");
bootstrap.releaseExternalResources(); // DOES NOT WORK?
}
});
}
});
}
}
上面的示例中的run()
命令在我的herver处理程序的messageReceived
函数中调用.
The run()
command from the above example is invoked in the messageReceived
function of my herver handler.
因此它变得异步并避免了await *函数.请求被正确调用.但是-由于我的未知原因-线
So it became asynchronous and avoid await* functions. Request is invoked properly. But - for uknown reason for me - the line
bootstrap.releaseExternalResources(); // DOES NOT WORK?
不起作用.抛出一个异常,说我无法终止当前正在使用的线程(这听起来很合理,但仍然无法给出以其他方式执行此操作的答案).
does not work. It throws an exception saying that I cannot kill the thread I am currently using (which sounds reasonable, but still does not give me an answer how to do that in a different way).
我也不确定这是正确的方法吗?
I am also not sure is this a correct approach?
也许您可以推荐有关netty中此类事件编程技术的教程?通常如何处理一些必须按指定顺序调用并彼此等待的异步请求?
Maybe you can recommend a tutorial of such event programming techniques in netty? How to deal - in general - with a few asynchronous requests that has to be invoked in specified order and wait for each other?
谢谢
推荐答案
如果您确实想在关闭时释放引导程序,则可以这样做:
If you really want to release the bootstrap on close you can do it like this:
channel.getCloseFuture().addListener(new ChannelFutureListener() {
public void operationComplete(ChannelFuture future) {
System.err.println("Disconnected");
new Thread(new Runnable() {
public void run() {
bootstrap.releaseExternalResources();
}
}).start();
}
});
这篇关于如何从netty中的HTTP服务器请求处理程序正确调用HTTP客户端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!