Example usage for org.apache.http.nio.reactor SessionRequestCallback SessionRequestCallback

List of usage examples for org.apache.http.nio.reactor SessionRequestCallback SessionRequestCallback

Introduction

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

Prototype

SessionRequestCallback

Source Link

Usage

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//ww  w .  ja  va2 s . com
 * @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);
                }
            }
        }
    };
}