发送http 请求(request)后,需要等待一段时间,比如30秒后才能得到响应(response)。采用java,如何高效的实现在一定时间内,发送http请求后,得到尽可能多的响应。
我尝试过线程池,但是我有大约十多种不同类型的http请求,线程池数量一多比较消耗资源。我也试过Apache的HttpAsyncioRequest,但是其内部实现就是线程池,在我的这个问题上实现效率也不高。有没有比较好的库或者方法,或者类似python coroutine那样,可以高效的实现并发请求。
高洛峰2017-04-17 17:21:27
Reference NIO selector
Let’s consider it a bit original:
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.
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.
PHP中文网2017-04-17 17:21:27
You can release the connection and resources after sending the request, and let the other party call back your interface after processing