Search in sources :

Example 6 with CloseableHttpAsyncClient

use of org.apache.http.impl.nio.client.CloseableHttpAsyncClient in project uavstack by uavorg.

the class HttpService method httpclientAsynctestException.

/**
 * 异步测试异常用例
 *
 * @return
 */
@GET
@Path("httpclientAsynctest_exception")
public String httpclientAsynctestException() {
    final CloseableHttpAsyncClient client = HttpAsyncClients.custom().build();
    HttpUriRequest httpMethod = new HttpGet("ht://10.10.37.41:8080/com.creditease.uav.monitorframework.buildFat/rs/redis/aredistest");
    client.start();
    client.execute(httpMethod, new FutureCallback<HttpResponse>() {

        @Override
        public void completed(HttpResponse result) {
            System.out.println(client.getClass().getName() + "---OK");
            try {
                client.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void failed(Exception ex) {
            System.out.println(client.getClass().getName() + "---FAIL");
            ex.printStackTrace();
            try {
                client.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void cancelled() {
            System.out.println(client.getClass().getName() + "---CANCEL");
            try {
                client.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });
    return "httpclientAsynctest success";
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpAsyncClient(org.apache.http.impl.nio.client.CloseableHttpAsyncClient) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) ClientProtocolException(org.apache.http.client.ClientProtocolException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 7 with CloseableHttpAsyncClient

use of org.apache.http.impl.nio.client.CloseableHttpAsyncClient in project nifi by apache.

the class SiteToSiteRestApiClient method initiateTransactionForSend.

/**
 * <p>
 * Initiate a transaction for sending data.
 * </p>
 *
 * <p>
 * If a proxy server requires auth, the proxy server returns 407 response with available auth schema such as basic or digest.
 * Then client has to resend the same request with its credential added.
 * This mechanism is problematic for sending data from NiFi.
 * </p>
 *
 * <p>
 * In order to resend a POST request with auth param,
 * NiFi has to either read flow-file contents to send again, or keep the POST body somewhere.
 * If we store that in memory, it would causes OOM, or storing it on disk slows down performance.
 * Rolling back processing session would be overkill.
 * Reading flow-file contents only when it's ready to send in a streaming way is ideal.
 * </p>
 *
 * <p>
 * Additionally, the way proxy authentication is done is vary among Proxy server software.
 * Some requires 407 and resend cycle for every requests, while others keep a connection between a client and
 * the proxy server, then consecutive requests skip auth steps.
 * The problem is, that how should we behave is only told after sending a request to the proxy.
 * </p>
 *
 * In order to handle above concerns correctly and efficiently, this method do the followings:
 *
 * <ol>
 * <li>Send a GET request to controller resource, to initiate an HttpAsyncClient. The instance will be used for further requests.
 *      This is not required by the Site-to-Site protocol, but it can setup proxy auth state safely.</li>
 * <li>Send a POST request to initiate a transaction. While doing so, it captures how a proxy server works.
 * If 407 and resend cycle occurs here, it implies that we need to do the same thing again when we actually send the data.
 * Because if the proxy keeps using the same connection and doesn't require an auth step, it doesn't do so here.</li>
 * <li>Then this method stores whether the final POST request should wait for the auth step.
 * So that {@link #openConnectionForSend} can determine when to produce contents.</li>
 * </ol>
 *
 * <p>
 * The above special sequence is only executed when a proxy instance is set, and its username is set.
 * </p>
 *
 * @param post a POST request to establish transaction
 * @return POST request response
 * @throws IOException thrown if the post request failed
 */
private HttpResponse initiateTransactionForSend(final HttpPost post) throws IOException {
    if (shouldCheckProxyAuth()) {
        final CloseableHttpAsyncClient asyncClient = getHttpAsyncClient();
        final HttpGet get = createGetControllerRequest();
        final Future<HttpResponse> getResult = asyncClient.execute(get, null);
        try {
            final HttpResponse getResponse = getResult.get(readTimeoutMillis, TimeUnit.MILLISECONDS);
            logger.debug("Proxy auth check has done. getResponse={}", getResponse.getStatusLine());
        } catch (final ExecutionException e) {
            logger.debug("Something has happened at get controller requesting thread for proxy auth check. {}", e.getMessage());
            throw toIOException(e);
        } catch (TimeoutException | InterruptedException e) {
            throw new IOException(e);
        }
    }
    final HttpAsyncRequestProducer asyncRequestProducer = new HttpAsyncRequestProducer() {

        private boolean requestHasBeenReset = false;

        @Override
        public HttpHost getTarget() {
            return URIUtils.extractHost(post.getURI());
        }

        @Override
        public HttpRequest generateRequest() throws IOException, HttpException {
            final BasicHttpEntity entity = new BasicHttpEntity();
            post.setEntity(entity);
            return post;
        }

        @Override
        public void produceContent(ContentEncoder encoder, IOControl ioctrl) throws IOException {
            encoder.complete();
            if (shouldCheckProxyAuth() && requestHasBeenReset) {
                logger.debug("Produced content again, assuming the proxy server requires authentication.");
                proxyAuthRequiresResend.set(true);
            }
        }

        @Override
        public void requestCompleted(HttpContext context) {
            debugProxyAuthState(context);
        }

        @Override
        public void failed(Exception ex) {
            final String msg = String.format("Failed to create transaction for %s", post.getURI());
            logger.error(msg, ex);
            eventReporter.reportEvent(Severity.WARNING, EVENT_CATEGORY, msg);
        }

        @Override
        public boolean isRepeatable() {
            return true;
        }

        @Override
        public void resetRequest() throws IOException {
            requestHasBeenReset = true;
        }

        @Override
        public void close() throws IOException {
        }
    };
    final Future<HttpResponse> responseFuture = getHttpAsyncClient().execute(asyncRequestProducer, new BasicAsyncResponseConsumer(), null);
    final HttpResponse response;
    try {
        response = responseFuture.get(readTimeoutMillis, TimeUnit.MILLISECONDS);
    } catch (final ExecutionException e) {
        logger.debug("Something has happened at initiate transaction requesting thread. {}", e.getMessage());
        throw toIOException(e);
    } catch (TimeoutException | InterruptedException e) {
        throw new IOException(e);
    }
    return response;
}
Also used : ContentEncoder(org.apache.http.nio.ContentEncoder) HttpGet(org.apache.http.client.methods.HttpGet) IOControl(org.apache.http.nio.IOControl) HttpContext(org.apache.http.protocol.HttpContext) HttpResponse(org.apache.http.HttpResponse) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) IOException(java.io.IOException) HandshakeException(org.apache.nifi.remote.exception.HandshakeException) HttpException(org.apache.http.HttpException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) URISyntaxException(java.net.URISyntaxException) TimeoutException(java.util.concurrent.TimeoutException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) ProtocolException(org.apache.nifi.remote.exception.ProtocolException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) PortNotRunningException(org.apache.nifi.remote.exception.PortNotRunningException) MalformedURLException(java.net.MalformedURLException) UnknownPortException(org.apache.nifi.remote.exception.UnknownPortException) CertificateException(java.security.cert.CertificateException) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) HttpAsyncRequestProducer(org.apache.http.nio.protocol.HttpAsyncRequestProducer) BasicAsyncResponseConsumer(org.apache.http.nio.protocol.BasicAsyncResponseConsumer) CloseableHttpAsyncClient(org.apache.http.impl.nio.client.CloseableHttpAsyncClient) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 8 with CloseableHttpAsyncClient

use of org.apache.http.impl.nio.client.CloseableHttpAsyncClient in project jaeger-client-java by jaegertracing.

the class JaegerRequestAndResponseInterceptorIntegrationTest method testAsyncHttpClientTracing.

@Test
public void testAsyncHttpClientTracing() throws Exception {
    HttpAsyncClientBuilder clientBuilder = HttpAsyncClients.custom();
    CloseableHttpAsyncClient client = TracingInterceptors.addTo(clientBuilder, tracer).build();
    client.start();
    // Verify that parent span is on top of the stack _before_ request is made
    assertEquals(parentSpan, tracer.activeSpan());
    // Make a request to the async client and wait for response
    client.execute(new HttpHost("localhost", mockServerRule.getPort()), new BasicHttpRequest("GET", "/testing"), new FutureCallback<org.apache.http.HttpResponse>() {

        @Override
        public void completed(org.apache.http.HttpResponse result) {
        }

        @Override
        public void failed(Exception ex) {
        }

        @Override
        public void cancelled() {
        }
    }).get();
    // Verify that parent span is on top of the stack _after_ request is made
    assertEquals(parentSpan, tracer.activeSpan());
    verifyTracing(parentSpan);
}
Also used : HttpHost(org.apache.http.HttpHost) CloseableHttpAsyncClient(org.apache.http.impl.nio.client.CloseableHttpAsyncClient) HttpResponse(org.mockserver.model.HttpResponse) BasicHttpRequest(org.apache.http.message.BasicHttpRequest) FutureCallback(org.apache.http.concurrent.FutureCallback) HttpAsyncClientBuilder(org.apache.http.impl.nio.client.HttpAsyncClientBuilder) Test(org.junit.Test)

Example 9 with CloseableHttpAsyncClient

use of org.apache.http.impl.nio.client.CloseableHttpAsyncClient in project spring-cloud-sleuth by spring-cloud.

the class WebClientTests method shouldAttachTraceIdWhenCallingAnotherServiceForAsyncHttpClient.

@Test
@SuppressWarnings("unchecked")
public void shouldAttachTraceIdWhenCallingAnotherServiceForAsyncHttpClient() throws Exception {
    Span span = this.tracer.nextSpan().name("foo").start();
    CloseableHttpAsyncClient client = this.httpAsyncClientBuilder.build();
    try (Tracer.SpanInScope ws = this.tracer.withSpanInScope(span)) {
        client.start();
        Future<HttpResponse> future = client.execute(new HttpGet("http://localhost:" + port), new FutureCallback<HttpResponse>() {

            @Override
            public void completed(HttpResponse result) {
            }

            @Override
            public void failed(Exception ex) {
            }

            @Override
            public void cancelled() {
            }
        });
        then(future.get()).isNotNull();
    } finally {
        client.close();
    }
    then(this.tracer.currentSpan()).isNull();
    then(this.reporter.getSpans()).isNotEmpty().extracting("traceId", String.class).containsOnly(span.context().traceIdString());
    then(this.reporter.getSpans()).extracting("kind.name").contains("CLIENT");
}
Also used : Tracer(brave.Tracer) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpAsyncClient(org.apache.http.impl.nio.client.CloseableHttpAsyncClient) HttpResponse(org.apache.http.HttpResponse) Span(brave.Span) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 10 with CloseableHttpAsyncClient

use of org.apache.http.impl.nio.client.CloseableHttpAsyncClient in project drill by apache.

the class WebUtils method getHttpClient.

/**
 * Get singleton instance of an HTTP Client.
 *
 * @param drillConfig {@link DrillConfig} instance needed to retrieve Drill SSL parameters.
 * @return HTTP Client instance.
 * @throws Exception if unable to create an HTTP Client.
 */
private static CloseableHttpAsyncClient getHttpClient(DrillConfig drillConfig) throws Exception {
    CloseableHttpAsyncClient localHttpClient = httpClient;
    if (localHttpClient == null) {
        synchronized (WebUtils.class) {
            localHttpClient = httpClient;
            if (httpClient == null) {
                localHttpClient = createHttpClient(drillConfig);
                localHttpClient.start();
                httpClient = localHttpClient;
            }
        }
    }
    return localHttpClient;
}
Also used : CloseableHttpAsyncClient(org.apache.http.impl.nio.client.CloseableHttpAsyncClient)

Aggregations

CloseableHttpAsyncClient (org.apache.http.impl.nio.client.CloseableHttpAsyncClient)34 HttpResponse (org.apache.http.HttpResponse)18 IOException (java.io.IOException)13 HttpGet (org.apache.http.client.methods.HttpGet)12 Test (org.junit.Test)8 HttpHost (org.apache.http.HttpHost)7 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)6 Future (java.util.concurrent.Future)5 InputStreamReader (java.io.InputStreamReader)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 GET (javax.ws.rs.GET)4 Path (javax.ws.rs.Path)4 ClientProtocolException (org.apache.http.client.ClientProtocolException)4 RequestConfig (org.apache.http.client.config.RequestConfig)4 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)4 FutureCallback (org.apache.http.concurrent.FutureCallback)4 HttpAsyncRequestProducer (org.apache.http.nio.protocol.HttpAsyncRequestProducer)4 Before (org.junit.Before)4 URL (java.net.URL)3 HttpEntity (org.apache.http.HttpEntity)3