use of org.apache.http.impl.nio.client.CloseableHttpAsyncClient in project elasticsearch by elastic.
the class RemoteScrollableHitSourceTests method sourceWithMockedRemoteCall.
/**
* Creates a hit source that doesn't make the remote request and instead returns data from some files. Also requests are always returned
* synchronously rather than asynchronously.
*/
@SuppressWarnings("unchecked")
private RemoteScrollableHitSource sourceWithMockedRemoteCall(boolean mockRemoteVersion, ContentType contentType, String... paths) throws Exception {
URL[] resources = new URL[paths.length];
for (int i = 0; i < paths.length; i++) {
resources[i] = Thread.currentThread().getContextClassLoader().getResource("responses/" + paths[i].replace("fail:", ""));
if (resources[i] == null) {
throw new IllegalArgumentException("Couldn't find [" + paths[i] + "]");
}
}
CloseableHttpAsyncClient httpClient = mock(CloseableHttpAsyncClient.class);
when(httpClient.<HttpResponse>execute(any(HttpAsyncRequestProducer.class), any(HttpAsyncResponseConsumer.class), any(HttpClientContext.class), any(FutureCallback.class))).thenAnswer(new Answer<Future<HttpResponse>>() {
int responseCount = 0;
@Override
public Future<HttpResponse> answer(InvocationOnMock invocationOnMock) throws Throwable {
// Throw away the current thread context to simulate running async httpclient's thread pool
threadPool.getThreadContext().stashContext();
HttpAsyncRequestProducer requestProducer = (HttpAsyncRequestProducer) invocationOnMock.getArguments()[0];
FutureCallback<HttpResponse> futureCallback = (FutureCallback<HttpResponse>) invocationOnMock.getArguments()[3];
HttpEntityEnclosingRequest request = (HttpEntityEnclosingRequest) requestProducer.generateRequest();
URL resource = resources[responseCount];
String path = paths[responseCount++];
ProtocolVersion protocolVersion = new ProtocolVersion("http", 1, 1);
if (path.startsWith("fail:")) {
String body = Streams.copyToString(new InputStreamReader(request.getEntity().getContent(), StandardCharsets.UTF_8));
if (path.equals("fail:rejection.json")) {
StatusLine statusLine = new BasicStatusLine(protocolVersion, RestStatus.TOO_MANY_REQUESTS.getStatus(), "");
BasicHttpResponse httpResponse = new BasicHttpResponse(statusLine);
futureCallback.completed(httpResponse);
} else {
futureCallback.failed(new RuntimeException(body));
}
} else {
StatusLine statusLine = new BasicStatusLine(protocolVersion, 200, "");
HttpResponse httpResponse = new BasicHttpResponse(statusLine);
httpResponse.setEntity(new InputStreamEntity(FileSystemUtils.openFileURLStream(resource), contentType));
futureCallback.completed(httpResponse);
}
return null;
}
});
return sourceWithMockedClient(mockRemoteVersion, httpClient);
}
use of org.apache.http.impl.nio.client.CloseableHttpAsyncClient in project spring-framework by spring-projects.
the class HttpComponentsAsyncClientHttpRequestFactoryTests method defaultSettingsOfHttpAsyncClientLostOnExecutorCustomization.
@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());
}
use of org.apache.http.impl.nio.client.CloseableHttpAsyncClient in project uavstack by uavorg.
the class HttpService method httpclientAsyncDoubleTest.
@GET
@Path("httpclientAsync_double_test")
public String httpclientAsyncDoubleTest() {
httpclientAsynctest();
final CloseableHttpAsyncClient client = HttpAsyncClients.custom().build();
HttpUriRequest httpMethod = new HttpGet("http://weibo.com/");
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");
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 "httpclientAsync_double_test";
}
use of org.apache.http.impl.nio.client.CloseableHttpAsyncClient in project uavstack by uavorg.
the class HttpService method httpclientAsynctest.
/**
* 异步测试用例
*
* @return
*/
@GET
@Path("httpclientAsynctest")
public String httpclientAsynctest() {
final CloseableHttpAsyncClient client = HttpAsyncClients.custom().build();
HttpUriRequest httpMethod = new HttpGet("https://www.baidu.com/");
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");
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";
}
use of org.apache.http.impl.nio.client.CloseableHttpAsyncClient in project tutorials by eugenp.
the class HttpAsyncClientLiveTest method whenUseHttpAsyncClient_thenCorrect.
// tests
@Test
public void whenUseHttpAsyncClient_thenCorrect() throws InterruptedException, ExecutionException, IOException {
final CloseableHttpAsyncClient client = HttpAsyncClients.createDefault();
client.start();
final HttpGet request = new HttpGet(HOST);
final Future<HttpResponse> future = client.execute(request, null);
final HttpResponse response = future.get();
assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
client.close();
}
Aggregations