下面列出了怎么用org.apache.http.impl.nio.client.CloseableHttpAsyncClient的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* 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());
}
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;
}
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();
}
}
@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();
}
@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());
}
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();
}
}
@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());
}
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);
}
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();
}
@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();
}
@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));
}
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();
}
@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());
}
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");
}
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();
}
}
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();
}
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();
}
@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());
}
@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());
}
@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;
}
@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());
}
/**
* 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;
}
/**
* 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;
}
/**
* 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;
}
@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();
}
/**
* 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;
}
private HttpAsyncClient startAsyncClient() {
HttpAsyncClient client = getAsyncClient();
if (client instanceof CloseableHttpAsyncClient) {
CloseableHttpAsyncClient closeableAsyncClient = (CloseableHttpAsyncClient) client;
if (!closeableAsyncClient.isRunning()) {
closeableAsyncClient.start();
}
}
return client;
}
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;
}
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);
}
}
@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();
}