使用Java AsyncHttpClient库执行异步连接? [英] Perform Async Connect with Java AsyncHttpClient Library?

查看:123
本文介绍了使用Java AsyncHttpClient库执行异步连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是将一些代码从.net移植到Java。我一直在使用AsyncHttpClient(https://github.com/sonatype/async-http-client)库和Netty提供程序。

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>() { ... });

NettyAsyncHttpProvider执行到网站的引导连接,即:

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

bootstrap.connect(new InetSocketAddress(
                                        AsyncHttpProviderUtils.getHost(uri), 
                                        AsyncHttpProviderUtils.getPort(uri)));

编辑:以澄清NettyAsyncHttpProvider类是AsyncHttpClient库的一部分。它与Netty本身无关。

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

这意味着像bbc网站(在我的低连接上),执行完成需要大约300ms(然后是异步接收/完成几乎立即发生)。这实际上会破坏我的调度程序的性能,该调度程序试图启动多个异步调用。当每个呼叫到达同一个域时,它运行良好。在我的情况下,我有数百个不同的域,因此每次调用都会产生初始延迟,同时建立连接会导致调度员的吞吐量下降。

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?

谢谢,
Paul

Thanks, Paul

推荐答案

回答我自己的问题......最后我尝试了原始的AsyncHttpClient库,Jetty和Apache HttpAsyncClient( http://hc.apache.org/httpcomponents-asyncclient-dev/index.html )试图找到一个图书馆这是100%异步。

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.

据我所知,这些库中的每一个都在异步执行实际的http请求之前执行阻塞连接。

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

为了继续前进,我已经完成了在自己的线程中执行每个http请求。我遵循了2.9。多线程请求执行一节中概述的模式( http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html )详细说明了如何使用这种方法。

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.

这篇关于使用Java AsyncHttpClient库执行异步连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆