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

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

Introduction

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

Prototype

void cancel();

Source Link

Document

Cancels the request.

Usage

From source file:org.apache.synapse.transport.passthru.ConnectCallback.java

public void timeout(SessionRequest request) {
    HostConnections pool = (HostConnections) request.getAttachment();
    pool.pendingConnectionFailed();/*  w ww .  j ava  2  s .  c o  m*/

    deliveryAgent.errorConnecting(pool.getRoute(), ErrorCodes.CONNECT_TIMEOUT, "Connection Timeout");

    handleError("Timeout connecting to : " + request.getRemoteAddress());
    request.cancel();
}

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  www  . j a  v  a2 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();
        }
    };
}

From source file:org.apache.synapse.transport.nhttp.HttpCoreNIOSender.java

/**
 * Return a SessionRequestCallback which gets notified of a connection failure
 * or an error during a send operation. This method finds the corresponding
 * Axis2 message context for the outgoing request, and find the message receiver
 * and sends a fault message back to the message receiver that is marked as
 * related to the outgoing request/* w w  w  .  j a v a  2  s  .  c  om*/
 * @return a Session request callback
 */
private SessionRequestCallback getSessionRequestCallback() {
    return new SessionRequestCallback() {
        public void completed(SessionRequest request) {
            if (log.isDebugEnabled() && request.getSession() != null
                    && request.getSession().getLocalAddress() != null) {
                log.debug("Connected to remote address : " + request.getSession().getRemoteAddress()
                        + " from local address : " + request.getSession().getLocalAddress());
            }
        }

        public void failed(SessionRequest request) {
            handleError(request, NhttpConstants.CONNECTION_FAILED,
                    "Connection refused or failed for : " + request.getRemoteAddress() + ", "
                            + "IO Exception occured : " + request.getException().getMessage());
        }

        public void timeout(SessionRequest request) {
            handleError(request, NhttpConstants.CONNECT_TIMEOUT,
                    "Timeout connecting to : " + request.getRemoteAddress());
            request.cancel();
        }

        public void cancelled(SessionRequest request) {
            handleError(request, NhttpConstants.CONNECT_CANCEL,
                    "Connection cancelled for : " + request.getRemoteAddress());
        }

        private void handleError(SessionRequest request, int errorCode, String errorMessage) {
            if (request.getAttachment() != null && request.getAttachment() instanceof Axis2HttpRequest) {

                Axis2HttpRequest axis2Request = (Axis2HttpRequest) request.getAttachment();
                if (!axis2Request.isCompleted()) {
                    handler.markRequestCompletedWithError(axis2Request, errorCode, errorMessage, null);
                }
            }
        }
    };
}

From source file:org.siddhiesb.transport.passthru.ConnectCallback.java

public void timeout(SessionRequest request) {
    HostConnections pool = (HostConnections) request.getAttachment();
    pool.pendingConnectionFailed();//from   ww w .  j a v a  2  s. c o  m

    deliveryAgent.errorConnecting(pool.getRoute(), org.siddhiesb.transport.passthru.ErrorCodes.CONNECT_TIMEOUT,
            "Connection Timeout");

    handleError("Timeout connecting to : " + request.getRemoteAddress());
    request.cancel();
}