use of org.apache.hc.core5.function.Supplier in project mercury by yellow013.
the class AsyncClientH2ServerPush method main.
public static void main(final String[] args) throws Exception {
final IOReactorConfig ioReactorConfig = IOReactorConfig.custom().setSoTimeout(Timeout.ofSeconds(5)).build();
final H2Config h2Config = H2Config.custom().setPushEnabled(true).build();
final CloseableHttpAsyncClient client = HttpAsyncClients.custom().setIOReactorConfig(ioReactorConfig).setVersionPolicy(HttpVersionPolicy.FORCE_HTTP_2).setH2Config(h2Config).build();
client.start();
client.register("*", new Supplier<AsyncPushConsumer>() {
@Override
public AsyncPushConsumer get() {
return new AbstractBinPushConsumer() {
@Override
protected void start(final HttpRequest promise, final HttpResponse response, final ContentType contentType) throws HttpException, IOException {
System.out.println(promise.getPath() + " (push)->" + new StatusLine(response));
}
@Override
protected int capacityIncrement() {
return Integer.MAX_VALUE;
}
@Override
protected void data(final ByteBuffer data, final boolean endOfStream) throws IOException {
}
@Override
protected void completed() {
}
@Override
public void failed(final Exception cause) {
System.out.println("(push)->" + cause);
}
@Override
public void releaseResources() {
}
};
}
});
final BasicHttpRequest request = BasicRequestBuilder.get("https://nghttp2.org/httpbin/").build();
System.out.println("Executing request " + request);
final Future<Void> future = client.execute(new BasicRequestProducer(request, null), new AbstractCharResponseConsumer<Void>() {
@Override
protected void start(final HttpResponse response, final ContentType contentType) throws HttpException, IOException {
System.out.println(request + "->" + new StatusLine(response));
}
@Override
protected int capacityIncrement() {
return Integer.MAX_VALUE;
}
@Override
protected void data(final CharBuffer data, final boolean endOfStream) throws IOException {
}
@Override
protected Void buildResult() throws IOException {
return null;
}
@Override
public void failed(final Exception cause) {
System.out.println(request + "->" + cause);
}
@Override
public void releaseResources() {
}
}, null);
future.get();
System.out.println("Shutting down");
client.close(CloseMode.GRACEFUL);
}
use of org.apache.hc.core5.function.Supplier in project httpcomponents-core by apache.
the class H2MultiplexingRequesterBootstrap method create.
public H2MultiplexingRequester create() {
final RequestHandlerRegistry<Supplier<AsyncPushConsumer>> registry = new RequestHandlerRegistry<>(uriPatternType);
for (final HandlerEntry<Supplier<AsyncPushConsumer>> entry : pushConsumerList) {
registry.register(entry.hostname, entry.uriPattern, entry.handler);
}
final ClientH2StreamMultiplexerFactory http2StreamHandlerFactory = new ClientH2StreamMultiplexerFactory(httpProcessor != null ? httpProcessor : H2Processors.client(), new DefaultAsyncPushConsumerFactory(registry), h2Config != null ? h2Config : H2Config.DEFAULT, charCodingConfig != null ? charCodingConfig : CharCodingConfig.DEFAULT, streamListener);
return new H2MultiplexingRequester(ioReactorConfig, (ioSession, attachment) -> new ClientH2PrefaceHandler(ioSession, http2StreamHandlerFactory, strictALPNHandshake), ioSessionDecorator, exceptionCallback, sessionListener, DefaultAddressResolver.INSTANCE, tlsStrategy != null ? tlsStrategy : new H2ClientTlsStrategy());
}
Aggregations