Hi,

We have developed a proxy using Apache mina 1.1.7. There are multiple
clients which connect to this proxy and this proxy connects to one third
party system. The clients which connect to the proxy are of two types -
(a) Client1 - Establishes a connection on startup and doesnt give it up
until its closed.
(b) Client2 - Establishes and closes a connection per request.

 Once we deployed it to production, over a period of time(say 2-3 days) and
about 50 clients (1 &2), the proxy seems unable to connect to the third
party system on intermittent basis. We have had no issues with this proxy
system in test.

 This is the proxy code:
     public void start() {

        if (this.acceptor == null) {
            socketAcceptorConfig = new SocketAcceptorConfig();
            socketAcceptorConfig.setDisconnectOnUnbind( false );
            socketAcceptorConfig.setReuseAddress(true);
            this.acceptor = new
SocketAcceptor(Runtime.getRuntime().availableProcessors() + 1,
Executors.newCachedThreadPool());
            //IoServiceConfig acceptorConfig =
this.acceptor.getDefaultConfig();
            //acceptorConfig.setThreadModel(ThreadModel.MANUAL);
            clientHandler = new XXXXClientHandler(this,
thirdPartyServerConfig);
            socketAddress = new InetSocketAddress(this.proxyPort);
            try {
                acceptor.unbindAll();
                acceptor.bind(socketAddress,
clientHandler,socketAcceptorConfig);
            } catch (Exception ex) {
        }
    }


------------------------------------------------------------------------------------------
    public class XXXXClientHandler extends XXXXAbstractHandler implements
Constants {
    private final XXXXServerHandler XXXXServerHandler;
    private final IoConnector connector;
    private final InetSocketAddress address;


    public XXXXClientHandler(XXXXProxyServer parent, XXXXServerConfiguration
XXXXServerConfig) {
        this.connector = new SocketConnector();

((IoConnectorConfig)connector.getDefaultConfig()).setConnectTimeout(XXXXServerConfig.timeout);
        this.address = new InetSocketAddress(XXXXServerConfig.address,
Integer.parseInt(XXXXServerConfig.port));
        this.XXXXServerHandler = new XXXXServerHandler(parent);
        super.parent = parent;
    }

    public void setTimeout(int timeout) {

((IoConnectorConfig)connector.getDefaultConfig()).setConnectTimeout(timeout);
    }

    public int getTimeout() {
        return
((IoConnectorConfig)connector.getDefaultConfig()).getConnectTimeout();
    }

    public void sessionOpened(final IoSession session) throws Exception {
        ConnectFuture future = connector.connect(this.address,
this.XXXXServerHandler);
        XXXXFutureListener futureListener = new
XXXXFutureListener(session,parent);
        future.addListener(futureListener);
    }

    @Override
    public void messageReceived(IoSession session, Object message) throws
Exception {
        //Do stuff

    }

    @Override
    public void messageSent(IoSession session, Object message) throws
Exception {
        //Do stuff
    }

    @Override
    public void exceptionCaught(IoSession session, Throwable cause) throws
Exception {
        //Do stuff
    }
}
-------------------------------------
public class XXXXFutureListener implements IoFutureListener,Constants {
public void operationComplete(IoFuture f) {
        ConnectFuture future = (ConnectFuture) f;
        if(!future.isConnected()){
            //Server connection not made.
            if (this.session != null){
                this.session.close();
                this.session.setTrafficMask(TrafficMask.ALL);
            }
            logger.error("Session could not be created with third party! ");
        }else{
            //connection working.
            try {
                future.getSession().setAttachment(this.session);
                this.session.setAttachment(future.getSession());
                future.getSession().setTrafficMask(TrafficMask.ALL);
            }
            catch (RuntimeIOException ex) {
                if (this.session != null)
                    this.session.close();

                logger.error("Session counld not be created!", ex);
            }
            finally {
                if (this.session != null)
                    this.session.setTrafficMask(TrafficMask.ALL);
            }
        }


    }
}

----------------------------------

We end up getting this "Session could not be created with third party!"
quite frequently. I know this is pretty vague. I was hoping if anyone can
sense some corrections in the code. Or any improvements that can be done.
Also, is it mandatory to do "setThreadModel(ThreadModel.MANUAL);" if we have
multiple clients talking to the proxy.
If I just pass "Executors.newCachedThreadPool()" onto the SocketAcceptor
constructor-> this.acceptor = new
SocketAcceptor(Runtime.getRuntime().availableProcessors() + 1,
Executors.newCachedThreadPool());
should I be still setting a setThreadModel?

Could it be that since I dont do a setThreadModel, my proxy is unable to
connect to the third party system under load. I read the ThreadModel
documentation and also browsed the mina code. The mina code, seems to use
the ThreadedPoolExecutor if a threadModel is not set. So, I assumed not
doing a setThreadModel wouldnt hurt me.

Thanks and Regards,
Arthur.

Reply via email to