下面列出了怎么用org.apache.http.nio.protocol.HttpAsyncResponseConsumer的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
public void executeAsyncDelegatesToConfiguredAsyncClient() {
// given
HttpClient client = Mockito.spy(createDefaultTestObject());
CloseableHttpAsyncClient asyncClient = mockAsyncClient(client);
BatchRequest request = createDefaultTestBatchRequest();
// when
client.executeAsync(request, createMockTestResultHandler());
// then
verify(client).getAsyncClient();
verify(asyncClient).execute(
any(HttpAsyncRequestProducer.class),
any(HttpAsyncResponseConsumer.class),
any(HttpContext.class),
any(FutureCallback.class));
}
@Override public <T> Future<T> execute(HttpAsyncRequestProducer requestProducer,
HttpAsyncResponseConsumer<T> responseConsumer, HttpContext httpContext,
FutureCallback<T> callback) {
TraceContext invocationContext = currentTraceContext.get();
if (invocationContext != null) {
httpContext.setAttribute(TraceContext.class.getName(), invocationContext);
}
return delegate.execute(
new TracingAsyncRequestProducer(requestProducer, httpContext),
new TracingAsyncResponseConsumer<>(responseConsumer, httpContext),
httpContext,
callback != null && invocationContext != null
? new TraceContextFutureCallback<>(callback, currentTraceContext, invocationContext)
: callback
);
}
private StreamingReadAsyncClient(final URI uri, final KinesisVideoSigner signer, final String inputInJson, final Integer connectionTimeoutInMillis, final Integer readTimeoutInMillis, final HttpAsyncResponseConsumer<HttpResponse> httpAsyncResponseConsumer, final FutureCallback<HttpResponse> futureCallback) {
this.uri = uri;
this.signer = signer;
this.inputInJson = inputInJson;
this.connectionTimeoutInMillis = connectionTimeoutInMillis;
this.readTimeoutInMillis = readTimeoutInMillis;
this.httpAsyncResponseConsumer = httpAsyncResponseConsumer;
this.futureCallback = futureCallback;
this.asyncClient = getHttpClient();
}
@SuppressWarnings("rawtypes")
public List<HttpAsyncResponseConsumer> makeConsumers(List<HttpAsyncResponseConsumer> list) {
List<HttpAsyncResponseConsumer> ls = new ArrayList<HttpAsyncResponseConsumer>();
for (HttpAsyncResponseConsumer r : list) {
ls.add(makeConsumer(r));
}
return ls;
}
@Test
public void producesPoolingAsyncResponseConsumer() {
// given
PoolingAsyncResponseConsumerFactory factory = createAsyncResponseConsumerFactory();
// when
HttpAsyncResponseConsumer result = factory.create();
// then
assertTrue(result instanceof PoolingAsyncResponseConsumer);
}
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
MethodInterceptResult result) throws Throwable {
HttpAsyncResponseConsumer consumer = (HttpAsyncResponseConsumer) allArguments[1];
HttpContext context = (HttpContext) allArguments[2];
FutureCallback callback = (FutureCallback) allArguments[3];
allArguments[1] = new HttpAsyncResponseConsumerWrapper(consumer);
allArguments[3] = new FutureCallbackWrapper(callback);
CONTEXT_LOCAL.set(context);
}
public Builder withHttpAsyncResponseConsumer(final HttpAsyncResponseConsumer<HttpResponse>
httpAsyncResponseConsumer) {
mHttpAsyncResponseConsumer = httpAsyncResponseConsumer;
return this;
}
@Override
public HttpAsyncResponseConsumer<HttpResponse> create() {
return new PoolingAsyncResponseConsumer(pool);
}
public LogbookHttpAsyncResponseConsumer(final HttpAsyncResponseConsumer<T> consumer) {
this.consumer = consumer;
}
@Override
protected HttpAsyncResponseConsumer<T> delegate() {
return consumer;
}
@Override
protected HttpAsyncResponseConsumer delegate() {
return delegate;
}
@Test
void shouldWrapIOException() throws IOException {
final HttpAsyncResponseConsumer<HttpResponse> unit = new LogbookHttpAsyncResponseConsumer<>(createConsumer());
final BasicHttpContext context = new BasicHttpContext();
context.setAttribute(Attributes.STAGE, stage);
final ResponseWritingStage last = mock(ResponseWritingStage.class);
when(stage.process(any())).thenReturn(last);
doThrow(new IOException()).when(last).write();
assertThrows(UncheckedIOException.class, () ->
unit.responseCompleted(context));
}
public HttpAsyncResponseConsumerWrapper(HttpAsyncResponseConsumer<T> consumer) {
this.consumer = consumer;
}
private Thread baseTest() throws Throwable {
Object[] allArguments = new Object[] {
producer,
consumer,
httpContext,
callback
};
Class[] types = new Class[] {
HttpAsyncRequestProducer.class,
HttpAsyncResponseConsumer.class,
HttpContext.class,
FutureCallback.class
};
httpAsyncClientInterceptor.beforeMethod(enhancedInstance, null, allArguments, types, null);
Assert.assertEquals(CONTEXT_LOCAL.get(), httpContext);
Assert.assertTrue(allArguments[1] instanceof HttpAsyncResponseConsumerWrapper);
Assert.assertTrue(allArguments[3] instanceof FutureCallbackWrapper);
sessionRequestConstructorInterceptor.onConstruct(enhancedInstance, null);
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
//start local
completeInterceptor.beforeMethod(enhancedInstance, null, null, null, null);
//start request
requestExecutorInterceptor.beforeMethod(enhancedInstance, null, null, null, null);
HttpAsyncResponseConsumerWrapper consumerWrapper = new HttpAsyncResponseConsumerWrapper(consumer);
consumerWrapper.responseReceived(response);
new FutureCallbackWrapper(callback).completed(null);
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
});
thread.start();
return thread;
}
TracingAsyncResponseConsumer(HttpAsyncResponseConsumer<T> responseConsumer,
HttpContext context) {
this.responseConsumer = responseConsumer;
this.context = context;
}
/**
* Retries given HTTP request. Called internally only, from the HttpFuture
*
* @param httpUriRequest the HttpUriRequest to retry
* @param responseConsumer the response consumer
* @param numRetry The retry count
* @return the resulting Future<HttpResponse> instance
*/
Future<HttpResponse> retryOperation( HttpUriRequest httpUriRequest, HttpAsyncResponseConsumer<HttpResponse> responseConsumer, int numRetry ) {
httpUriRequest.setHeader( "X-Tio-Retry-Count", Integer.toString( numRetry ) );
return responseConsumer == null ? asyncClient.execute( httpUriRequest, null ) : asyncClient.execute( HttpAsyncMethods.create( httpUriRequest ), responseConsumer, null, null );
}
/**
* Instantiates a new Http future.
*
* @param asyncHttpService async http service instance
* @param httpUriRequest the http uri request
* @param responseConsumer the response consumer
* @param httpResponseFuture the http response future
* @param body the body
*/
public HttpFuture( AsyncHttpService asyncHttpService, HttpUriRequest httpUriRequest, HttpAsyncResponseConsumer<HttpResponse> responseConsumer, Future<HttpResponse> httpResponseFuture, String body ) {
this( asyncHttpService, httpUriRequest, httpResponseFuture, body );
this.responseConsumer = responseConsumer;
}
HttpAsyncResponseConsumer<HttpResponse> create();
protected abstract HttpAsyncResponseConsumer<T> delegate();