0

出于某种原因HttpAsyncClient,如果setConnectionManagerShared设置为true. 我发现了这个错误,但无法弄清楚我错过了什么。

这是我创建新客户的方法

def apply(proxy: Option[HttpHost], cookieStore: Option[CookieStore]) = {

val builder = HttpAsyncClients.custom.
  setConnectionManager(connManager).
  setConnectionManagerShared(true).
  setDefaultCredentialsProvider(credentialsProvider).
  setDefaultRequestConfig(defaultRequestConfig).
  setSSLStrategy(sslStrategy)

proxy.map(builder.setProxy)
builder.setDefaultCookieStore(cookieStore.getOrElse(new BasicCookieStore)) // Use custom cookie store if necessary.

// Create an HttpClient with the given custom dependencies and configuration.
val client: HttpAsyncClient = new HttpAsyncClient(builder.build)
client
}

全班设在这里

我应该改变什么?

4

1 回答 1

1
DefaultConnectingIOReactor ioReactor = new DefaultConnectingIOReactor();
PoolingNHttpClientConnectionManager cm = new PoolingNHttpClientConnectionManager(ioReactor);

CloseableHttpAsyncClient client1 = HttpAsyncClients.custom()
        .setConnectionManager(cm)
        .build();
CloseableHttpAsyncClient client2 = HttpAsyncClients.custom()
        .setConnectionManager(cm)
        .setConnectionManagerShared(true)
        .build();

client1.start();
client2.start();

final CountDownLatch latch = new CountDownLatch(2);
FutureCallback callback = new FutureCallback<HttpResponse>() {
    @Override
    public void completed(HttpResponse result) {
        latch.countDown();
        System.out.println(result.getStatusLine());
    }

    @Override
    public void failed(Exception ex) {
        latch.countDown();
        System.out.println(ex.getMessage());
    }

    @Override
    public void cancelled() {
        latch.countDown();
    }
};

client1.execute(new HttpGet("http://httpbin.org/get"), callback);
client2.execute(new HttpGet("http://httpbin.org/get"), callback);
latch.await();

// I am aware this is sloppy
client1.close();
client2.close();
于 2015-09-08T08:34:08.353 回答