简体   繁体   English

使用netty4异步调用链接HTTP请求-响应

[英]Link HTTP request-response using netty4 async call

I am writing a simple HTTP client using netty-4.0x. 我正在使用netty-4.0x编写一个简单的HTTP客户端。
Build the pipeline as below : 建立管道如下:

pipeline.addLast("codec", new HttpClientCodec());
pipeline.addLast("inflater", new HttpContentDecompressor());
pipeline.addLast("handler", new HttpResponseHandler());

where HttpResponseHandler provides implementation of messageReceived() , 其中HttpResponseHandler提供messageReceived()

Now there is a thread-pool which call the client and keep sending http message, I understand that ChannelFuture future = channel.write(request); 现在有一个线程池,它调用客户端并继续发送http消息,我知道ChannelFuture future = channel.write(request); is async call and will come out without blocking 是异步调用,将无阻塞地显示

The query which i am having is, is there a way to link request-response, without calling future.sync() call. 我正在查询,有一种方法可以链接请求-响应,而无需调用future.sync()调用。

Thanks for all the help in advance !!! 谢谢所有的帮助提前!

如果也对服务器进行编码,则可以让客户端向HTTP标头添加唯一的ID,并让服务器在响应中回显它。

If you are following strict HTTP pipelining rules then, on any given channel, the responses will be returned in the order the requests were sent in. It should be enough to maintain a request queue, removing the front of the queue for each response received. 如果您遵循严格的HTTP流水线规则,则在任何给定通道上,将按照发送请求的顺序返回响应。这足以维护请求队列,并为收到的每个响应除去队列的开头。

If you are creating a new channel, and a new pipeline for that channel, for each request, it's even easier. 如果您要为每个请求创建一个新通道以及该通道的新管道,则更加简单。 Either way you can add a handler to your pipeline which remembers the request (or queue of requests), and returns the request and response to your application when the response is received. 无论哪种方式,您都可以在管道中添加一个处理程序,以记住请求(或请求队列),并在收到响应时将请求和响应返回给应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM