网络 - Java 异步http请求。
怪我咯
怪我咯 2017-04-17 17:19:27
0
2
663

发送http 请求(request)后,需要等待一段时间,比如30秒后才能得到响应(response)。采用java,如何高效的实现在一定时间内,发送http请求后,得到尽可能多的响应。
我尝试过线程池,但是我有大约十多种不同类型的http请求,线程池数量一多比较消耗资源。我也试过Apache的HttpAsyncioRequest,但是其内部实现就是线程池,在我的这个问题上实现效率也不高。有没有比较好的库或者方法,或者类似python coroutine那样,可以高效的实现并发请求。

怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(2)
小葫芦

Reference NIO selector

Let’s consider it a bit original:

  1. No matter how concurrent or asynchronous you are, a TCP connection must be established before an http request is sent out. After it is sent out, the client must have a thread in a listening state waiting for the server's return.

  2. The client sends http requests non-blockingly. After each thread request is sent out, a new thread is created to listen for http returns, and the original thread does other work.

This solution will consume double N threads and N TCP connections.

Some considerations for optimization: instead of consuming a thread for monitoring each connection, one thread can monitor the corresponding -> selector of all connections.

洪涛

You can release the connection and resources after sending the request, and let the other party call back your interface after processing

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!