public <T> Future<T> execute( final Request request, final HttpClientResponseHandler<T> handler, final FutureCallback<T> callback) { final BasicFuture<T> future = new BasicFuture<>(callback); final ExecRunnable<T> runnable = new ExecRunnable<>( future, request, this.executor != null ? this.executor : Executor.newInstance(), handler); if (this.concurrentExec != null) { this.concurrentExec.execute(runnable); } else { final Thread t = new Thread(runnable); t.setDaemon(true); t.start(); } return future; }
/** * Creates a {@code ReactiveResponseConsumer}. */ public ReactiveResponseConsumer() { this.responseFuture = new BasicFuture<>(null); }
/** * Creates a {@code ReactiveResponseConsumer}. */ public ReactiveResponseConsumer() { this.responseFuture = new BasicFuture<>(null); }
public IOSessionRequest( final NamedEndpoint remoteEndpoint, final SocketAddress remoteAddress, final SocketAddress localAddress, final Timeout timeout, final Object attachment, final FutureCallback<IOSession> callback) { super(); this.remoteEndpoint = remoteEndpoint; this.remoteAddress = remoteAddress; this.localAddress = localAddress; this.timeout = timeout; this.attachment = attachment; this.future = new BasicFuture<>(callback); this.closeableRef = new AtomicReference<>(null); }
public IOSessionRequest( final NamedEndpoint remoteEndpoint, final SocketAddress remoteAddress, final SocketAddress localAddress, final Timeout timeout, final Object attachment, final FutureCallback<IOSession> callback) { super(); this.remoteEndpoint = remoteEndpoint; this.remoteAddress = remoteAddress; this.localAddress = localAddress; this.timeout = timeout; this.attachment = attachment; this.future = new BasicFuture<>(callback); this.closeableRef = new AtomicReference<>(null); }
/** * Creates a {@code ReactiveResponseConsumer} that will call back the supplied {@link FutureCallback} with a * streamable response. * * @param responseCallback the callback to invoke when the response is available for consumption. */ public ReactiveResponseConsumer(final FutureCallback<Message<HttpResponse, Publisher<ByteBuffer>>> responseCallback) { this.responseFuture = new BasicFuture<>(Args.notNull(responseCallback, "responseCallback")); }
/** * Creates a {@code ReactiveResponseConsumer} that will call back the supplied {@link FutureCallback} with a * streamable response. * * @param responseCallback the callback to invoke when the response is available for consumption. */ public ReactiveResponseConsumer(final FutureCallback<Message<HttpResponse, Publisher<ByteBuffer>>> responseCallback) { this.responseFuture = new BasicFuture<>(Args.notNull(responseCallback, "responseCallback")); }
public Future<ClientSessionEndpoint> connect( final HttpHost host, final Timeout timeout, final FutureCallback<ClientSessionEndpoint> callback) throws InterruptedException { final BasicFuture<ClientSessionEndpoint> future = new BasicFuture<>(callback); requestSession(host, timeout, new FutureCallback<IOSession>() { @Override public void completed(final IOSession session) { future.completed(new ClientSessionEndpoint(session)); } @Override public void failed(final Exception cause) { future.failed(cause); } @Override public void cancelled() { future.cancel(); } }); return future; }
public <T> Future<T> execute( final Request request, final HttpClientResponseHandler<T> handler, final FutureCallback<T> callback) { final BasicFuture<T> future = new BasicFuture<>(callback); final ExecRunnable<T> runnable = new ExecRunnable<>( future, request, this.executor != null ? this.executor : Executor.newInstance(), handler); if (this.concurrentExec != null) { this.concurrentExec.execute(runnable); } else { final Thread t = new Thread(runnable); t.setDaemon(true); t.start(); } return future; }
@Override public void consumeResponse( final HttpResponse response, final EntityDetails entityDetails, final HttpContext httpContext, final FutureCallback<Void> resultCallback ) { this.entityDetails = entityDetails; this.responseCompletion = new BasicFuture<>(resultCallback); this.responseFuture.completed(new Message<HttpResponse, Publisher<ByteBuffer>>(response, reactiveDataConsumer)); }
@Override public void consumeResponse( final HttpResponse response, final EntityDetails entityDetails, final HttpContext httpContext, final FutureCallback<Void> resultCallback ) { this.entityDetails = entityDetails; this.responseCompletion = new BasicFuture<>(resultCallback); this.responseFuture.completed(new Message<HttpResponse, Publisher<ByteBuffer>>(response, reactiveDataConsumer)); }
@Override public Future<ListenerEndpoint> listen(final SocketAddress address, final FutureCallback<ListenerEndpoint> callback) { if (getStatus().compareTo(IOReactorStatus.SHUTTING_DOWN) >= 0) { throw new IOReactorShutdownException("I/O reactor has been shut down"); } final BasicFuture<ListenerEndpoint> future = new BasicFuture<>(callback); this.requestQueue.add(new ListenerEndpointRequest(address, future)); this.selector.wakeup(); return future; }
@Override public Future<ListenerEndpoint> listen(final SocketAddress address, final FutureCallback<ListenerEndpoint> callback) { if (getStatus().compareTo(IOReactorStatus.SHUTTING_DOWN) >= 0) { throw new IOReactorShutdownException("I/O reactor has been shut down"); } final BasicFuture<ListenerEndpoint> future = new BasicFuture<>(callback); this.requestQueue.add(new ListenerEndpointRequest(address, future)); this.selector.wakeup(); return future; }
@Test(expected = CancellationException.class) public void testAsyncCancelled() throws Exception { final BasicFuture<Object> future = new BasicFuture<>(null); final Thread t = new Thread() { @Override public void run() { try { Thread.sleep(100); future.cancel(true); } catch (final InterruptedException ex) { } } }; t.setDaemon(true); t.start(); future.get(60, TimeUnit.SECONDS); }
@Test(expected=TimeoutValueException.class) public void testAsyncNegativeTimeout() throws Exception { final BasicFuture<Object> future = new BasicFuture<>(null); future.get(-1, TimeUnit.MILLISECONDS); }
@Test public void testCancelled() throws Exception { final ComplexFuture<Object> future = new ComplexFuture<>(null); final Future<Object> dependency1 = new BasicFuture<>(null); future.setDependency(dependency1); Assert.assertFalse(future.isDone()); future.cancel(); Assert.assertThat(future.isCancelled(), CoreMatchers.is(true)); Assert.assertThat(dependency1.isCancelled(), CoreMatchers.is(true)); final Future<Object> dependency2 = new BasicFuture<>(null); future.setDependency(dependency2); Assert.assertThat(dependency2.isCancelled(), CoreMatchers.is(true)); }
@Test public void testCompleted() throws Exception { final ComplexFuture<Object> future = new ComplexFuture<>(null); final Future<Object> dependency1 = new BasicFuture<>(null); future.setDependency(dependency1); Assert.assertFalse(future.isDone()); future.completed(Boolean.TRUE); Assert.assertThat(future.isCancelled(), CoreMatchers.is(false)); Assert.assertThat(dependency1.isCancelled(), CoreMatchers.is(false)); final Future<Object> dependency2 = new BasicFuture<>(null); future.setDependency(dependency2); Assert.assertThat(dependency2.isCancelled(), CoreMatchers.is(true)); }
@Test public void testCancelled() throws Exception { final ComplexCancellable cancellable = new ComplexCancellable(); final BasicFuture<Object> dependency1 = new BasicFuture<>(null); cancellable.setDependency(dependency1); Assert.assertFalse(cancellable.isCancelled()); cancellable.cancel(); Assert.assertThat(cancellable.isCancelled(), CoreMatchers.is(true)); Assert.assertThat(dependency1.isCancelled(), CoreMatchers.is(true)); final BasicFuture<Object> dependency2 = new BasicFuture<>(null); cancellable.setDependency(dependency2); Assert.assertThat(dependency2.isCancelled(), CoreMatchers.is(true)); }
@Test public void testCompletedWithTimeout() throws Exception { final BasicFutureCallback<Object> callback = new BasicFutureCallback<>(); final BasicFuture<Object> future = new BasicFuture<>(callback); Assert.assertFalse(future.isDone()); final Object result = new Object(); final Exception boom = new Exception(); future.completed(result); future.failed(boom); Assert.assertTrue(callback.isCompleted()); Assert.assertSame(result, callback.getResult()); Assert.assertFalse(callback.isFailed()); Assert.assertNull(callback.getException()); Assert.assertFalse(callback.isCancelled()); Assert.assertSame(result, future.get(1, TimeUnit.MILLISECONDS)); Assert.assertTrue(future.isDone()); Assert.assertFalse(future.isCancelled()); }
@Test public void testCompleted() throws Exception { final BasicFutureCallback<Object> callback = new BasicFutureCallback<>(); final BasicFuture<Object> future = new BasicFuture<>(callback); Assert.assertFalse(future.isDone()); final Object result = new Object(); final Exception boom = new Exception(); future.completed(result); future.failed(boom); Assert.assertTrue(callback.isCompleted()); Assert.assertSame(result, callback.getResult()); Assert.assertFalse(callback.isFailed()); Assert.assertNull(callback.getException()); Assert.assertFalse(callback.isCancelled()); Assert.assertSame(result, future.get()); Assert.assertTrue(future.isDone()); Assert.assertFalse(future.isCancelled()); }