Search in sources :

Example 6 with Callback

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;
}
Also used : AsyncClientExchangeHandler(org.apache.hc.core5.http.nio.AsyncClientExchangeHandler) BasicClientExchangeHandler(org.apache.hc.core5.http.nio.support.BasicClientExchangeHandler) ComplexFuture(org.apache.hc.core5.concurrent.ComplexFuture)

Example 7 with Callback

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);
    }
}
Also used : IOException(java.io.IOException) ClientHttp1IOEventHandler(org.apache.hc.core5.http.impl.nio.ClientHttp1IOEventHandler)

Example 8 with Callback

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;
}
Also used : AsyncClientExchangeHandler(org.apache.hc.core5.http.nio.AsyncClientExchangeHandler) BasicFuture(org.apache.hc.core5.concurrent.BasicFuture) BasicClientExchangeHandler(org.apache.hc.core5.http.nio.support.BasicClientExchangeHandler)

Example 9 with Callback

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());
    }
}
Also used : ContentType(org.apache.hc.core5.http.ContentType) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 10 with Callback

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;
}
Also used : Deadline(org.apache.hc.core5.util.Deadline) BasicFuture(org.apache.hc.core5.concurrent.BasicFuture) TimeUnit(java.util.concurrent.TimeUnit) TimeoutException(java.util.concurrent.TimeoutException) DeadlineTimeoutException(org.apache.hc.core5.util.DeadlineTimeoutException)

Aggregations

Test (org.junit.jupiter.api.Test)10 IOException (java.io.IOException)6 FutureCallback (org.apache.hc.core5.concurrent.FutureCallback)6 CancellationException (java.util.concurrent.CancellationException)5 TimeoutException (java.util.concurrent.TimeoutException)5 HttpResponse (org.apache.hc.core5.http.HttpResponse)5 ExecutionException (java.util.concurrent.ExecutionException)4 ClassicHttpResponse (org.apache.hc.core5.http.ClassicHttpResponse)4 StringEntity (org.apache.hc.core5.http.io.entity.StringEntity)4 TimeoutValueException (org.apache.hc.core5.util.TimeoutValueException)4 Stopwatch (com.google.common.base.Stopwatch)3 SimpleHttpRequest (org.apache.hc.client5.http.async.methods.SimpleHttpRequest)3 SimpleHttpResponse (org.apache.hc.client5.http.async.methods.SimpleHttpResponse)3 ContentType (org.apache.hc.core5.http.ContentType)3 BasicHeader (org.apache.hc.core5.http.message.BasicHeader)3 BasicHttpResponse (org.apache.hc.core5.http.message.BasicHttpResponse)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)2 HttpGet (org.apache.hc.client5.http.classic.methods.HttpGet)2 HttpPost (org.apache.hc.client5.http.classic.methods.HttpPost)2