Example usage for org.apache.http.nio.reactor SessionRequest isCompleted

List of usage examples for org.apache.http.nio.reactor SessionRequest isCompleted

Introduction

In this page you can find the example usage for org.apache.http.nio.reactor SessionRequest isCompleted.

Prototype

boolean isCompleted();

Source Link

Document

Determines whether the request has been completed (either successfully or unsuccessfully).

Usage

From source file:com.alibaba.openapi.client.rpc.AlibabaClientReactor.java

public <T> Future<T> send(Request request, Class<T> resultType, ClientPolicy clientPolicy,
        final RequestPolicy requestPolicy, FutureCallback<T> callback) {
    final InvokeContext context = new InvokeContext();
    context.setRequest(request);//from  ww w .  jav a  2 s  .c o m
    context.setPolicy(requestPolicy);
    context.setCallback(callback);
    context.setResultType(resultType);
    int serverPort = requestPolicy.isUseHttps() ? clientPolicy.getHttpsPort() : clientPolicy.getHttpPort();
    LoggerHelper.getClientLogger().finer(
            "Use " + (clientPolicy.isUseHttps() ? "https" : "http") + " connect and create SessionRequest");
    //SessionRequestCallback??
    //SessionRequest??
    final SessionRequest req = ioReactor.connect(
            new InetSocketAddress(clientPolicy.getServerHost(), serverPort), null, context,
            new SessionRequestCallbackTrigger());

    return new Future<T>() {
        private boolean cancelled = false;

        public boolean cancel(boolean mayInterruptIfRunning) {
            if (req.isCompleted() || cancelled) {
                return false;
            }
            cancelled = true;
            req.cancel();
            context.completed();
            return true;
        }

        public boolean isCancelled() {
            return cancelled;
        }

        public boolean isDone() {
            return context.isCompleted() || cancelled;
        }

        public T get() throws InterruptedException, ExecutionException {
            context.waitForComplete();
            return _get();
        }

        public T get(long timeout, TimeUnit unit)
                throws InterruptedException, ExecutionException, TimeoutException {
            context.waitForComplete(timeout, unit);
            return _get();
        }

        @SuppressWarnings("unchecked")
        private T _get() throws ExecutionException {
            Response response = ((InvokeContext) req.getAttachment()).getResponse();
            Throwable targetException = response.getException();
            if (targetException != null) {
                if (requestPolicy.getErrorHandler() != null && targetException instanceof OceanException) {
                    requestPolicy.getErrorHandler().handle((OceanException) targetException);
                }
                throw new ExecutionException(targetException.getMessage(), targetException);
            }
            return (T) response.getResult();
        }
    };
}