use of org.apache.hc.client5.http.async.AsyncExecCallback in project mercury by yellow013.
the class AsyncClientMessageTrailers method main.
public static final void main(final String[] args) throws Exception {
final IOReactorConfig ioReactorConfig = IOReactorConfig.custom().setSoTimeout(Timeout.ofSeconds(5)).build();
final CloseableHttpAsyncClient client = HttpAsyncClients.custom().setIOReactorConfig(ioReactorConfig).addExecInterceptorAfter(ChainElement.PROTOCOL.name(), "custom", new AsyncExecChainHandler() {
@Override
public void execute(final HttpRequest request, final AsyncEntityProducer entityProducer, final AsyncExecChain.Scope scope, final AsyncExecChain chain, final AsyncExecCallback asyncExecCallback) throws HttpException, IOException {
// Send MD5 hash in a trailer by decorating the original entity producer
chain.proceed(request, entityProducer != null ? new DigestingEntityProducer("MD5", entityProducer) : null, scope, asyncExecCallback);
}
}).build();
client.start();
final SimpleHttpRequest request = SimpleRequestBuilder.post("http://httpbin.org/post").setBody("some stuff", ContentType.TEXT_PLAIN).build();
System.out.println("Executing request " + request);
final Future<SimpleHttpResponse> future = client.execute(SimpleRequestProducer.create(request), SimpleResponseConsumer.create(), new FutureCallback<SimpleHttpResponse>() {
@Override
public void completed(final SimpleHttpResponse response) {
System.out.println(request + "->" + new StatusLine(response));
System.out.println(response.getBody());
}
@Override
public void failed(final Exception ex) {
System.out.println(request + "->" + ex);
}
@Override
public void cancelled() {
System.out.println(request + " cancelled");
}
});
future.get();
System.out.println("Shutting down");
client.close(CloseMode.GRACEFUL);
}
use of org.apache.hc.client5.http.async.AsyncExecCallback in project mercury by yellow013.
the class AsyncClientInterceptors method main.
public static void main(final String[] args) throws Exception {
final IOReactorConfig ioReactorConfig = IOReactorConfig.custom().setSoTimeout(Timeout.ofSeconds(5)).build();
final CloseableHttpAsyncClient client = HttpAsyncClients.custom().setIOReactorConfig(ioReactorConfig).addRequestInterceptorFirst(new HttpRequestInterceptor() {
private final AtomicLong count = new AtomicLong(0);
@Override
public void process(final HttpRequest request, final EntityDetails entity, final HttpContext context) throws HttpException, IOException {
request.setHeader("request-id", Long.toString(count.incrementAndGet()));
}
}).addExecInterceptorAfter(ChainElement.PROTOCOL.name(), "custom", new AsyncExecChainHandler() {
@Override
public void execute(final HttpRequest request, final AsyncEntityProducer requestEntityProducer, final AsyncExecChain.Scope scope, final AsyncExecChain chain, final AsyncExecCallback asyncExecCallback) throws HttpException, IOException {
final Header idHeader = request.getFirstHeader("request-id");
if (idHeader != null && "13".equalsIgnoreCase(idHeader.getValue())) {
final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_NOT_FOUND, "Oppsie");
final ByteBuffer content = ByteBuffer.wrap("bad luck".getBytes(StandardCharsets.US_ASCII));
final AsyncDataConsumer asyncDataConsumer = asyncExecCallback.handleResponse(response, new BasicEntityDetails(content.remaining(), ContentType.TEXT_PLAIN));
asyncDataConsumer.consume(content);
asyncDataConsumer.streamEnd(null);
} else {
chain.proceed(request, requestEntityProducer, scope, asyncExecCallback);
}
}
}).build();
client.start();
final String requestUri = "http://httpbin.org/get";
for (int i = 0; i < 20; i++) {
final SimpleHttpRequest request = SimpleRequestBuilder.get(requestUri).build();
System.out.println("Executing request " + request);
final Future<SimpleHttpResponse> future = client.execute(request, new FutureCallback<SimpleHttpResponse>() {
@Override
public void completed(final SimpleHttpResponse response) {
System.out.println(request + "->" + new StatusLine(response));
System.out.println(response.getBody());
}
@Override
public void failed(final Exception ex) {
System.out.println(request + "->" + ex);
}
@Override
public void cancelled() {
System.out.println(request + " cancelled");
}
});
future.get();
}
System.out.println("Shutting down");
client.close(CloseMode.GRACEFUL);
}
Aggregations