Simplified signature of #execute methods that take AsyncClientExchangeHandler as a parameter
This commit is contained in:
parent
70ee2d4912
commit
feb6a5fbdc
|
@ -29,9 +29,10 @@ package org.apache.hc.client5.http.impl.async;
|
|||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
|
||||
import org.apache.hc.client5.http.protocol.HttpClientContext;
|
||||
import org.apache.hc.core5.concurrent.Cancellable;
|
||||
import org.apache.hc.core5.concurrent.ComplexFuture;
|
||||
import org.apache.hc.core5.concurrent.FutureCallback;
|
||||
import org.apache.hc.core5.function.Supplier;
|
||||
import org.apache.hc.core5.http.nio.AsyncClientExchangeHandler;
|
||||
import org.apache.hc.core5.http.nio.AsyncRequestProducer;
|
||||
import org.apache.hc.core5.http.nio.AsyncResponseConsumer;
|
||||
|
@ -55,41 +56,34 @@ abstract class AbstractMinimalHttpAsyncClientBase extends AbstractHttpAsyncClien
|
|||
final HttpContext context,
|
||||
final FutureCallback<T> callback) {
|
||||
final ComplexFuture<T> future = new ComplexFuture<>(callback);
|
||||
execute(new BasicClientExchangeHandler<>(
|
||||
requestProducer,
|
||||
responseConsumer,
|
||||
callback),
|
||||
context, future, new Supplier<T>() {
|
||||
future.setDependency(execute(new BasicClientExchangeHandler<>(
|
||||
requestProducer,
|
||||
responseConsumer,
|
||||
new FutureCallback<T>() {
|
||||
|
||||
@Override
|
||||
public T get() {
|
||||
return responseConsumer.getResult();
|
||||
public void completed(final T result) {
|
||||
future.completed(result);
|
||||
}
|
||||
|
||||
});
|
||||
@Override
|
||||
public void failed(final Exception ex) {
|
||||
future.failed(ex);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancelled() {
|
||||
future.cancel();
|
||||
}
|
||||
|
||||
})));
|
||||
return future;
|
||||
}
|
||||
|
||||
public final <T extends AsyncClientExchangeHandler> Future<T> execute(
|
||||
final T exchangeHandler,
|
||||
final HttpContext context,
|
||||
final FutureCallback<T> callback) {
|
||||
final ComplexFuture<T> future = new ComplexFuture<>(callback);
|
||||
execute(exchangeHandler, context, future, new Supplier<T>() {
|
||||
|
||||
@Override
|
||||
public T get() {
|
||||
return exchangeHandler;
|
||||
}
|
||||
|
||||
});
|
||||
return future;
|
||||
public final Cancellable execute(final AsyncClientExchangeHandler exchangeHandler) {
|
||||
return execute(exchangeHandler, HttpClientContext.create());
|
||||
}
|
||||
|
||||
abstract <T> void execute(
|
||||
AsyncClientExchangeHandler exchangeHandler,
|
||||
HttpContext context,
|
||||
ComplexFuture<T> resultFuture,
|
||||
Supplier<T> resultSupplier);
|
||||
public abstract Cancellable execute(AsyncClientExchangeHandler exchangeHandler, HttpContext context);
|
||||
|
||||
}
|
||||
|
|
|
@ -41,11 +41,11 @@ import org.apache.hc.client5.http.impl.ExecSupport;
|
|||
import org.apache.hc.client5.http.impl.classic.RequestFailedException;
|
||||
import org.apache.hc.client5.http.impl.nio.MultuhomeConnectionInitiator;
|
||||
import org.apache.hc.client5.http.protocol.HttpClientContext;
|
||||
import org.apache.hc.core5.concurrent.ComplexFuture;
|
||||
import org.apache.hc.core5.concurrent.Cancellable;
|
||||
import org.apache.hc.core5.concurrent.ComplexCancellable;
|
||||
import org.apache.hc.core5.concurrent.FutureCallback;
|
||||
import org.apache.hc.core5.function.Callback;
|
||||
import org.apache.hc.core5.function.Resolver;
|
||||
import org.apache.hc.core5.function.Supplier;
|
||||
import org.apache.hc.core5.http.EntityDetails;
|
||||
import org.apache.hc.core5.http.Header;
|
||||
import org.apache.hc.core5.http.HttpException;
|
||||
|
@ -110,12 +110,11 @@ public final class MinimalHttp2AsyncClient extends AbstractMinimalHttpAsyncClien
|
|||
}
|
||||
|
||||
@Override
|
||||
<T> void execute(
|
||||
public Cancellable execute(
|
||||
final AsyncClientExchangeHandler exchangeHandler,
|
||||
final HttpContext context,
|
||||
final ComplexFuture<T> resultFuture,
|
||||
final Supplier<T> resultSupplier) {
|
||||
final HttpContext context) {
|
||||
ensureRunning();
|
||||
final ComplexCancellable cancellable = new ComplexCancellable();
|
||||
final HttpClientContext clientContext = HttpClientContext.adapt(context);
|
||||
try {
|
||||
exchangeHandler.produceRequest(new RequestChannel() {
|
||||
|
@ -149,11 +148,7 @@ public final class MinimalHttp2AsyncClient extends AbstractMinimalHttpAsyncClien
|
|||
|
||||
@Override
|
||||
public void failed(final Exception cause) {
|
||||
try {
|
||||
exchangeHandler.failed(cause);
|
||||
} finally {
|
||||
resultFuture.failed(cause);
|
||||
}
|
||||
exchangeHandler.failed(cause);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -187,9 +182,6 @@ public final class MinimalHttp2AsyncClient extends AbstractMinimalHttpAsyncClien
|
|||
public void consumeResponse(
|
||||
final HttpResponse response, final EntityDetails entityDetails) throws HttpException, IOException {
|
||||
exchangeHandler.consumeResponse(response, entityDetails);
|
||||
if (entityDetails == null) {
|
||||
resultFuture.completed(resultSupplier.get());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -205,7 +197,6 @@ public final class MinimalHttp2AsyncClient extends AbstractMinimalHttpAsyncClien
|
|||
@Override
|
||||
public void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException {
|
||||
exchangeHandler.streamEnd(trailers);
|
||||
resultFuture.completed(resultSupplier.get());
|
||||
}
|
||||
|
||||
};
|
||||
|
@ -214,9 +205,9 @@ public final class MinimalHttp2AsyncClient extends AbstractMinimalHttpAsyncClien
|
|||
log.debug(ConnPoolSupport.getId(session) + ": executing message exchange " + exchangeId);
|
||||
session.addLast(new ExecutionCommand(
|
||||
new LoggingAsyncClientExchangeHandler(log, exchangeId, internalExchangeHandler),
|
||||
resultFuture, clientContext));
|
||||
cancellable, clientContext));
|
||||
} else {
|
||||
session.addLast(new ExecutionCommand(internalExchangeHandler, resultFuture, clientContext));
|
||||
session.addLast(new ExecutionCommand(internalExchangeHandler, cancellable, clientContext));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -231,19 +222,21 @@ public final class MinimalHttp2AsyncClient extends AbstractMinimalHttpAsyncClien
|
|||
}
|
||||
|
||||
});
|
||||
if (resultFuture != null) {
|
||||
resultFuture.setDependency(sessionFuture);
|
||||
}
|
||||
cancellable.setDependency(new Cancellable() {
|
||||
|
||||
@Override
|
||||
public boolean cancel() {
|
||||
return sessionFuture.cancel(true);
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
});
|
||||
} catch (final HttpException | IOException ex) {
|
||||
try {
|
||||
exchangeHandler.failed(ex);
|
||||
} finally {
|
||||
resultFuture.failed(ex);
|
||||
}
|
||||
exchangeHandler.failed(ex);
|
||||
}
|
||||
return cancellable;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -47,10 +47,11 @@ import org.apache.hc.client5.http.nio.AsyncConnectionEndpoint;
|
|||
import org.apache.hc.client5.http.protocol.HttpClientContext;
|
||||
import org.apache.hc.client5.http.routing.RoutingSupport;
|
||||
import org.apache.hc.core5.concurrent.BasicFuture;
|
||||
import org.apache.hc.core5.concurrent.Cancellable;
|
||||
import org.apache.hc.core5.concurrent.ComplexCancellable;
|
||||
import org.apache.hc.core5.concurrent.ComplexFuture;
|
||||
import org.apache.hc.core5.concurrent.FutureCallback;
|
||||
import org.apache.hc.core5.function.Callback;
|
||||
import org.apache.hc.core5.function.Supplier;
|
||||
import org.apache.hc.core5.http.EntityDetails;
|
||||
import org.apache.hc.core5.http.Header;
|
||||
import org.apache.hc.core5.http.HttpException;
|
||||
|
@ -209,12 +210,11 @@ public final class MinimalHttpAsyncClient extends AbstractMinimalHttpAsyncClient
|
|||
}
|
||||
|
||||
@Override
|
||||
<T> void execute(
|
||||
public Cancellable execute(
|
||||
final AsyncClientExchangeHandler exchangeHandler,
|
||||
final HttpContext context,
|
||||
final ComplexFuture<T> resultFuture,
|
||||
final Supplier<T> resultSupplier) {
|
||||
final HttpContext context) {
|
||||
ensureRunning();
|
||||
final ComplexCancellable cancellable = new ComplexCancellable();
|
||||
final HttpClientContext clientContext = HttpClientContext.adapt(context);
|
||||
try {
|
||||
exchangeHandler.produceRequest(new RequestChannel() {
|
||||
|
@ -331,7 +331,6 @@ public final class MinimalHttpAsyncClient extends AbstractMinimalHttpAsyncClient
|
|||
if (messageCountDown.decrementAndGet() <= 0) {
|
||||
endpoint.releaseAndReuse();
|
||||
}
|
||||
resultFuture.completed(resultSupplier.get());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -351,7 +350,6 @@ public final class MinimalHttpAsyncClient extends AbstractMinimalHttpAsyncClient
|
|||
endpoint.releaseAndReuse();
|
||||
}
|
||||
exchangeHandler.streamEnd(trailers);
|
||||
resultFuture.completed(resultSupplier.get());
|
||||
}
|
||||
|
||||
};
|
||||
|
@ -360,28 +358,31 @@ public final class MinimalHttpAsyncClient extends AbstractMinimalHttpAsyncClient
|
|||
|
||||
@Override
|
||||
public void failed(final Exception ex) {
|
||||
resultFuture.failed(ex);
|
||||
exchangeHandler.failed(ex);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancelled() {
|
||||
resultFuture.cancel();
|
||||
exchangeHandler.cancel();
|
||||
}
|
||||
|
||||
});
|
||||
if (resultFuture != null) {
|
||||
resultFuture.setDependency(leaseFuture);
|
||||
}
|
||||
|
||||
cancellable.setDependency(new Cancellable() {
|
||||
|
||||
@Override
|
||||
public boolean cancel() {
|
||||
return leaseFuture.cancel(true);
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
} catch (final HttpException | IOException ex) {
|
||||
try {
|
||||
exchangeHandler.failed(ex);
|
||||
} finally {
|
||||
resultFuture.failed(ex);
|
||||
}
|
||||
exchangeHandler.failed(ex);
|
||||
}
|
||||
return cancellable;
|
||||
}
|
||||
|
||||
private class InternalAsyncClientEndpoint extends AsyncClientEndpoint {
|
||||
|
|
Loading…
Reference in New Issue