Hi,
 
  I am using Mina for building a server application. This application will act as a bridge  between the client and the service provider(a web service). When the client invokes a service for the first time I create a IoSession to the service provider and after servicing the client I store the session in the pool so that I dont need to recreate it and can improve the performance. But when I try to reuse the Session for the next client request for the same service it hangs. I wrote a sample program to explain this and it seems to be working fine for HTTP Get but it is not working working for HTTP post as the connection is reset by the service provider.
 
  Any pointers on this?
 
Regards,
Jana
package org.my.mina.examples;

import java.net.InetSocketAddress;

import org.apache.mina.common.ByteBuffer;
import org.apache.mina.common.ConnectFuture;
import org.apache.mina.common.IoHandlerAdapter;
import org.apache.mina.common.IoSession;
import org.apache.mina.transport.socket.nio.SocketConnector;

public class SessionReuseIssue {

        public static void main(String args[]) throws Exception {
                successGet();
                failPost();
                System.exit(0);
        }

        public static void successGet() throws Exception {
                System.out.println("------------->successGet<-------------");
                SocketConnector connector = new SocketConnector();
                InetSocketAddress address = new 
InetSocketAddress("www.google.com", 80);
                ConnectFuture future = connector.connect(address, new 
ConnectHandler());
                future.join(100000);
                IoSession session = future.getSession();
                session.setWriteTimeout(50000);
                String httpHeader = "GET / HTTP/1.1  \n"
                                + "User-Agent: Jakarta 
Commons-HttpnClient/3.1-alpha1 \n"
                                + "Host: junglebook:8090 \n" + "Connection: 
keep-alive \n"
                                + "\n";
                session.write(ByteBuffer.wrap(httpHeader.getBytes()));
                System.out.println("Sleeping");
                Thread.sleep(5000);
                System.out.println("Waking");
                session.write(ByteBuffer.wrap(httpHeader.getBytes()));
                System.out.println("Sleeping");
                Thread.sleep(5000);
                System.out.println("Waking");
        }

        public static void failPost() throws Exception {
                System.out.println("------------->failPost<-------------");
                SocketConnector connector = new SocketConnector();
                InetSocketAddress address = new 
InetSocketAddress("www.google.com", 80);
                ConnectFuture future = connector.connect(address, new 
ConnectHandler());
                future.join(100000);
                IoSession session = future.getSession();
                session.setWriteTimeout(50000);
                String httpHeader = "POST / HTTP/1.1  \n"
                                + "User-Agent: Jakarta 
Commons-HttpnClient/3.1-alpha1 \n"
                                + "Host: junglebook:8090 \n" + "Connection: 
keep-alive \n"
                                + "\n" + "Content-Length: 5 " + "12345";
                session.write(ByteBuffer.wrap(httpHeader.getBytes()));
                System.out.println("Sleeping");
                Thread.sleep(5000);
                System.out.println("Waking");
                session.write(ByteBuffer.wrap(httpHeader.getBytes()));
                System.out.println("Sleeping");
                Thread.sleep(5000);
                System.out.println("Waking");
        }

}

class ConnectHandler extends IoHandlerAdapter {

        @Override
        public void exceptionCaught(IoSession arg0, Throwable arg1)
                        throws Exception {
                super.exceptionCaught(arg0, arg1);
                System.out.println(arg1.toString());
        }

        @Override
        public void messageReceived(IoSession arg0, Object arg1) throws 
Exception {
                super.messageReceived(arg0, arg1);
                System.out.println("messageReceived");
        }

        @Override
        public void messageSent(IoSession arg0, Object arg1) throws Exception {
                super.messageSent(arg0, arg1);
                System.out.println("messageSent");
        }

        @Override
        public void sessionClosed(IoSession arg0) throws Exception {
                super.sessionClosed(arg0);
        }

        @Override
        public void sessionCreated(IoSession arg0) throws Exception {
                super.sessionCreated(arg0);
                System.out.println("session created");
        }

}

Reply via email to