Search in sources :

Example 1 with PoolingAsyncClientConnectionManager

use of org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager in project metrics by dropwizard.

the class InstrumentedHttpAsyncClientsTest method usesCustomClientConnectionManager.

@Test
public void usesCustomClientConnectionManager() throws Exception {
    try (PoolingAsyncClientConnectionManager clientConnectionManager = spy(new PoolingAsyncClientConnectionManager())) {
        client = InstrumentedHttpAsyncClients.custom(metricRegistry, metricNameStrategy, clientConnectionManager).disableAutomaticRetries().build();
        client.start();
        final SimpleHttpRequest request = SimpleHttpRequests.get("http://localhost:" + httpServer.getAddress().getPort() + "/");
        final String metricName = "some.made.up.metric.name";
        httpServer.createContext("/", exchange -> {
            exchange.sendResponseHeaders(200, 0L);
            exchange.setStreams(null, null);
            exchange.getResponseBody().write("TEST".getBytes(StandardCharsets.US_ASCII));
            exchange.close();
        });
        httpServer.start();
        when(metricNameStrategy.getNameFor(any(), any(HttpRequest.class))).thenReturn(metricName);
        final Future<SimpleHttpResponse> responseFuture = client.execute(request, new FutureCallback<SimpleHttpResponse>() {

            @Override
            public void completed(SimpleHttpResponse result) {
                assertThat(result.getCode()).isEqualTo(200);
            }

            @Override
            public void failed(Exception ex) {
                fail();
            }

            @Override
            public void cancelled() {
                fail();
            }
        });
        responseFuture.get(1L, TimeUnit.SECONDS);
        verify(clientConnectionManager, atLeastOnce()).connect(any(), any(), any(), any(), any(), any());
    }
}
Also used : SimpleHttpRequest(org.apache.hc.client5.http.async.methods.SimpleHttpRequest) HttpRequest(org.apache.hc.core5.http.HttpRequest) SimpleHttpRequest(org.apache.hc.client5.http.async.methods.SimpleHttpRequest) PoolingAsyncClientConnectionManager(org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager) SimpleHttpResponse(org.apache.hc.client5.http.async.methods.SimpleHttpResponse) IOException(java.io.IOException) ConnectionClosedException(org.apache.hc.core5.http.ConnectionClosedException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 2 with PoolingAsyncClientConnectionManager

use of org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager in project cxf by apache.

the class AsyncHTTPConduitFactory method setupNIOClient.

public synchronized void setupNIOClient(HTTPClientPolicy clientPolicy) {
    if (client != null) {
        return;
    }
    final IOReactorConfig config = IOReactorConfig.custom().setIoThreadCount(ioThreadCount).setSelectInterval(TimeValue.ofMilliseconds(selectInterval)).setSoLinger(TimeValue.ofMilliseconds(soLinger)).setSoTimeout(Timeout.ofMilliseconds(soTimeout)).setSoKeepAlive(soKeepalive).setTcpNoDelay(tcpNoDelay).build();
    final Registry<TlsStrategy> tlsStrategy = RegistryBuilder.<TlsStrategy>create().register("https", DefaultClientTlsStrategy.getSystemDefault()).build();
    connectionManager = new PoolingAsyncClientConnectionManager(tlsStrategy, PoolConcurrencyPolicy.STRICT, PoolReusePolicy.LIFO, TimeValue.ofMilliseconds(connectionTTL), DefaultSchemePortResolver.INSTANCE, SystemDefaultDnsResolver.INSTANCE);
    connectionManager.setDefaultMaxPerRoute(maxPerRoute);
    connectionManager.setMaxTotal(maxConnections);
    final RedirectStrategy redirectStrategy = new RedirectStrategy() {

        public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException {
            return false;
        }

        public URI getLocationURI(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException {
            return null;
        }
    };
    final HttpAsyncClientBuilder httpAsyncClientBuilder = HttpAsyncClients.custom().setConnectionManager(connectionManager).setRedirectStrategy(redirectStrategy).setDefaultCookieStore(new BasicCookieStore() {

        private static final long serialVersionUID = 1L;

        public void addCookie(Cookie cookie) {
        }
    });
    adaptClientBuilder(httpAsyncClientBuilder);
    client = httpAsyncClientBuilder.setIOReactorConfig(config).build();
    // Start the client thread
    client.start();
    // Always start the idle checker thread to validate pending requests and
    // use the ConnectionMaxIdle to close the idle connection
    new CloseIdleConnectionThread(connectionManager, client).start();
}
Also used : TlsStrategy(org.apache.hc.core5.http.nio.ssl.TlsStrategy) DefaultClientTlsStrategy(org.apache.hc.client5.http.ssl.DefaultClientTlsStrategy) HttpRequest(org.apache.hc.core5.http.HttpRequest) Cookie(org.apache.hc.client5.http.cookie.Cookie) HttpContext(org.apache.hc.core5.http.protocol.HttpContext) HttpResponse(org.apache.hc.core5.http.HttpResponse) HttpAsyncClientBuilder(org.apache.hc.client5.http.impl.async.HttpAsyncClientBuilder) IOReactorConfig(org.apache.hc.core5.reactor.IOReactorConfig) BasicCookieStore(org.apache.hc.client5.http.cookie.BasicCookieStore) PoolingAsyncClientConnectionManager(org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager) RedirectStrategy(org.apache.hc.client5.http.protocol.RedirectStrategy)

Aggregations

PoolingAsyncClientConnectionManager (org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager)2 HttpRequest (org.apache.hc.core5.http.HttpRequest)2 IOException (java.io.IOException)1 ExecutionException (java.util.concurrent.ExecutionException)1 SimpleHttpRequest (org.apache.hc.client5.http.async.methods.SimpleHttpRequest)1 SimpleHttpResponse (org.apache.hc.client5.http.async.methods.SimpleHttpResponse)1 BasicCookieStore (org.apache.hc.client5.http.cookie.BasicCookieStore)1 Cookie (org.apache.hc.client5.http.cookie.Cookie)1 HttpAsyncClientBuilder (org.apache.hc.client5.http.impl.async.HttpAsyncClientBuilder)1 RedirectStrategy (org.apache.hc.client5.http.protocol.RedirectStrategy)1 DefaultClientTlsStrategy (org.apache.hc.client5.http.ssl.DefaultClientTlsStrategy)1 ConnectionClosedException (org.apache.hc.core5.http.ConnectionClosedException)1 HttpResponse (org.apache.hc.core5.http.HttpResponse)1 TlsStrategy (org.apache.hc.core5.http.nio.ssl.TlsStrategy)1 HttpContext (org.apache.hc.core5.http.protocol.HttpContext)1 IOReactorConfig (org.apache.hc.core5.reactor.IOReactorConfig)1 Test (org.junit.Test)1