Example usage for org.apache.http.concurrent FutureCallback cancelled

List of usage examples for org.apache.http.concurrent FutureCallback cancelled

Introduction

In this page you can find the example usage for org.apache.http.concurrent FutureCallback cancelled.

Prototype

void cancelled();

Source Link

Usage

From source file:co.paralleluniverse.fibers.httpasyncclient.FiberCloseableHttpAsyncClient.java

private static <T> FutureCallback<T> wrapCallbackWithFuture(final SettableFuture<T> future,
        final FutureCallback<T> callback) {
    return new FutureCallback<T>() {

        @Override/*from   w w w . j a va  2  s.c  o  m*/
        public void completed(T result) {
            future.set(result);
            callback.completed(result);
        }

        @Override
        public void failed(Exception ex) {
            future.setException(ex);
            callback.failed(ex);
        }

        @Override
        public void cancelled() {
            future.cancel(true);
            callback.cancelled();
        }
    };
}

From source file:com.clxcommunications.xms.CallbackWrapperTest.java

@Test
public void dropperLogsCancelled() throws Exception {
    FutureCallback<Integer> wrapped = CallbackWrapper.exceptionDropper.wrap(exceptionalCallback);

    wrapped.cancelled();

    assertThat(logger.getLoggingEvents(), is(Arrays.asList(LoggingEvent.error(CANCELLED_EXCEPTION,
            "caught and dropped exception in callback: {}", CANCELLED_EXCEPTION.getMessage()))));
}

From source file:com.vmware.loginsightapi.LogInsightClientMockTest.java

@Test
public void testIngestionCancelled() {
    Message msg1 = new Message("Testing the ingestion");
    msg1.addField("vclap_test_id", "11111");
    IngestionRequest request = new IngestionRequest();
    request.addMessage(msg1);//ww w  .  jav a2 s.c o  m
    testIngestionQueryUrlAndHeaders(request);

    doAnswer(new Answer<Future<HttpResponse>>() {
        @Override
        public Future<HttpResponse> answer(InvocationOnMock invocation) {
            FutureCallback<HttpResponse> responseCallback = invocation.getArgumentAt(1, FutureCallback.class);
            responseCallback.cancelled();
            return null;
        }
    }).when(asyncHttpClient).execute(any(HttpUriRequest.class), any(FutureCallback.class));

    try {
        CompletableFuture<IngestionResponse> responseFuture = client.ingest(request);
        responseFuture.get(0, TimeUnit.MILLISECONDS);
    } catch (ExecutionException e) {
        logger.error("Exception raised " + ExceptionUtils.getStackTrace(e));
        Assert.assertTrue(e.getCause() instanceof LogInsightApiException);
        Assert.assertEquals(e.getCause().getMessage(), "Ingestion cancelled");
    } catch (Exception e) {
        Assert.assertTrue(false);
    }
}

From source file:com.vmware.loginsightapi.LogInsightClientMockTest.java

@Test
public void testMessageQueryCancelled() {
    MessageQuery mqb = getMessageQueryForTest();
    testMessageQueryUrlAndHeaders(mqb);/*from  www .j a v  a 2s.c o  m*/

    doAnswer(new Answer<Future<HttpResponse>>() {
        @Override
        public Future<HttpResponse> answer(InvocationOnMock invocation) {
            @SuppressWarnings("unchecked")
            FutureCallback<HttpResponse> responseCallback = invocation.getArgumentAt(1, FutureCallback.class);
            responseCallback.cancelled();
            return null;
        }
    }).when(asyncHttpClient).execute(any(HttpUriRequest.class), any(FutureCallback.class));

    try {
        CompletableFuture<MessageQueryResponse> responseFuture = client.messageQuery(mqb.toUrlString());
        responseFuture.get(0, TimeUnit.MILLISECONDS);
    } catch (ExecutionException e) {
        logger.error("Exception raised " + ExceptionUtils.getStackTrace(e));
        Assert.assertTrue(e.getCause() instanceof LogInsightApiException);
        Assert.assertEquals(e.getCause().getMessage(), "Cancelled message Query");
    } catch (Exception e) {
        Assert.assertTrue(false);
    }
}

From source file:com.vmware.loginsightapi.LogInsightClientMockTest.java

@Test
public void testAggregateQueryCancelled() {
    List<FieldConstraint> constraints = new ConstraintBuilder().eq("vclap_caseid", "1423244")
            .gt("timestamp", "0").build();
    AggregateQuery aqb = (AggregateQuery) new AggregateQuery().limit(100).setConstraints(constraints);
    testAggregateQueryUrlAndHeaders(aqb);

    doAnswer(new Answer<Future<HttpResponse>>() {
        @Override//from  w  w  w  .j a v a 2  s  .  c  o  m
        public Future<HttpResponse> answer(InvocationOnMock invocation) {
            @SuppressWarnings("unchecked")
            FutureCallback<HttpResponse> responseCallback = invocation.getArgumentAt(1, FutureCallback.class);
            responseCallback.cancelled();
            return null;
        }
    }).when(asyncHttpClient).execute(any(HttpUriRequest.class), any(FutureCallback.class));

    try {
        CompletableFuture<AggregateResponse> responseFuture = client.aggregateQuery(aqb.toUrlString());
        responseFuture.get(0, TimeUnit.MILLISECONDS);
    } catch (ExecutionException e) {
        logger.error("Exception raised " + ExceptionUtils.getStackTrace(e));
        Assert.assertTrue(e.getCause() instanceof LogInsightApiException);
        Assert.assertEquals(e.getCause().getMessage(), "Cancelled message Query");
    } catch (Exception e) {
        Assert.assertTrue(false);
    }
}

From source file:com.ysheng.auth.common.restful.BaseClient.java

/**
 * Performs an asynchronous DELETE operation.
 *
 * @param path The path of the RESTful operation.
 * @param expectedHttpStatus The expected HTTP status of the operation.
 * @param responseHandler The asynchronous response handler.
 * @throws IOException The error that contains detail information.
 *///  ww w. j  a v a 2  s . c  om
public final void deleteAsync(final String path, final int expectedHttpStatus,
        final FutureCallback<Void> responseHandler) throws IOException {
    restClient.performAsync(RestClient.Method.DELETE, path, null, new FutureCallback<HttpResponse>() {
        @Override
        public void completed(HttpResponse httpResponse) {
            try {
                restClient.checkResponse(httpResponse, expectedHttpStatus);
            } catch (Exception e) {
                responseHandler.failed(e);
            }

            responseHandler.completed(null);
        }

        @Override
        public void failed(Exception e) {
            responseHandler.failed(e);
        }

        @Override
        public void cancelled() {
            responseHandler.cancelled();
        }
    });
}

From source file:com.ysheng.auth.common.restful.BaseClient.java

/**
 * Performs an asynchronous GET operation.
 *
 * @param path The path of the RESTful operation.
 * @param expectedHttpStatus The expected HTTP status of the operation.
 * @param responseHandler The asynchronous response handler.
 * @param <T> The type of the response.
 * @throws IOException The error that contains detail information.
 *//*from   w w  w. j av  a 2 s .c o m*/
public final <T> void getAsync(final String path, final int expectedHttpStatus,
        final FutureCallback<T> responseHandler) throws IOException {
    restClient.performAsync(RestClient.Method.GET, path, null, new FutureCallback<HttpResponse>() {
        @Override
        public void completed(HttpResponse httpResponse) {
            T response = null;
            try {
                restClient.checkResponse(httpResponse, expectedHttpStatus);
                response = JsonSerializer.deserialize(httpResponse.getEntity(), new TypeReference<T>() {
                });
            } catch (Exception e) {
                responseHandler.failed(e);
            }

            responseHandler.completed(response);
        }

        @Override
        public void failed(Exception e) {
            responseHandler.failed(e);
        }

        @Override
        public void cancelled() {
            responseHandler.cancelled();
        }
    });
}

From source file:com.ysheng.auth.common.restful.BaseClient.java

/**
 * Performs an asynchronous POST operation.
 *
 * @param path The path of the RESTful operation.
 * @param payload The payload of the RESTful operation.
 * @param expectedHttpStatus The expected HTTP status of the operation.
 * @param responseHandler The asynchronous response handler.
 * @param <T> The type of the response.
 * @throws IOException The error that contains detail information.
 */// ww  w.j a va 2s  . co m
public final <T> void postAsync(final String path, final Object payload, final int expectedHttpStatus,
        final FutureCallback<T> responseHandler) throws IOException {
    restClient.performAsync(RestClient.Method.POST, path, JsonSerializer.serialize(payload),
            new FutureCallback<HttpResponse>() {
                @Override
                public void completed(HttpResponse httpResponse) {
                    T response = null;

                    try {
                        restClient.checkResponse(httpResponse, expectedHttpStatus);
                        response = JsonSerializer.deserialize(httpResponse.getEntity(), new TypeReference<T>() {
                        });
                    } catch (Exception e) {
                        responseHandler.failed(e);
                    }

                    responseHandler.completed(response);
                }

                @Override
                public void failed(Exception e) {
                    responseHandler.failed(e);
                }

                @Override
                public void cancelled() {
                    responseHandler.cancelled();
                }
            });
}

From source file:org.apache.http.impl.nio.conn.TestPoolingHttpClientAsyncConnectionManager.java

@Test
public void testRequestConnectionCancelled() throws Exception {
    final HttpHost target = new HttpHost("localhost");
    final HttpRoute route = new HttpRoute(target);
    final Future<NHttpClientConnection> future = connman.requestConnection(route, "some state", 1000L, 2000L,
            TimeUnit.MILLISECONDS, null);
    Assert.assertNotNull(future);/*from  w  w w  .j a  v a 2  s  .c om*/

    Mockito.verify(pool).lease(Matchers.same(route), Matchers.eq("some state"), Matchers.eq(1000L),
            Matchers.eq(2000L), Matchers.eq(TimeUnit.MILLISECONDS), poolEntryCallbackCaptor.capture());
    final FutureCallback<CPoolEntry> callaback = poolEntryCallbackCaptor.getValue();
    callaback.cancelled();

    Assert.assertTrue(future.isDone());
    Assert.assertTrue(future.isCancelled());
    Assert.assertNull(future.get());
}