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: 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 #2
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 #3
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 #4
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 #5
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 #6
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 #7
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 #8
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 #9
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 #10
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 #11
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 #12
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 #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: 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 #15
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 #16
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 #17
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 #18
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 #19
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 #20
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 #21
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 #22
Source File: HttpComponentsAsyncClientHttpRequestFactory.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Create a new instance of the {@code HttpComponentsAsyncClientHttpRequestFactory}
 * with the given {@link CloseableHttpClient} and {@link CloseableHttpAsyncClient} instances.
 * @param httpClient the CloseableHttpClient instance to use for this request factory
 * @param asyncClient the CloseableHttpAsyncClient instance to use for this request factory
 */
public HttpComponentsAsyncClientHttpRequestFactory(
		CloseableHttpClient httpClient, CloseableHttpAsyncClient asyncClient) {

	super(httpClient);
	this.asyncClient = asyncClient;
}
 
Example #23
Source File: HttpComponentsAsyncClientHttpRequestFactory.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Return the {@code CloseableHttpAsyncClient} used for
 * {@linkplain #createAsyncRequest(URI, HttpMethod) asynchronous execution}.
 * @deprecated as of 4.3.10, in favor of {@link #getAsyncClient()}
 */
@Deprecated
public CloseableHttpAsyncClient getHttpAsyncClient() {
	Assert.state(this.asyncClient instanceof CloseableHttpAsyncClient,
			"No CloseableHttpAsyncClient - use getAsyncClient() instead");
	return (CloseableHttpAsyncClient) this.asyncClient;
}
 
Example #24
Source File: HttpComponentsAsyncClientHttpRequestFactory.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Create a new instance of the {@code HttpComponentsAsyncClientHttpRequestFactory}
 * with the given {@link CloseableHttpClient} and {@link CloseableHttpAsyncClient} instances.
 * @param httpClient the CloseableHttpClient instance to use for this request factory
 * @param asyncClient the CloseableHttpAsyncClient instance to use for this request factory
 */
public HttpComponentsAsyncClientHttpRequestFactory(
		CloseableHttpClient httpClient, CloseableHttpAsyncClient asyncClient) {

	super(httpClient);
	this.asyncClient = asyncClient;
}
 
Example #25
Source File: HttpAsyncClientLiveTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenUseSSLWithHttpAsyncClient_thenCorrect() throws Exception {
    final TrustStrategy acceptingTrustStrategy = (certificate, authType) -> true;
    final SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();

    final CloseableHttpAsyncClient client = HttpAsyncClients.custom().setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE).setSSLContext(sslContext).build();

    client.start();
    final HttpGet request = new HttpGet(HOST_WITH_SSL);
    final Future<HttpResponse> future = client.execute(request, null);
    final HttpResponse response = future.get();
    assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
    client.close();
}
 
Example #26
Source File: HttpComponentsAsyncClientHttpRequestFactory.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Return the {@code CloseableHttpAsyncClient} used for
 * {@linkplain #createAsyncRequest(URI, HttpMethod) asynchronous execution}.
 * @deprecated as of 4.3.10, in favor of {@link #getAsyncClient()}
 */
@Deprecated
public CloseableHttpAsyncClient getHttpAsyncClient() {
	Assert.state(this.asyncClient instanceof CloseableHttpAsyncClient,
			"No CloseableHttpAsyncClient - use getAsyncClient() instead");
	return (CloseableHttpAsyncClient) this.asyncClient;
}
 
Example #27
Source File: HttpComponentsAsyncClientHttpRequestFactory.java    From java-technology-stack with MIT License 5 votes vote down vote up
private HttpAsyncClient startAsyncClient() {
	HttpAsyncClient client = getAsyncClient();
	if (client instanceof CloseableHttpAsyncClient) {
		CloseableHttpAsyncClient closeableAsyncClient = (CloseableHttpAsyncClient) client;
		if (!closeableAsyncClient.isRunning()) {
			closeableAsyncClient.start();
		}
	}
	return client;
}
 
Example #28
Source File: TestUtils.java    From salt-netapi-client with MIT License 5 votes vote down vote up
public static CloseableHttpAsyncClient defaultClient() {
    RequestConfig requestConfig = RequestConfig.custom()
            .setConnectionRequestTimeout(0)
            .setConnectTimeout(0)
            .setSocketTimeout(0)
            .setCookieSpec(CookieSpecs.STANDARD)
            .build();
    HttpAsyncClientBuilder httpClientBuilder = HttpAsyncClients.custom();
    httpClientBuilder.setDefaultRequestConfig(requestConfig);

    CloseableHttpAsyncClient asyncHttpClient = httpClientBuilder.build();
    asyncHttpClient.start();
    return asyncHttpClient;
}
 
Example #29
Source File: HttpDownloadHandler.java    From cetty with Apache License 2.0 5 votes vote down vote up
private void asyncHttpClientDownload(HandlerContext ctx, Seed seed) {
    Payload payload = ctx.cetty().getPayload();
    CloseableHttpAsyncClient httpAsyncClient = ctx.cetty().getHttpAsyncClient();

    try {
        httpAsyncClient.execute(convertHttpUriRequest(seed, payload), convertHttpClientContext(seed, payload), new CallBack(seed, ctx, payload));
    } catch (Exception e) {
        logger.warn("download {} page error !", seed.getUrl(), e);
    }
}
 
Example #30
Source File: AsyncHttpClientGenerator.java    From cetty with Apache License 2.0 5 votes vote down vote up
@Override
protected CloseableHttpAsyncClient build(Payload payload) {
    HttpAsyncClientBuilder asyncClientBuilder = HttpAsyncClients.custom();

    if (StringUtils.isNotBlank(payload.getUserAgent())) {
        asyncClientBuilder.setUserAgent(payload.getUserAgent());
    } else {
        asyncClientBuilder.setUserAgent("");
    }

    asyncClientBuilder.setRedirectStrategy(new CustomRedirectStrategy());

    asyncClientBuilder.setConnectionManagerShared(true);

    RequestConfig requestConfig = RequestConfig.custom()
            .setConnectTimeout(payload.getConnectTimeout())
            .setSocketTimeout(payload.getSocketTimeout()).build();

    ConnectionConfig connectionConfig = ConnectionConfig.custom()
            .setMalformedInputAction(CodingErrorAction.IGNORE)
            .setUnmappableInputAction(CodingErrorAction.IGNORE)
            .setCharset(Consts.UTF_8).build();

    poolingNHttpClientConnectionManager.setDefaultConnectionConfig(connectionConfig);
    asyncClientBuilder.setConnectionManager(poolingNHttpClientConnectionManager);
    asyncClientBuilder.setDefaultRequestConfig(requestConfig);
    if (payload.getProxy() != null) {
        Proxy proxy = payload.getProxy();
        HttpHost httpHost = new HttpHost(proxy.getHost(), proxy.getPort(), proxy.getScheme());
        asyncClientBuilder.setProxy(httpHost);
    }
    reduceCookie(asyncClientBuilder,payload);
    return asyncClientBuilder.build();
}