开发者

Perform Async Connect with Java AsyncHttpClient Library?

开发者 https://www.devze.com 2023-03-27 12:19 出处:网络
I\'m just porting some code over from .net to Java. I\'ve been using the AsyncHttpClient (https://github.com/sonatype/async-http-client) library with the Netty provider.

I'm just porting some code over from .net to Java. I've been using the AsyncHttpClient (https://github.com/sonatype/async-http-client) library with the Netty provider.

Doing a little bit of testing I was surprised to see that during the execute call e.g:

httpClient.prepareGet("http://bbc.co.uk").execute(
                  new AsyncCompletionHandler<Response>() { ... });

The NettyAsyncHttpProvider performs a bootstrap connect to the site i.e.:

bootstrap.connect(new InetSocketAddress(
                                        AsyncHttpProviderU开发者_Go百科tils.getHost(uri), 
                                        AsyncHttpProviderUtils.getPort(uri)));

EDIT: to clarify the NettyAsyncHttpProvider class is part of the AsyncHttpClient Library. It has nothing to do with Netty itself.

Which means on something like the bbc website (on my lowly connection) it takes about 300ms for the execute to complete (then the async receive/complete happens almost immediately). This really kills the performance of my dispatcher which is trying to start multiple async calls. When each call is to the same domain it works well. In my situation I have hundreds of different domains so each call incurs the initial delay while the connection is established which kills the throughput of the dispatcher.

Can anyone offer any advice on how to use this library so it truly works in an async fashion or an alternative library?

Thanks, Paul


To answer my own question...in the end I tried the original AsyncHttpClient library, Jetty and the Apache HttpAsyncClient (http://hc.apache.org/httpcomponents-asyncclient-dev/index.html) to try and find a library that is 100% asynchronous.

As far as I can tell each of these libraries performs a blocking connect before performing the actual http request asynchronously.

In order to move forward I've gone with executing each http request in its own thread. I've followed the pattern outlined in section "2.9. Multithreaded request execution" (http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html) that details how to use this approach.

This approach is now working well and allows the my "dispatcher" to rapidly create numerous async calls without having any blocking in the main thread.


Yes, have you looked at the java non-blocking api? (a.k.a NIO)? There is a stack overflow thread about it which is useful. Also this article by IBM developerWorks should help you figure out if it's what you need.

0

精彩评论

暂无评论...
验证码 换一张
取 消