org.apache.http.impl.nio.client.CloseableHttpAsyncClient Java Examples

The following examples show how to use org.apache.http.impl.nio.client.CloseableHttpAsyncClient. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Async.java    From salt-netapi-client with MIT License 6 votes vote down vote up
public static void main(String[] args) {
    CloseableHttpAsyncClient httpClient = HttpClientUtils.defaultClient();
    // Init the client
    SaltClient client = new SaltClient(URI.create(SALT_API_URL), new HttpAsyncClientImpl(httpClient));

    // Clean up afterwards by calling close()
    Runnable cleanup = () -> {
        try {
            httpClient.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    };

    // Perform a non-blocking login
    client.login(USER, PASSWORD, AuthModule.AUTO)
            .thenAccept(t -> System.out.println("Token -> " + t.getToken()))
            .thenRun(cleanup);
}
 
Example #2
Source File: BufferedJestHttpClientTest.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
@Test
public void executeAsyncDelegatesToFailureHandlerOnPrepareRequestIOException() throws IOException {

    // given
    BufferedJestHttpClient client = spy(createDefaultTestHttpClient());
    CloseableHttpAsyncClient asyncClient = mockAsyncClient(client);

    String expectedMesage = UUID.randomUUID().toString();
    BufferedBulk bulk = createDefaultTestBufferedBulk();
    when(client.prepareRequest(bulk)).thenThrow(new IOException(expectedMesage));

    JestResultHandler<JestResult> jestResultHandler = createMockTestResultHandler();

    // when
    client.executeAsync(bulk, jestResultHandler);

    // then
    verify(jestResultHandler).failed(exceptionCaptor.capture());
    assertEquals(expectedMesage, exceptionCaptor.getValue().getMessage());

    verify(client, never()).getAsyncClient();
    verify(asyncClient, never()).execute(any(HttpUriRequest.class), any());

}
 
Example #3
Source File: RestClient.java    From light with Apache License 2.0 6 votes vote down vote up
private CloseableHttpAsyncClient asyncHttpClient() throws Exception {
    PoolingNHttpClientConnectionManager connectionManager = new PoolingNHttpClientConnectionManager(
            ioReactor(), asyncRegistry());
    Map<String, Object> asyncHttpClientMap = (Map<String, Object>)configMap.get(ASYNC_REST_TEMPLATE);
    connectionManager.setMaxTotal((Integer)asyncHttpClientMap.get(MAX_CONNECTION_TOTAL));
    connectionManager.setDefaultMaxPerRoute((Integer) asyncHttpClientMap.get(MAX_CONNECTION_PER_ROUTE));
    // Now handle all the specific route defined.
    Map<String, Object> routeMap = (Map<String, Object>)asyncHttpClientMap.get(ROUTES);
    Iterator<String> it = routeMap.keySet().iterator();
    while (it.hasNext()) {
        String route = it.next();
        Integer maxConnection = (Integer)routeMap.get(route);
        connectionManager.setMaxPerRoute(new HttpRoute(new HttpHost(
                route)), maxConnection);
    }
    RequestConfig config = RequestConfig.custom()
            .setConnectTimeout((Integer) asyncHttpClientMap.get(TIMEOUT_MILLISECONDS))
            .build();

    return HttpAsyncClientBuilder
            .create()
            .setConnectionManager(connectionManager)
            .setDefaultRequestConfig(config)
            .build();
}
 
Example #4
Source File: FiberApacheHttpClientRequestExecutor.java    From jbender with Apache License 2.0 6 votes vote down vote up
public FiberApacheHttpClientRequestExecutor(final Validator<CloseableHttpResponse> resValidator, final int maxConnections, final int timeout, final int parallelism) throws IOReactorException {
  final DefaultConnectingIOReactor ioreactor = new DefaultConnectingIOReactor(IOReactorConfig.custom().
    setConnectTimeout(timeout).
    setIoThreadCount(parallelism).
    setSoTimeout(timeout).
    build());

  final PoolingNHttpClientConnectionManager mngr = new PoolingNHttpClientConnectionManager(ioreactor);
  mngr.setDefaultMaxPerRoute(maxConnections);
  mngr.setMaxTotal(maxConnections);

  final CloseableHttpAsyncClient ahc = HttpAsyncClientBuilder.create().
    setConnectionManager(mngr).
    setDefaultRequestConfig(RequestConfig.custom().setLocalAddress(null).build()).build();

  client = new FiberHttpClient(ahc);
  validator = resValidator;
}
 
Example #5
Source File: BufferedJestHttpClientTest.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
@Test
public void executeAsyncDelegatesToConfiguredAsyncClient() {

    // given
    BufferedJestHttpClient client = spy(createDefaultTestHttpClient());
    CloseableHttpAsyncClient asyncClient = mockAsyncClient(client);

    Bulk bulk = createDefaultTestBufferedBulk();

    // when
    client.executeAsync(bulk, createMockTestResultHandler());

    // then
    verify(client).getAsyncClient();
    verify(asyncClient).execute(any(HttpUriRequest.class), any());

}
 
Example #6
Source File: ODataProductSynchronizer.java    From DataHubSystem with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public CloseableHttpAsyncClient generateClient ()
{
   CredentialsProvider credsProvider = new BasicCredentialsProvider();
   credsProvider.setCredentials(new AuthScope (AuthScope.ANY),
           new UsernamePasswordCredentials(serviceUser, servicePass));
   RequestConfig rqconf = RequestConfig.custom()
         .setCookieSpec(CookieSpecs.DEFAULT)
         .setSocketTimeout(Timeouts.SOCKET_TIMEOUT)
         .setConnectTimeout(Timeouts.CONNECTION_TIMEOUT)
         .setConnectionRequestTimeout(Timeouts.CONNECTION_REQUEST_TIMEOUT)
         .build();
   CloseableHttpAsyncClient res = HttpAsyncClients.custom ()
         .setDefaultCredentialsProvider (credsProvider)
         .setDefaultRequestConfig(rqconf)
         .build ();
   res.start ();
   return res;
}
 
Example #7
Source File: AsyncClientExecuteProxy.java    From yunpian-java-sdk with MIT License 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
    try {
        httpclient.start();
        HttpHost proxy = new HttpHost("someproxy", 8080);
        RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
        HttpGet request = new HttpGet("https://issues.apache.org/");
        request.setConfig(config);
        Future<HttpResponse> future = httpclient.execute(request, null);
        HttpResponse response = future.get();
        System.out.println("Response: " + response.getStatusLine());
        System.out.println("Shutting down");
    } finally {
        httpclient.close();
    }
}
 
Example #8
Source File: HttpClientTest.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
@Test
public void lifecycleStartStartsPoolOnlyOnce() {

    // given
    PoolingAsyncResponseConsumerFactory asyncResponseConsumerFactory =
            mock(PoolingAsyncResponseConsumerFactory.class);

    HttpClient httpClient = createTestHttpClient(
            mock(CloseableHttpAsyncClient.class),
            mock(ServerPool.class),
            mock(RequestFactory.class),
            asyncResponseConsumerFactory);

    // when
    httpClient.start();
    httpClient.start();

    // then
    verify(asyncResponseConsumerFactory, times(1)).start();

}
 
Example #9
Source File: HttpClientTest.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
@Test
public void executeAsyncDelegatesToFailureHandlerOnCreateClientRequestIOException() throws IOException {

    // given
    RequestFactory requestFactory = mock(RequestFactory.class);
    HttpClient client = createTestHttpClient(
            mock(CloseableHttpAsyncClient.class),
            mock(ServerPool.class),
            requestFactory,
            mock(HttpAsyncResponseConsumerFactory.class)
    );

    String expectedMessage = UUID.randomUUID().toString();
    BatchRequest request = createDefaultTestBatchRequest();
    when(requestFactory.create(any(), any())).thenThrow(new IOException(expectedMessage));

    ResponseHandler<Response> responseHandler = createMockTestResultHandler();

    // when
    client.executeAsync(request, responseHandler);

    // then
    verify(responseHandler).failed(exceptionCaptor.capture());
    assertEquals(expectedMessage, exceptionCaptor.getValue().getMessage());

}
 
Example #10
Source File: HttpProxyClient.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
private CloseableHttpAsyncClient getAsyncProxyHttpClient(URI proxyUri) {
  LOG.info("Creating async proxy http client");
  PoolingNHttpClientConnectionManager cm = getAsyncConnectionManager();
  HttpHost proxy = new HttpHost(proxyUri.getHost(), proxyUri.getPort());
  
  HttpAsyncClientBuilder clientBuilder = HttpAsyncClients.custom();
  if (cm != null) {
    clientBuilder = clientBuilder.setConnectionManager(cm);
  }

  if (proxy != null) {
    clientBuilder = clientBuilder.setProxy(proxy);
  }
  clientBuilder = setRedirects(clientBuilder);
  return clientBuilder.build();
}
 
Example #11
Source File: AsyncClientProxyAuthentication.java    From yunpian-java-sdk with MIT License 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope("someproxy", 8080),
            new UsernamePasswordCredentials("username", "password"));
    CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom().setDefaultCredentialsProvider(credsProvider)
            .build();
    try {
        httpclient.start();
        HttpHost proxy = new HttpHost("someproxy", 8080);
        RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
        HttpGet httpget = new HttpGet("https://issues.apache.org/");
        httpget.setConfig(config);
        Future<HttpResponse> future = httpclient.execute(httpget, null);
        HttpResponse response = future.get();
        System.out.println("Response: " + response.getStatusLine());
        System.out.println("Shutting down");
    } finally {
        httpclient.close();
    }
}
 
Example #12
Source File: AsyncClientHttpExchangeStreaming.java    From yunpian-java-sdk with MIT License 6 votes vote down vote up
public static void main(final String[] args) throws Exception {
    CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
    try {
        httpclient.start();
        Future<Boolean> future = httpclient.execute(HttpAsyncMethods.createGet("http://localhost:8080/"),
                new MyResponseConsumer(), null);
        Boolean result = future.get();
        if (result != null && result.booleanValue()) {
            System.out.println("Request successfully executed");
        } else {
            System.out.println("Request failed");
        }
        System.out.println("Shutting down");
    } finally {
        httpclient.close();
    }
    System.out.println("Done");
}
 
Example #13
Source File: PiwikTrackerTest.java    From matomo-java-tracker with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testSendBulkRequestAsync_Iterable_StringFT() throws Exception {
    List<PiwikRequest> requests = new ArrayList<>();
    CloseableHttpAsyncClient client = mock(CloseableHttpAsyncClient.class);
    PiwikRequest request = mock(PiwikRequest.class);
    HttpResponse response = mock(HttpResponse.class);
    Future<HttpResponse> future = mock(Future.class);
    doReturn(response).when(future).get();
    doReturn(true).when(future).isDone();

    doReturn("query").when(request).getQueryString();
    requests.add(request);
    doReturn(client).when(piwikTracker).getHttpAsyncClient();
    doReturn(future).when(client)
            .execute(argThat(new CorrectPostRequest("{\"requests\":[\"?query\"],\"token_auth\":\"12345678901234567890123456789012\"}")), any());

    assertEquals(response, piwikTracker.sendBulkRequestAsync(requests, "12345678901234567890123456789012").get());
}
 
Example #14
Source File: HttpClient.java    From aliyun-tsdb-java-sdk with Apache License 2.0 6 votes vote down vote up
HttpClient(Config config, CloseableHttpAsyncClient httpclient, SemaphoreManager semaphoreManager, ScheduledExecutorService connectionGcService)
        throws HttpClientInitException {
    this.host = config.getHost();
    this.port = config.getPort();
    this.httpCompress = config.isHttpCompress();
    this.httpclient = httpclient;
    this.semaphoreManager = semaphoreManager;
    this.httpAddressManager = HttpAddressManager.createHttpAddressManager(config);
    this.unCompletedTaskNum = new AtomicInteger(0);
    this.httpResponseCallbackFactory = new HttpResponseCallbackFactory(unCompletedTaskNum, this, this.httpCompress);
    this.connectionGcService = connectionGcService;
    this.sslEnable = config.isSslEnable();
    this.authType = config.getAuthType();
    this.instanceId = config.getInstanceId();
    this.tsdbUser = config.getTsdbUser();
    this.basicPwd = config.getBasicPwd();
    this.certContent = config.getCertContent();
}
 
Example #15
Source File: PiwikTrackerTest.java    From matomo-java-tracker with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Test of sendRequestAsync method, of class PiwikTracker.
 */
@Test
public void testSendRequestAsync() throws Exception {
    PiwikRequest request = mock(PiwikRequest.class);
    CloseableHttpAsyncClient client = mock(CloseableHttpAsyncClient.class);
    HttpResponse response = mock(HttpResponse.class);
    Future<HttpResponse> future = mock(Future.class);

    doReturn(client).when(piwikTracker).getHttpAsyncClient();
    doReturn("query").when(request).getQueryString();
    doReturn(response).when(future).get();
    doReturn(true).when(future).isDone();
    doReturn(future).when(client)
            .execute(argThat(new CorrectGetRequest("http://test.com?query")), any());

    assertEquals(response, piwikTracker.sendRequestAsync(request).get());
}
 
Example #16
Source File: HttpAsyncClientLiveTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void whenUseCookiesWithHttpAsyncClient_thenCorrect() throws Exception {
    final BasicCookieStore cookieStore = new BasicCookieStore();
    final BasicClientCookie cookie = new BasicClientCookie(COOKIE_NAME, "1234");
    cookie.setDomain(COOKIE_DOMAIN);
    cookie.setPath("/");
    cookieStore.addCookie(cookie);
    final CloseableHttpAsyncClient client = HttpAsyncClients.custom().build();
    client.start();
    final HttpGet request = new HttpGet(HOST_WITH_COOKIE);

    final HttpContext localContext = new BasicHttpContext();
    localContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);

    final Future<HttpResponse> future = client.execute(request, localContext, null);
    final HttpResponse response = future.get();
    assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
    client.close();
}
 
Example #17
Source File: HttpClientFactoryTest.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
@Test
public void createInstanceConfiguresAsyncHttpClient() {

    // given
    HttpClientFactory factory = Mockito.spy(createDefaultTestHttpClientFactory());
    HttpClient client = spy(factory.createConfiguredClient(any(), any(), any()));
    when(factory.createConfiguredClient(any(), any(), any())).thenReturn(client);

    NHttpClientConnectionManager connectionManager = mock(NHttpClientConnectionManager.class);
    when(factory.getAsyncConnectionManager()).thenReturn(connectionManager);

    CloseableHttpAsyncClient httpAsyncClient = mock(CloseableHttpAsyncClient.class);
    when(factory.createAsyncHttpClient(any())).thenReturn(httpAsyncClient);

    // when
    factory.createInstance();

    // then
    verify(factory, times(1)).createAsyncHttpClient(eq(connectionManager));

}
 
Example #18
Source File: HttpAsyncDownloaderBuilder.java    From JMCCC with MIT License 6 votes vote down vote up
protected CloseableHttpAsyncClient buildDefaultHttpAsyncClient() {
	HttpHost httpProxy = resolveProxy(proxy);
	return HttpAsyncClientBuilder.create()
			.setMaxConnTotal(maxConnections)
			.setMaxConnPerRoute(maxConnections)
			.setProxy(httpProxy)
			.setDefaultIOReactorConfig(IOReactorConfig.custom()
					.setConnectTimeout(connectTimeout)
					.setSoTimeout(readTimeout)
					.build())
			.setDefaultRequestConfig(RequestConfig.custom()
					.setConnectTimeout(connectTimeout)
					.setSocketTimeout(readTimeout)
					.setProxy(httpProxy)
					.build())
			.setDefaultHeaders(Arrays.asList(new BasicHeader("Accept-Encoding", "gzip")))
			.build();
}
 
Example #19
Source File: PiwikTrackerTest.java    From matomo-java-tracker with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testSendBulkRequestAsync_Iterable_StringFF() throws Exception {
    List<PiwikRequest> requests = new ArrayList<>();
    CloseableHttpAsyncClient client = mock(CloseableHttpAsyncClient.class);
    PiwikRequest request = mock(PiwikRequest.class);
    HttpResponse response = mock(HttpResponse.class);
    Future<HttpResponse> future = mock(Future.class);
    doReturn(response).when(future).get();
    doReturn(true).when(future).isDone();

    doReturn("query").when(request).getQueryString();
    requests.add(request);
    doReturn(client).when(piwikTracker).getHttpAsyncClient();
    doReturn(future).when(client)
            .execute(argThat(new CorrectPostRequest("{\"requests\":[\"?query\"]}")), any());

    assertEquals(response, piwikTracker.sendBulkRequestAsync(requests, null).get());
}
 
Example #20
Source File: AsyncClientAuthentication.java    From yunpian-java-sdk with MIT License 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope("localhost", 443),
            new UsernamePasswordCredentials("username", "password"));
    CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom().setDefaultCredentialsProvider(credsProvider)
            .build();
    try {
        HttpGet httpget = new HttpGet("http://localhost/");

        System.out.println("Executing request " + httpget.getRequestLine());
        Future<HttpResponse> future = httpclient.execute(httpget, null);
        HttpResponse response = future.get();
        System.out.println("Response: " + response.getStatusLine());
        System.out.println("Shutting down");
    } finally {
        httpclient.close();
    }
}
 
Example #21
Source File: HttpComponentsAsyncClientHttpRequestFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void defaultSettingsOfHttpAsyncClientLostOnExecutorCustomization() throws Exception {
	CloseableHttpAsyncClient client = HttpAsyncClientBuilder.create()
			.setDefaultRequestConfig(RequestConfig.custom().setConnectTimeout(1234).build())
			.build();
	HttpComponentsAsyncClientHttpRequestFactory factory = new HttpComponentsAsyncClientHttpRequestFactory(client);

	URI uri = new URI(baseUrl + "/status/ok");
	HttpComponentsAsyncClientHttpRequest request = (HttpComponentsAsyncClientHttpRequest)
			factory.createAsyncRequest(uri, HttpMethod.GET);

	assertNull("No custom config should be set with a custom HttpClient",
			request.getHttpContext().getAttribute(HttpClientContext.REQUEST_CONFIG));

	factory.setConnectionRequestTimeout(4567);
	HttpComponentsAsyncClientHttpRequest request2 = (HttpComponentsAsyncClientHttpRequest)
			factory.createAsyncRequest(uri, HttpMethod.GET);
	Object requestConfigAttribute = request2.getHttpContext().getAttribute(HttpClientContext.REQUEST_CONFIG);
	assertNotNull(requestConfigAttribute);
	RequestConfig requestConfig = (RequestConfig) requestConfigAttribute;

	assertEquals(4567, requestConfig.getConnectionRequestTimeout());
	// No way to access the request config of the HTTP client so no way to "merge" our customizations
	assertEquals(-1, requestConfig.getConnectTimeout());
}
 
Example #22
Source File: AsyncClientHttpExchangeFutureCallback.java    From yunpian-java-sdk with MIT License 5 votes vote down vote up
public static void main(final String[] args) throws Exception {
    RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(3000).setConnectTimeout(3000).build();
    CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom().setDefaultRequestConfig(requestConfig).build();
    try {
        httpclient.start();
        final HttpGet[] requests = new HttpGet[] { new HttpGet("http://www.apache.org/"),
                new HttpGet("https://www.verisign.com/"), new HttpGet("http://www.google.com/") };
        final CountDownLatch latch = new CountDownLatch(requests.length);
        for (final HttpGet request : requests) {
            httpclient.execute(request, new FutureCallback<HttpResponse>() {

                @Override
                public void completed(final HttpResponse response) {
                    latch.countDown();
                    System.out.println(request.getRequestLine() + "->" + response.getStatusLine());
                }

                @Override
                public void failed(final Exception ex) {
                    latch.countDown();
                    System.out.println(request.getRequestLine() + "->" + ex);
                }

                @Override
                public void cancelled() {
                    latch.countDown();
                    System.out.println(request.getRequestLine() + " cancelled");
                }

            });
        }
        latch.await();
        System.out.println("Shutting down");
    } finally {
        httpclient.close();
    }
    System.out.println("Done");
}
 
Example #23
Source File: MicrometerHttpClientInterceptorTest.java    From micrometer with Apache License 2.0 5 votes vote down vote up
private CloseableHttpAsyncClient asyncClient() {
    MicrometerHttpClientInterceptor interceptor = new MicrometerHttpClientInterceptor(registry,
            request -> request.getRequestLine().getUri(),
            Tags.empty(),
            true);
    return asyncClient(interceptor);
}
 
Example #24
Source File: BceHttpClient.java    From bce-sdk-java with Apache License 2.0 5 votes vote down vote up
/**
 * Create asynchronous http client based on connection manager.
 *
 * @param connectionManager Asynchronous http client connection manager.
 * @return Asynchronous http client based on connection manager.
 */
protected CloseableHttpAsyncClient createHttpAsyncClient(NHttpClientConnectionManager connectionManager) {
    HttpAsyncClientBuilder builder = HttpAsyncClients.custom().setConnectionManager(connectionManager);

    int socketBufferSizeInBytes = this.config.getSocketBufferSizeInBytes();
    if (socketBufferSizeInBytes > 0) {
        builder.setDefaultConnectionConfig(
                ConnectionConfig.custom().setBufferSize(socketBufferSizeInBytes).build());
    }
    return builder.build();
}
 
Example #25
Source File: WebClientTests.java    From spring-cloud-sleuth with Apache License 2.0 5 votes vote down vote up
@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:" + this.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.spans).isNotEmpty().extracting("traceId", String.class)
			.containsOnly(span.context().traceIdString());
	then(this.spans).extracting("kind.name").contains("CLIENT");
}
 
Example #26
Source File: ApacheHttpAsyncClientPluginIT.java    From glowroot with Apache License 2.0 5 votes vote down vote up
@Override
public void transactionMarker() throws Exception {
    CloseableHttpAsyncClient httpClient = HttpAsyncClients.createDefault();
    httpClient.start();
    HttpGet httpGet = new HttpGet("http://localhost:" + getPort() + "/hello1");
    SimpleFutureCallback callback = new SimpleFutureCallback();
    Future<HttpResponse> future = httpClient.execute(httpGet, callback);
    callback.latch.await();
    httpClient.close();
    int responseStatusCode = future.get().getStatusLine().getStatusCode();
    if (responseStatusCode != 200) {
        throw new IllegalStateException(
                "Unexpected response status code: " + responseStatusCode);
    }
}
 
Example #27
Source File: HttpClientManager.java    From azure-notificationhubs-java-backend with Apache License 2.0 5 votes vote down vote up
public static void setHttpAsyncClient(CloseableHttpAsyncClient httpAsyncClient) {
    synchronized (HttpClientManager.class) {
        if (HttpClientManager.httpAsyncClient == null) {
            HttpClientManager.httpAsyncClient = httpAsyncClient;
        } else {
            throw new RuntimeException("Cannot setHttpAsyncClient after having previously set, or after default already initialized from earlier call to getHttpAsyncClient.");
        }
    }
}
 
Example #28
Source File: SaltClientTest.java    From salt-netapi-client with MIT License 5 votes vote down vote up
@Test
public void testRunRequestWithSocketTimeout() {
    exception.expect(CompletionException.class);
    exception.expectCause(instanceOf(SocketTimeoutException.class));

    RequestConfig requestConfig = RequestConfig.custom()
            .setSocketTimeout(1000)
            .setCookieSpec(CookieSpecs.STANDARD)
            .build();
    HttpAsyncClientBuilder httpClientBuilder = HttpAsyncClients.custom();
    httpClientBuilder.setDefaultRequestConfig(requestConfig);

    CloseableHttpAsyncClient asyncHttpClient = httpClientBuilder.build();
    asyncHttpClient.start();

    // create a local SaltClient with a fast timeout configuration
    // to do not lock tests more than 2s
    URI uri = URI.create("http://localhost:" + Integer.toString(MOCK_HTTP_PORT));
    SaltClient clientWithFastTimeout = new SaltClient(uri, new HttpAsyncClientImpl(asyncHttpClient));


    stubFor(any(urlMatching(".*"))
            .willReturn(aResponse()
            .withFixedDelay(2000)));

    clientWithFastTimeout.login("user", "pass", AUTO).toCompletableFuture().join();
}
 
Example #29
Source File: HttpClient.java    From log4j2-elasticsearch with Apache License 2.0 5 votes vote down vote up
/**
 * @param asyncClient actual Apache HTTP client
 * @param serverPool pool of servers to use
 * @param requestFactory {@link Request} adapter
 * @param asyncResponseConsumerFactory async response consumer provider
 */
HttpClient(
        CloseableHttpAsyncClient asyncClient,
        ServerPool serverPool,
        RequestFactory requestFactory,
        HttpAsyncResponseConsumerFactory asyncResponseConsumerFactory
) {
    this.asyncClient = asyncClient;
    this.serverPool = serverPool;
    this.httpRequestFactory = requestFactory;
    this.asyncResponseConsumerFactory = asyncResponseConsumerFactory;
}
 
Example #30
Source File: Options.java    From wisdom with Apache License 2.0 5 votes vote down vote up
/**
 * Refreshes the options, and restores defaults.
 */
public static void refresh() {
    // Load timeouts
    Object connectionTimeout = Options.getOption(Option.CONNECTION_TIMEOUT);
    if (connectionTimeout == null) {
        connectionTimeout = CONNECTION_TIMEOUT;
    }

    Object socketTimeout = Options.getOption(Option.SOCKET_TIMEOUT);
    if (socketTimeout == null) {
        socketTimeout = SOCKET_TIMEOUT;
    }

    // Create common default configuration
    final BasicCookieStore store = new BasicCookieStore();
    RequestConfig clientConfig = RequestConfig.custom()
            .setConnectTimeout(((Long) connectionTimeout).intValue() * TimeUtils.TIME_FACTOR)
            .setSocketTimeout(((Long) socketTimeout).intValue()  * TimeUtils.TIME_FACTOR)
            .setConnectionRequestTimeout(((Long) socketTimeout).intValue() * TimeUtils.TIME_FACTOR)
            .setCookieSpec(CookieSpecs.STANDARD)
            .build();

    // Create clients
    setOption(Option.HTTPCLIENT,
            HttpClientBuilder.create()
                    .setDefaultRequestConfig(clientConfig)
                    .setDefaultCookieStore(store)
                    .build()
    );

    setOption(Option.COOKIES, store);

    CloseableHttpAsyncClient asyncClient = HttpAsyncClientBuilder.create()
            .setDefaultRequestConfig(clientConfig)
            .setDefaultCookieStore(store)
            .build();
    setOption(Option.ASYNCHTTPCLIENT, asyncClient);
}