如何配置Apache的异步客户端允许未决请求的数量 [英] How to configure the number of allowed pending requests in Apache async client

查看:933
本文介绍了如何配置Apache的异步客户端允许未决请求的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个HTTP客户端应用程序,使用Apache httpasyncclient版本4.0.2。

I'm developing an HTTP client application, using Apache httpasyncclient version 4.0.2.

我想配置未决请求的最大数量。起初我假定这个数目是相同的最大连接数。我在下面的方式设置这对20:

I would like to configure the maximum number of pending requests. Initially I assumed this number is the same as the maximum number of connections. I set this to 20 in the following way:

    final CloseableHttpAsyncClient httpclient;

在构造函数中:

    final NHttpConnectionFactory<ManagedNHttpClientConnection> connFactory = new ManagedNHttpClientConnectionFactory(new DefaultHttpRequestWriterFactory(), new DefaultHttpResponseParserFactory(), HeapByteBufferAllocator.INSTANCE);
    final IOReactorConfig ioReactorConfig = IOReactorConfig.custom()
            .setIoThreadCount(4)
            .setConnectTimeout(30000)
            .setSoTimeout(30000)
            .build();
    final PoolingNHttpClientConnectionManager connManager = new PoolingNHttpClientConnectionManager(new DefaultConnectingIOReactor(ioReactorConfig), connFactory);
    final int maxConns = 20;
    connManager.setDefaultMaxPerRoute(maxConns);
    connManager.setMaxTotal(maxConns);
    httpclient = HttpAsyncClientBuilder.create().setConnectionManager(connManager).build();
    httpclient.start();

和更高版本:

    final BasicAsyncRequestProducer requestProducer = new BasicAsyncRequestProducer(URIUtils.extractHost(URI.create(serverAddress)), request) {
        @Override
        public void requestCompleted(HttpContext context) {
            pendings.add(callback);
            logMessage(Direction.REQUEST, req);
            handler.onContentWriteCompleted();
        }
    };
    httpclient.execute(requestProducer, HttpAsyncMethods.createConsumer(), new HttpClientContext(), callback);

在哪里回调是我处理响应。

where callback is where I handle the response.

作为一个概念证明失败。事实上,获得做四线程运行,但是当我尝试发送20条信息同时,只有8立即发送,其余必须等到服务器响应他们。

As a proof of concept it fails. Indeed get do four threads running, but when I try to send 20 messages simultaneously, only 8 are sent immediately, the rest must wait until the server responds to them.

阿帕奇调试消息表明20个连接确实已建立。看来,更多的配置必须做到的。

Apache debug messages are indicating that 20 connections have indeed been created. It seems that more configuration must be done.

推荐答案

阿帕奇HttpAsyncClient保持着无限的请求,执行队列,并且不试图限制挂起的请求数。各种应用程序可能会或可能不希望扼杀请求率并没有简单的方法来满足他们的所有。

Apache HttpAsyncClient maintains an unbounded request execution queue and does not attempt to limit the number of pending requests. Various applications may or may not want to throttle request rate and there is no easy way to satisfy them all.

一个可以然而很容易节流使用简单的信号并发请求的数目。

One can however fairly easily throttle the number of concurrent requests using a simple semaphore.

final Semaphore semaphore = new Semaphore(maxConcurrencyLevel);
for (int i = 0; i < n; i++) {

    semaphore.acquire();
    this.httpclient.execute(
            new BasicAsyncRequestProducer(target, request),
            new MyResponseConsumer(),
            new FutureCallback<HttpResponse>() {

                @Override
                public void completed(final HttpResponse result) {
                    semaphore.release();
                }

                @Override
                public void failed(final Exception ex) {
                    semaphore.release();
                }

                @Override
                public void cancelled() {
                    semaphore.release();
                }

            });
}

这篇关于如何配置Apache的异步客户端允许未决请求的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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