apache-httpclient-4.xapache-commons-httpclientasynchttpclientapache-httpasyncclient

HttpAsyncClient isn't making request if setConnectionManagerShared is set to true


For some reason HttpAsyncClient isn't making request if setConnectionManagerShared is set to true. I found this bug but couldn't figure out what I'm missing.

Here is how I create new client

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
}

Full class is located is here.

What should I change ?


Solution

  • 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();