use of org.apache.hc.core5.function.Callback in project httpcomponents-core by apache.
the class H2MultiplexingRequester method execute.
public final <T> Future<T> execute(final AsyncRequestProducer requestProducer, final AsyncResponseConsumer<T> responseConsumer, final HandlerFactory<AsyncPushConsumer> pushHandlerFactory, final Timeout timeout, final HttpContext context, final FutureCallback<T> callback) {
Args.notNull(requestProducer, "Request producer");
Args.notNull(responseConsumer, "Response consumer");
Args.notNull(timeout, "Timeout");
final ComplexFuture<T> future = new ComplexFuture<>(callback);
final AsyncClientExchangeHandler exchangeHandler = new BasicClientExchangeHandler<>(requestProducer, responseConsumer, new FutureContribution<T>(future) {
@Override
public void completed(final T result) {
future.completed(result);
}
});
execute(exchangeHandler, pushHandlerFactory, future, timeout, context != null ? context : HttpCoreContext.create());
return future;
}
use of org.apache.hc.core5.function.Callback in project httpcomponents-core by apache.
the class ClientHttp1UpgradeHandler method upgrade.
@Override
public void upgrade(final ProtocolIOSession ioSession, final FutureCallback<ProtocolIOSession> callback) {
final ClientHttp1IOEventHandler eventHandler = new ClientHttp1IOEventHandler(http1StreamHandlerFactory.create(ioSession));
ioSession.upgrade(eventHandler);
try {
eventHandler.connected(ioSession);
if (callback != null) {
callback.completed(ioSession);
}
} catch (final IOException ex) {
eventHandler.exception(ioSession, ex);
}
}
use of org.apache.hc.core5.function.Callback in project httpcomponents-core by apache.
the class HttpAsyncRequester method execute.
public final <T> Future<T> execute(final AsyncRequestProducer requestProducer, final AsyncResponseConsumer<T> responseConsumer, final HandlerFactory<AsyncPushConsumer> pushHandlerFactory, final Timeout timeout, final HttpContext context, final FutureCallback<T> callback) {
Args.notNull(requestProducer, "Request producer");
Args.notNull(responseConsumer, "Response consumer");
Args.notNull(timeout, "Timeout");
final BasicFuture<T> future = new BasicFuture<>(callback);
final AsyncClientExchangeHandler exchangeHandler = new BasicClientExchangeHandler<>(requestProducer, responseConsumer, new FutureContribution<T>(future) {
@Override
public void completed(final T result) {
future.completed(result);
}
});
execute(exchangeHandler, pushHandlerFactory, timeout, context != null ? context : HttpCoreContext.create());
return future;
}
use of org.apache.hc.core5.function.Callback in project httpcomponents-core by apache.
the class AbstractCharAsyncEntityConsumer method streamStart.
@Override
public final void streamStart(final EntityDetails entityDetails, final FutureCallback<T> resultCallback) throws IOException, HttpException {
Args.notNull(resultCallback, "Result callback");
this.resultCallback = resultCallback;
try {
final ContentType contentType = entityDetails != null ? ContentType.parse(entityDetails.getContentType()) : null;
setCharset(ContentType.getCharset(contentType, null));
streamStart(contentType);
} catch (final UnsupportedCharsetException ex) {
throw new UnsupportedEncodingException(ex.getMessage());
}
}
use of org.apache.hc.core5.function.Callback in project httpcomponents-core by apache.
the class StrictConnPool method lease.
@Override
public Future<PoolEntry<T, C>> lease(final T route, final Object state, final Timeout requestTimeout, final FutureCallback<PoolEntry<T, C>> callback) {
Args.notNull(route, "Route");
Args.notNull(requestTimeout, "Request timeout");
Asserts.check(!this.isShutDown.get(), "Connection pool shut down");
final Deadline deadline = Deadline.calculate(requestTimeout);
final BasicFuture<PoolEntry<T, C>> future = new BasicFuture<PoolEntry<T, C>>(callback) {
@Override
public synchronized PoolEntry<T, C> get(final long timeout, final TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
try {
return super.get(timeout, unit);
} catch (final TimeoutException ex) {
cancel();
throw ex;
}
}
};
final boolean acquiredLock;
try {
acquiredLock = this.lock.tryLock(requestTimeout.getDuration(), requestTimeout.getTimeUnit());
} catch (final InterruptedException interruptedException) {
Thread.currentThread().interrupt();
future.cancel();
return future;
}
if (acquiredLock) {
try {
final LeaseRequest<T, C> request = new LeaseRequest<>(route, state, requestTimeout, future);
final boolean completed = processPendingRequest(request);
if (!request.isDone() && !completed) {
this.pendingRequests.add(request);
}
if (request.isDone()) {
this.completedRequests.add(request);
}
} finally {
this.lock.unlock();
}
fireCallbacks();
} else {
future.failed(DeadlineTimeoutException.from(deadline));
}
return future;
}
Aggregations