下面列出了怎么用org.apache.http.nio.protocol.BasicAsyncResponseConsumer的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
public void disablingPooledResponseBuffersInitializesClientWithBasicAsyncResponseConsumerFactory() {
// given
HttpClientFactory.Builder builder = new HttpClientFactory.Builder();
builder.withServerList(TEST_SERVER_LIST)
.withMaxTotalConnections(TEST_MAX_TOTAL_CONNECTIONS)
.withPooledResponseBuffers(false);
HttpClientFactory factory = spy(builder.build());
// when
factory.createInstance();
// then
ArgumentCaptor<HttpAsyncResponseConsumerFactory> captor = ArgumentCaptor.forClass(HttpAsyncResponseConsumerFactory.class);
verify(factory).createConfiguredClient(any(), any(), captor.capture());
assertEquals(BasicAsyncResponseConsumer.class, captor.getValue().create().getClass());
}
@Test
public void executeAsyncResponseIsNotPooledIfPoolNotConfigured() {
// given
HCHttp.Builder testObjectFactoryBuilder =
HCHttpTest.createDefaultHttpObjectFactoryBuilder();
testObjectFactoryBuilder.withPooledResponseBuffers(false);
HttpClient client = spy(testObjectFactoryBuilder.build().createClient());
CloseableHttpAsyncClient asyncClient = mockAsyncClient(client);
BatchRequest request = createDefaultTestBatchRequest();
// when
client.executeAsync(request, createMockTestResultHandler());
// then
verify(client).getAsyncClient();
verify(asyncClient).execute(
any(HttpAsyncRequestProducer.class),
asyncConsumerCaptor.capture(),
any(HttpContext.class),
any(FutureCallback.class));
assertEquals(BasicAsyncResponseConsumer.class, asyncConsumerCaptor.getValue().getClass());
}
/**
* Invokes the given method with the given arguments and invokes the
* {@code JsonRpcCallback} with the result cast to the given
* {@code returnType}, or null if void. The {@code extraHeaders} are added
* to the request.
*
* @param methodName the name of the method to invoke
* @param argument the arguments to the method
* @param extraHeaders extra headers to add to the request
* @param returnType the return type
* @param callback the {@code JsonRpcCallback}
*/
@SuppressWarnings("unchecked")
private <T> Future<T> doInvoke(String methodName, Object argument, Class<T> returnType, Map<String, String> extraHeaders, JsonRpcCallback<T> callback) {
String path = serviceUrl.getPath() + (serviceUrl.getQuery() != null ? "?" + serviceUrl.getQuery() : "");
int port = serviceUrl.getPort() != -1 ? serviceUrl.getPort() : serviceUrl.getDefaultPort();
HttpRequest request = new BasicHttpEntityEnclosingRequest("POST", path);
addHeaders(request, headers);
addHeaders(request, extraHeaders);
try {
writeRequest(methodName, argument, request);
} catch (IOException e) {
callback.onError(e);
}
HttpHost target = new HttpHost(serviceUrl.getHost(), port, serviceUrl.getProtocol());
BasicAsyncRequestProducer asyncRequestProducer = new BasicAsyncRequestProducer(target, request);
BasicAsyncResponseConsumer asyncResponseConsumer = new BasicAsyncResponseConsumer();
RequestAsyncFuture<T> futureCallback = new RequestAsyncFuture<>(returnType, callback);
BasicHttpContext httpContext = new BasicHttpContext();
requester.execute(asyncRequestProducer, asyncResponseConsumer, pool, httpContext, futureCallback);
return (callback instanceof JsonRpcFuture ? (Future<T>) callback : null);
}