如果 setConnectionManagerShared 设置为 true,则 HttpAsyncClient 不发出请求
HttpAsyncClient isn't making request if setConnectionManagerShared is set to true
出于某种原因,如果 setConnectionManagerShared
设置为 true
,HttpAsyncClient
不会发出请求。我找到了这个 bug 但无法弄清楚我遗漏了什么。
这是我创建新客户的方法
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
}
完整 class 位于 here.
我应该改变什么?
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();
出于某种原因,如果 setConnectionManagerShared
设置为 true
,HttpAsyncClient
不会发出请求。我找到了这个 bug 但无法弄清楚我遗漏了什么。
这是我创建新客户的方法
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
}
完整 class 位于 here.
我应该改变什么?
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();