本文整理汇总了Java中org.asynchttpclient.ListenableFuture类的典型用法代码示例。如果您正苦于以下问题:Java ListenableFuture类的具体用法?Java ListenableFuture怎么用?Java ListenableFuture使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ListenableFuture类属于org.asynchttpclient包,在下文中一共展示了ListenableFuture类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: sendRequest
import org.asynchttpclient.ListenableFuture; //导入依赖的package包/类
public <T> ListenableFuture<T> sendRequest(final Request request,//
final AsyncHandler<T> asyncHandler,//
NettyResponseFuture<T> future,//
boolean reclaimCache) {
if (isClosed())
throw new IllegalStateException("Closed");
validateWebSocketRequest(request, asyncHandler);
ProxyServer proxyServer = getProxyServer(config, request);
// websockets use connect tunnelling to work with proxies
if (proxyServer != null && (request.getUri().isSecured() || request.getUri().isWebSocket()) && !isConnectDone(request, future))
if (future != null && future.isConnectAllowed())
// SSL proxy or websocket: CONNECT for sure
return sendRequestWithCertainForceConnect(request, asyncHandler, future, reclaimCache, proxyServer, true);
else
// CONNECT will depend if we can pool or connection or if we have to open a new one
return sendRequestThroughSslProxy(request, asyncHandler, future, reclaimCache, proxyServer);
else
// no CONNECT for sure
return sendRequestWithCertainForceConnect(request, asyncHandler, future, reclaimCache, proxyServer, false);
}
示例2: sendRequestWithCertainForceConnect
import org.asynchttpclient.ListenableFuture; //导入依赖的package包/类
/**
* We know for sure if we have to force to connect or not, so we can build the HttpRequest right away This reduces the probability of having a pooled channel closed by the
* server by the time we build the request
*/
private <T> ListenableFuture<T> sendRequestWithCertainForceConnect(//
Request request,//
AsyncHandler<T> asyncHandler,//
NettyResponseFuture<T> future,//
boolean reclaimCache,//
ProxyServer proxyServer,//
boolean forceConnect) {
NettyResponseFuture<T> newFuture = newNettyRequestAndResponseFuture(request, asyncHandler, future, proxyServer, forceConnect);
Channel channel = getOpenChannel(future, request, proxyServer, asyncHandler);
if (Channels.isChannelValid(channel))
return sendRequestWithOpenChannel(request, proxyServer, newFuture, asyncHandler, channel);
else
return sendRequestWithNewChannel(request, proxyServer, newFuture, asyncHandler, reclaimCache);
}
示例3: sendRequestWithOpenChannel
import org.asynchttpclient.ListenableFuture; //导入依赖的package包/类
private <T> ListenableFuture<T> sendRequestWithOpenChannel(Request request, ProxyServer proxy, NettyResponseFuture<T> future, AsyncHandler<T> asyncHandler, Channel channel) {
if (asyncHandler instanceof AsyncHandlerExtensions)
AsyncHandlerExtensions.class.cast(asyncHandler).onConnectionPooled(channel);
scheduleRequestTimeout(future);
future.setChannelState(ChannelState.POOLED);
future.attachChannel(channel, false);
LOGGER.debug("Using open Channel {} for {} '{}'", channel, future.getNettyRequest().getHttpRequest().getMethod(), future.getNettyRequest().getHttpRequest().getUri());
if (Channels.isChannelValid(channel)) {
Channels.setAttribute(channel, future);
writeRequest(future, channel);
} else {
// bad luck, the channel was closed in-between
// there's a very good chance onClose was already notified but the
// future wasn't already registered
handleUnexpectedClosedChannel(channel, future);
}
return future;
}
示例4: testMaxTotalConnectionsException
import org.asynchttpclient.ListenableFuture; //导入依赖的package包/类
@Test(groups = "standalone", expectedExceptions = TooManyConnectionsException.class)
public void testMaxTotalConnectionsException() throws Throwable {
try (AsyncHttpClient client = asyncHttpClient(config().setKeepAlive(true).setMaxConnections(1))) {
String url = getTargetUrl();
List<ListenableFuture<Response>> futures = new ArrayList<>();
for (int i = 0; i < 5; i++) {
logger.info("{} requesting url [{}]...", i, url);
futures.add(client.prepareGet(url).execute());
}
Exception exception = null;
for (ListenableFuture<Response> future : futures) {
try {
future.get();
} catch (Exception ex) {
exception = ex;
break;
}
}
assertNotNull(exception);
throw exception.getCause();
}
}
示例5: waitForAndAssertResponse
import org.asynchttpclient.ListenableFuture; //导入依赖的package包/类
private void waitForAndAssertResponse(ListenableFuture<Response> responseFuture) throws InterruptedException, java.util.concurrent.ExecutionException, IOException {
Response response = responseFuture.get();
if (500 == response.getStatusCode()) {
StringBuilder sb = new StringBuilder();
sb.append("==============\n");
sb.append("500 response from call\n");
sb.append("Headers:" + response.getHeaders() + "\n");
sb.append("==============\n");
logger.debug(sb.toString());
assertEquals(response.getStatusCode(), 500, "Should have 500 status code");
assertTrue(response.getHeader("X-Exception").contains("invalid.chunk.length"), "Should have failed due to chunking");
fail("HARD Failing the test due to provided InputStreamBodyGenerator, chunking incorrectly:" + response.getHeader("X-Exception"));
} else {
assertEquals(response.getResponseBodyAsBytes(), LARGE_IMAGE_BYTES);
}
}
示例6: shouldCreateClusterAdminClient
import org.asynchttpclient.ListenableFuture; //导入依赖的package包/类
@Test
public void shouldCreateClusterAdminClient() throws ExecutionException, InterruptedException, IOException {
// Given
final BoundRequestBuilder boundRequestBuilder = mock(BoundRequestBuilder.class);
final ListenableFuture<Response> listenableFuture = mock(ListenableFuture.class);
final Response response = mock(Response.class);
when(response.getStatusCode()).thenReturn(200);
when(response.getResponseBody()).thenReturn("{\"status\":\"GREEN\", \"cluster_name\":\"someClusterName\", \"timed_out\":\"someTimedOut\"}");
when(listenableFuture.get()).thenReturn(response);
when(boundRequestBuilder.execute()).thenReturn(listenableFuture);
when(httpClient.prepareGet(anyString())).thenReturn(boundRequestBuilder);
final ClusterAdminClient cluster = adminClient.cluster();
final ClusterHealthRequestBuilder clusterHealthRequestBuilder = cluster.prepareHealth("someIndexName");
//When
clusterHealthRequestBuilder.execute();
//Then
Mockito.verify(httpClient).prepareGet("/_cluster/health/someIndexName");
assertThat(cluster, notNullValue());
}
示例7: shouldCreateIndicesAdminClient
import org.asynchttpclient.ListenableFuture; //导入依赖的package包/类
@Test
public void shouldCreateIndicesAdminClient() throws ExecutionException, InterruptedException, IOException {
//Given
final BoundRequestBuilder boundRequestBuilder = mock(BoundRequestBuilder.class);
when(httpClient.preparePut(anyString())).thenReturn(boundRequestBuilder);
when(boundRequestBuilder.setBody(anyString())).thenReturn(boundRequestBuilder);
when(boundRequestBuilder.setCharset(Charset.forName("UTF-8"))).thenReturn(boundRequestBuilder);
final ListenableFuture<Response> listenableFuture = mock(ListenableFuture.class);
when(boundRequestBuilder.execute()).thenReturn(listenableFuture);
final Response response = mock(Response.class);
when(listenableFuture.get()).thenReturn(response);
when(response.getStatusCode()).thenReturn(200);
when(response.getResponseBody()).thenReturn("{\"acknowledged\":\"true\"}");
final IndicesAdminClient indicesAdminClient = adminClient.indices();
final CreateIndexRequestBuilder createIndexRequestBuilder = indicesAdminClient.prepareCreate("someIndexName");
//When
createIndexRequestBuilder.execute();
//Then
verify(httpClient).preparePut("/someIndexName");
assertThat(indicesAdminClient, notNullValue());
}
示例8: shouldAddQueryToException
import org.asynchttpclient.ListenableFuture; //导入依赖的package包/类
@Test
public void shouldAddQueryToException() throws Exception {
//given
BoundRequestBuilder boundRequestBuilderMock = mock(BoundRequestBuilder.class);
when(httpClient.preparePost("/some-index/_search")).thenReturn(boundRequestBuilderMock);
when(boundRequestBuilderMock.setBody(any(String.class))).thenReturn(boundRequestBuilderMock);
when(boundRequestBuilderMock.setCharset(Charset.forName("UTF-8"))).thenReturn(boundRequestBuilderMock);
TimeoutException timeoutException = new TimeoutException();
when(boundRequestBuilderMock.execute()).thenReturn(new ListenableFuture.CompletedFailure<>(timeoutException));
//when
JsonObject query = createSampleQuery();
try {
searchRequestBuilder.setQuery(query).execute();
} catch (RuntimeException e) {
//then
assertThat(e.getCause().getCause(), is(timeoutException));
assertThat(e.getMessage(), is("{\"query\":" + query.toString() + "}"));
}
}
示例9: shouldPrepareCount
import org.asynchttpclient.ListenableFuture; //导入依赖的package包/类
@Test
public void shouldPrepareCount() throws ExecutionException, InterruptedException, IOException {
//Given
final BoundRequestBuilder boundRequestBuilder = mock(BoundRequestBuilder.class);
when(asyncHttpClient.prepareGet(anyString())).thenReturn(boundRequestBuilder);
final ListenableFuture listenableFuture = mock(ListenableFuture.class);
when(boundRequestBuilder.execute()).thenReturn(listenableFuture);
when(boundRequestBuilder.setBody(anyString())).thenReturn(boundRequestBuilder);
final Response response = mock(Response.class);
when(listenableFuture.get()).thenReturn(response);
when(response.getStatusCode()).thenReturn(200);
when(response.getResponseBody()).thenReturn("{\"count\":201}");
final CountRequestBuilder countRequestBuilder = client.prepareCount("someIndexName");
//When
countRequestBuilder.execute();
//Then
verify(asyncHttpClient).prepareGet("http://someHost:9200/someIndexName/_count");
}
示例10: shouldPrepareBulk
import org.asynchttpclient.ListenableFuture; //导入依赖的package包/类
@Test
public void shouldPrepareBulk() throws ExecutionException, InterruptedException, IOException {
//Given
final BoundRequestBuilder boundRequestBuilder = mock(BoundRequestBuilder.class);
when(asyncHttpClient.preparePost(anyString())).thenReturn(boundRequestBuilder);
final ListenableFuture listenableFuture = mock(ListenableFuture.class);
when(boundRequestBuilder.execute()).thenReturn(listenableFuture);
when(boundRequestBuilder.setBody(anyString())).thenReturn(boundRequestBuilder);
when(boundRequestBuilder.setCharset(Charset.forName("UTF-8"))).thenReturn(boundRequestBuilder);
final Response response = mock(Response.class);
when(listenableFuture.get()).thenReturn(response);
when(response.getStatusCode()).thenReturn(200);
when(response.getResponseBody()).thenReturn("{\"errors\":false}");
final BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();
bulkRequestBuilder.add(new DeleteActionBuilder("someIndexName", "someId", "someType"));
//When
bulkRequestBuilder.execute();
//Then
verify(asyncHttpClient).preparePost("http://someHost:9200/_bulk");
}
示例11: shouldPrepareGet
import org.asynchttpclient.ListenableFuture; //导入依赖的package包/类
@Test
public void shouldPrepareGet() throws ExecutionException, InterruptedException, IOException {
//Given
final BoundRequestBuilder boundRequestBuilder = mock(BoundRequestBuilder.class);
when(asyncHttpClient.prepareGet(anyString())).thenReturn(boundRequestBuilder);
final ListenableFuture listenableFuture = mock(ListenableFuture.class);
when(boundRequestBuilder.execute()).thenReturn(listenableFuture);
final Response response = mock(Response.class);
when(listenableFuture.get()).thenReturn(response);
when(response.getStatusCode()).thenReturn(200);
when(response.getResponseBody()).thenReturn("{\"_id\":\"46711\"}");
final GetRequestBuilder getRequestBuilder = client.prepareGet("someIndexName", "someDocumentType", "someProductId");
//When
getRequestBuilder.execute();
//Then
verify(asyncHttpClient).prepareGet("http://someHost:9200/someIndexName/someDocumentType/someProductId");
}
示例12: shouldPrepareDelete
import org.asynchttpclient.ListenableFuture; //导入依赖的package包/类
@Test
public void shouldPrepareDelete() throws ExecutionException, InterruptedException, IOException {
//Given
final BoundRequestBuilder boundRequestBuilder = mock(BoundRequestBuilder.class);
when(asyncHttpClient.prepareDelete(anyString())).thenReturn(boundRequestBuilder);
final ListenableFuture listenableFuture = mock(ListenableFuture.class);
when(boundRequestBuilder.execute()).thenReturn(listenableFuture);
final Response response = mock(Response.class);
when(listenableFuture.get()).thenReturn(response);
when(response.getStatusCode()).thenReturn(200);
final DeleteRequestBuilder deleteRequestBuilder = client.prepareDelete();
deleteRequestBuilder.setIndexName("someIndexName");
deleteRequestBuilder.setDocumentType("someDocumentType");
deleteRequestBuilder.setId("someId");
//When
deleteRequestBuilder.execute();
//Then
verify(asyncHttpClient).prepareDelete("http://someHost:9200/someIndexName/someDocumentType/someId");
}
示例13: shouldPrepareAnalyze
import org.asynchttpclient.ListenableFuture; //导入依赖的package包/类
@Test
public void shouldPrepareAnalyze() throws ExecutionException, InterruptedException, IOException {
final BoundRequestBuilder boundRequestBuilder = mock(BoundRequestBuilder.class);
when(asyncHttpClient.prepareGet(anyString())).thenReturn(boundRequestBuilder);
final ListenableFuture listenableFuture = mock(ListenableFuture.class);
when(boundRequestBuilder.setCharset(Charset.forName("UTF-8"))).thenReturn(boundRequestBuilder);
when(boundRequestBuilder.setBody(anyString())).thenReturn(boundRequestBuilder);
when(boundRequestBuilder.execute()).thenReturn(listenableFuture);
final Response response = mock(Response.class);
when(response.getStatusCode()).thenReturn(200);
when(response.getResponseBody()).thenReturn("{ \"tokens\": [] }");
when(listenableFuture.get()).thenReturn(response);
final AnalyzeRequestBuilder analyzeRequestBuilder = client.prepareAnalyze("hello world");
//When
analyzeRequestBuilder.execute();
//Then
verify(asyncHttpClient).prepareGet("http://someHost:9200/_analyze");
}
示例14: shouldPrepareIndex
import org.asynchttpclient.ListenableFuture; //导入依赖的package包/类
@Test
public void shouldPrepareIndex() throws ExecutionException, InterruptedException, IOException {
//Given
final BoundRequestBuilder boundRequestBuilder = mock(BoundRequestBuilder.class);
when(asyncHttpClient.preparePost(anyString())).thenReturn(boundRequestBuilder);
when(boundRequestBuilder.setBody(anyString())).thenReturn(boundRequestBuilder);
when(boundRequestBuilder.setCharset(Charset.forName("UTF-8"))).thenReturn(boundRequestBuilder);
final ListenableFuture listenableFuture = mock(ListenableFuture.class);
when(boundRequestBuilder.execute()).thenReturn(listenableFuture);
final Response response = mock(Response.class);
when(listenableFuture.get()).thenReturn(response);
when(response.getStatusCode()).thenReturn(200);
final IndexRequestBuilder indexRequestBuilder = client.prepareIndex();
indexRequestBuilder.setSource(new JsonObject());
//When
indexRequestBuilder.execute();
//Then
verify(asyncHttpClient).preparePost("http://someHost:9200");
}
示例15: shouldGetAdminClientCluster
import org.asynchttpclient.ListenableFuture; //导入依赖的package包/类
@Test
public void shouldGetAdminClientCluster() throws ExecutionException, InterruptedException, IOException {
// Given
final BoundRequestBuilder boundRequestBuilder = mock(BoundRequestBuilder.class);
final ListenableFuture<Response> listenableFuture = mock(ListenableFuture.class);
final Response response = mock(Response.class);
when(response.getStatusCode()).thenReturn(200);
when(response.getResponseBody()).thenReturn("{\"status\":\"GREEN\", \"cluster_name\":\"someClusterName\", \"timed_out\":\"someTimedOut\"}");
when(listenableFuture.get()).thenReturn(response);
when(boundRequestBuilder.execute()).thenReturn(listenableFuture);
when(asyncHttpClient.prepareGet(anyString())).thenReturn(boundRequestBuilder);
final ClusterAdminClient cluster = client.admin().cluster();
final ClusterHealthRequestBuilder clusterHealthRequestBuilder = cluster.prepareHealth("someIndexName");
//When
clusterHealthRequestBuilder.execute();
//Then
Mockito.verify(asyncHttpClient).prepareGet("http://someHost:9200/_cluster/health/someIndexName");
assertThat(cluster, notNullValue());
}