@Override public void addListener(Runnable listener, Executor executor) { future.addListener(listener, executor); }
@Override public void addListener(Runnable r, Executor exec) { this.ahcFuture.addListener(r, exec); }
/** * Creates a {@link CompletionStage} from a {@link ListenableFuture}. * @param listenableFuture the future of the ning library * @param executor the executor to run the future in * @param <T> Type of the value that will be returned. * @return the Java 8 future implementation */ private static <T> CompletionStage<T> wrap(final ListenableFuture<T> listenableFuture, final Executor executor) { final CompletableFuture<T> result = new CompletableFuture<>(); final Runnable listener = () -> { try { final T value = listenableFuture.get(); result.complete(value); } catch (final InterruptedException | ExecutionException e) { result.completeExceptionally(e.getCause()); } }; listenableFuture.addListener(listener, executor); return result; }
/** * Creates a {@link CompletionStage} from a {@link ListenableFuture}. * @param listenableFuture the future of the ning library * @param executor the executor to run the future in * @param <T> Type of the value that will be returned. * @return the Java 8 future implementation */ private static <T> CompletionStage<T> wrap(final ListenableFuture<T> listenableFuture, final Executor executor) { final CompletableFuture<T> result = new CompletableFuture<>(); final Runnable listener = () -> { try { final T value = listenableFuture.get(); result.complete(value); } catch (final InterruptedException | ExecutionException e) { result.completeExceptionally(e.getCause()); } }; listenableFuture.addListener(listener, executor); return result; }
/** * Creates a {@link java.util.concurrent.CompletableFuture} from a {@link com.ning.http.client.ListenableFuture}. * @param listenableFuture the future of the ning library * @param executor the executor to run the future in * @param <T> Type of the value that will be returned. * @return the Java 8 future implementation */ private static <T> CompletableFuture<T> wrap(final ListenableFuture<T> listenableFuture, final Executor executor) { final CompletableFuture<T> result = new CompletableFuture<>(); final Runnable listener = () -> { try { final T value = listenableFuture.get(); result.complete(value); } catch (final InterruptedException | ExecutionException e) { result.completeExceptionally(e.getCause()); } }; listenableFuture.addListener(listener, executor); return result; }
/** * Creates a {@link java.util.concurrent.CompletableFuture} from a {@link com.ning.http.client.ListenableFuture}. * @param listenableFuture the future of the ning library * @param executor the executor to run the future in * @param <T> Type of the value that will be returned. * @return the Java 8 future implementation */ static <T> CompletableFuture<T> wrap(final ListenableFuture<T> listenableFuture, final Executor executor) { final CompletableFuture<T> result = new CompletableFuture<T>(); final Runnable listener = () -> { try { final T value = listenableFuture.get(); result.complete(value); } catch (final InterruptedException | ExecutionException e) { result.completeExceptionally(e.getCause()); } }; listenableFuture.addListener(listener, executor); return result; }
/** * Creates a {@link CompletionStage} from a {@link ListenableFuture}. * @param listenableFuture the future of the ning library * @param executor the executor to run the future in * @param <T> Type of the value that will be returned. * @return the Java 8 future implementation */ private static <T> CompletionStage<T> wrap(final ListenableFuture<T> listenableFuture, final Executor executor) { final CompletableFuture<T> result = new CompletableFuture<>(); final Runnable listener = () -> { try { final T value = listenableFuture.get(); result.complete(value); } catch (final InterruptedException | ExecutionException e) { result.completeExceptionally(e.getCause()); } }; listenableFuture.addListener(listener, executor); return result; }
/** * Creates a {@link CompletionStage} from a {@link ListenableFuture}. * @param listenableFuture the future of the ning library * @param executor the executor to run the future in * @param <T> Type of the value that will be returned. * @return the Java 8 future implementation */ private static <T> CompletionStage<T> wrap(final ListenableFuture<T> listenableFuture, final Executor executor) { final CompletableFuture<T> result = new CompletableFuture<>(); final Runnable listener = () -> { try { final T value = listenableFuture.get(); result.complete(value); } catch (final InterruptedException | ExecutionException e) { result.completeExceptionally(e.getCause()); } }; listenableFuture.addListener(listener, executor); return result; }
responseFuture.addListener(new Runnable() { @Override public void run() {
private ListenableFuture<WebResponse> simpleAsyncRequest(RequestBuilder requestBuilder, Executor executor) { requestBuilder = addHeadersToRequestBuilder(requestBuilder); try { final SettableFuture<WebResponse> future = SettableFuture.create(); final com.ning.http.client.ListenableFuture<Response> responseFuture = httpClient.executeRequest( requestBuilder.build()); responseFuture.addListener(new Runnable() { @Override public void run() { try { future.set(new WebResponseImpl(injector, responseFuture.get())); } catch (InterruptedException e) { throw new TransportException(e); } catch (ExecutionException e) { throw new TransportException(e); } } }, executor); return future; } catch (IOException e) { throw new TransportException(e); } }
@Test(groups = { "standalone", "default_provider" }) public void testListenableFuture() throws Throwable { final AtomicInteger statusCode = new AtomicInteger(500); try (AsyncHttpClient client = getAsyncHttpClient(null)) { final CountDownLatch latch = new CountDownLatch(1); final ListenableFuture<Response> future = client.prepareGet(getTargetUrl()).execute(); future.addListener(new Runnable() { public void run() { try { statusCode.set(future.get().getStatusCode()); latch.countDown(); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } }, Executors.newFixedThreadPool(1)); latch.await(10, TimeUnit.SECONDS); assertEquals(statusCode.get(), 200); } } }
public static <T> ComposableFuture<T> fromListenableFuture(final Provider<T> provider) { return ComposableFutures.build(consumer -> { final ListenableFuture<T> source = provider.provide(); source.addListener(() -> { try { final T result = source.get(); consumer.consume(Try.fromValue(result)); } catch (final InterruptedException e) { consumer.consume(Try.fromError(e)); } catch (final ExecutionException e) { final Throwable error = e.getCause() != null ? e.getCause() : e; consumer.consume(Try.fromError(error)); } }, ComposableFutures.getExecutor()); }); } }