Search in sources :

Example 6 with Request

use of org.apache.hc.client5.http.fluent.Request in project metrics by dropwizard.

the class InstrumentedHttpClientsTest method registersExpectedExceptionMetrics.

@Test
public void registersExpectedExceptionMetrics() throws Exception {
    HttpServer httpServer = HttpServer.create(new InetSocketAddress(0), 0);
    final HttpGet get = new HttpGet("http://localhost:" + httpServer.getAddress().getPort() + "/");
    final String requestMetricName = "request";
    final String exceptionMetricName = "exception";
    httpServer.createContext("/", HttpExchange::close);
    httpServer.start();
    when(metricNameStrategy.getNameFor(any(), any(HttpRequest.class))).thenReturn(requestMetricName);
    when(metricNameStrategy.getNameFor(any(), any(Exception.class))).thenReturn(exceptionMetricName);
    try {
        client.execute(get);
        fail();
    } catch (NoHttpResponseException expected) {
        assertThat(metricRegistry.getMeters()).containsKey("exception");
    } finally {
        httpServer.stop(0);
    }
}
Also used : HttpRequest(org.apache.hc.core5.http.HttpRequest) NoHttpResponseException(org.apache.hc.core5.http.NoHttpResponseException) InetSocketAddress(java.net.InetSocketAddress) HttpGet(org.apache.hc.client5.http.classic.methods.HttpGet) HttpServer(com.sun.net.httpserver.HttpServer) HttpExchange(com.sun.net.httpserver.HttpExchange) NoHttpResponseException(org.apache.hc.core5.http.NoHttpResponseException) Test(org.junit.Test)

Example 7 with Request

use of org.apache.hc.client5.http.fluent.Request in project metrics by dropwizard.

the class HttpClientMetricNameStrategiesTest method hostAndMethodWithoutNameInWrappedRequest.

@Test
public void hostAndMethodWithoutNameInWrappedRequest() throws URISyntaxException {
    HttpRequest request = rewriteRequestURI(new HttpPost("http://my.host.com/whatever"));
    assertThat(HOST_AND_METHOD.getNameFor(null, request), is("org.apache.hc.client5.http.classic.HttpClient.my.host.com.post-requests"));
}
Also used : HttpRequest(org.apache.hc.core5.http.HttpRequest) HttpPost(org.apache.hc.client5.http.classic.methods.HttpPost) Test(org.junit.Test)

Example 8 with Request

use of org.apache.hc.client5.http.fluent.Request in project metrics by dropwizard.

the class HttpClientMetricNameStrategiesTest method querylessUrlAndMethodWithNameInWrappedRequest.

@Test
public void querylessUrlAndMethodWithNameInWrappedRequest() throws URISyntaxException {
    HttpRequest request = rewriteRequestURI(new HttpPut("https://thing.com:8090/my/path?ignore=this&and=this"));
    assertThat(QUERYLESS_URL_AND_METHOD.getNameFor("some-service", request), is("org.apache.hc.client5.http.classic.HttpClient.some-service.https://thing.com:8090/my/path.put-requests"));
}
Also used : HttpRequest(org.apache.hc.core5.http.HttpRequest) HttpPut(org.apache.hc.client5.http.classic.methods.HttpPut) Test(org.junit.Test)

Example 9 with Request

use of org.apache.hc.client5.http.fluent.Request in project metrics by dropwizard.

the class HttpClientMetricNameStrategiesTest method hostAndMethodWithNameInWrappedRequest.

@Test
public void hostAndMethodWithNameInWrappedRequest() throws URISyntaxException {
    HttpRequest request = rewriteRequestURI(new HttpPost("http://my.host.com/whatever"));
    assertThat(HOST_AND_METHOD.getNameFor("some-service", request), is("org.apache.hc.client5.http.classic.HttpClient.some-service.my.host.com.post-requests"));
}
Also used : HttpRequest(org.apache.hc.core5.http.HttpRequest) HttpPost(org.apache.hc.client5.http.classic.methods.HttpPost) Test(org.junit.Test)

Example 10 with Request

use of org.apache.hc.client5.http.fluent.Request 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

IOException (java.io.IOException)8 HttpRequest (org.apache.hc.core5.http.HttpRequest)7 Test (org.junit.Test)7 SimpleHttpRequest (org.apache.hc.client5.http.async.methods.SimpleHttpRequest)5 HttpClientContext (org.apache.hc.client5.http.protocol.HttpClientContext)5 SimpleHttpResponse (org.apache.hc.client5.http.async.methods.SimpleHttpResponse)4 ExecutionException (java.util.concurrent.ExecutionException)3 HttpPost (org.apache.hc.client5.http.classic.methods.HttpPost)3 RequestConfig (org.apache.hc.client5.http.config.RequestConfig)3 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 HttpExchange (com.sun.net.httpserver.HttpExchange)2 ClientProtocolException (org.apache.hc.client5.http.ClientProtocolException)2 Configurable (org.apache.hc.client5.http.config.Configurable)2 BasicCookieStore (org.apache.hc.client5.http.cookie.BasicCookieStore)2 PoolingAsyncClientConnectionManager (org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager)2 ConnectionClosedException (org.apache.hc.core5.http.ConnectionClosedException)2 HttpContext (org.apache.hc.core5.http.protocol.HttpContext)2 Timer (com.codahale.metrics.Timer)1 Gson (com.google.gson.Gson)1