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

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

Introduction

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

Prototype

IOSession getSession();

Source Link

Document

Returns IOSession instance created as a result of this request or null if the request is still pending.

Usage

From source file:com.ok2c.lightmtp.examples.LocalMailClientTransportExample.java

public static void main(final String[] args) throws Exception {

    String text1 = "From: root\r\n" + "To: testuser1\r\n" + "Subject: test message 1\r\n" + "\r\n"
            + "This is a short test message 1\r\n";
    String text2 = "From: root\r\n" + "To: testuser1, testuser2\r\n" + "Subject: test message 2\r\n" + "\r\n"
            + "This is a short test message 2\r\n";
    String text3 = "From: root\r\n" + "To: testuser1, testuser2, testuser3\r\n" + "Subject: test message 3\r\n"
            + "\r\n" + "This is a short test message 3\r\n";

    DeliveryRequest request1 = new BasicDeliveryRequest("root", Arrays.asList("testuser1"),
            new ByteArraySource(text1.getBytes("US-ASCII")));

    DeliveryRequest request2 = new BasicDeliveryRequest("root", Arrays.asList("testuser1", "testuser2"),
            new ByteArraySource(text2.getBytes("US-ASCII")));

    DeliveryRequest request3 = new BasicDeliveryRequest("root",
            Arrays.asList("testuser1", "testuser2", "testuser3"),
            new ByteArraySource(text3.getBytes("US-ASCII")));

    Queue<DeliveryRequest> queue = new ConcurrentLinkedQueue<DeliveryRequest>();
    queue.add(request1);/*from   www. ja  va2  s. c o  m*/
    queue.add(request2);
    queue.add(request3);

    CountDownLatch messageCount = new CountDownLatch(queue.size());

    IOReactorConfig config = IOReactorConfig.custom().setIoThreadCount(1).build();

    MailClientTransport mua = new LocalMailClientTransport(config);
    mua.start(new MyDeliveryRequestHandler(messageCount));

    SessionEndpoint endpoint = new SessionEndpoint(new InetSocketAddress("localhost", 2525));

    SessionRequest sessionRequest = mua.connect(endpoint, queue, null);
    sessionRequest.waitFor();

    IOSession iosession = sessionRequest.getSession();
    if (iosession != null) {
        messageCount.await();
    } else {
        IOException ex = sessionRequest.getException();
        if (ex != null) {
            System.out.println("Connection failed: " + ex.getMessage());
        }
    }

    System.out.println("Shutting down I/O reactor");
    try {
        mua.shutdown();
    } catch (IOException ex) {
        mua.forceShutdown();
    }
    System.out.println("Done");
}

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

public void completed(SessionRequest request) {
    HostConnections pool = (HostConnections) request.getAttachment();
    pool.pendingConnectionSucceeded();/*from   w w  w.  ja  v  a2 s.co  m*/
    if (log.isDebugEnabled()) {
        if (request.getSession() != null && request.getSession().getLocalAddress() != null) {
            log.debug("Connected to remote address: " + request.getSession().getRemoteAddress()
                    + " from local address: " + request.getSession().getLocalAddress());
        }
    }
}

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  a2s . c  o m*/
 * @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);
                }
            }
        }
    };
}