You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by Markus Feindler <Ma...@gmx.de> on 2009/01/18 02:12:33 UTC

Automatically reestablish connection - two nio processors problem

Hello,

I'm right now trying to reestablish connection, after the session has 
been closed.
Whenever the session is closed, I reestablish it in the session 
listener: sessionDestroyed.

The "problem" is, I see two NioProcessor threads in debug 
mode(NioProcessor-1, Nio-Processor-2,pool-2-thread1), when the first 
session has been destroyed and the second one established. Further 
establishments dont create new NioProcessors.
Im testing by connecting to a telnet server and after connection is set 
up, I close it. I also tested by connecting to a self written server 
that doenst accept strings and then closes connection session.

If you have other suggestions how to handle connection reestablishment 
just tell me.

Here is my code:

Apache Mina 2.0.0M4
____________________________
package de.pred2k3.comssh.client;

import java.net.InetSocketAddress;

import org.apache.mina.core.filterchain.IoFilter;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.service.IoService;
import org.apache.mina.core.service.IoServiceListener;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.filter.logging.MdcInjectionFilter;
import org.apache.mina.transport.socket.nio.NioSocketConnector;

public class Nioclient {
    public NioSocketConnector connector;
    public IoSession session;
    public ConnectFuture future;
   
    public static void main(String[] args) throws Exception {
        Nioclient c1 = new Nioclient();
        c1.connect();
        c1.go();
    }
   
    public Nioclient(){
       
    }
   
    public void connect() throws InterruptedException{
        connector = new NioSocketConnector();
        connector.setHandler(new SSHClientHandler());
        connector.getFilterChain().addLast("logger", new LoggingFilter());
        connector.getFilterChain().addLast("codec", new 
ProtocolCodecFilter(new TextLineCodecFactory()));
        connector.addListener(new IoServiceListener(){

            @Override
            public void serviceActivated(IoService arg0) throws Exception {
                // TODO Auto-generated method stub
               
            }

            @Override
            public void serviceDeactivated(IoService arg0) throws 
Exception {
                // TODO Auto-generated method stub
               
            }

            @Override
            public void serviceIdle(IoService arg0, IdleStatus arg1)
                    throws Exception {
                // TODO Auto-generated method stub
               
            }

            @Override
            public void sessionCreated(IoSession arg0) throws Exception {
                // TODO Auto-generated method stub
            }

            @Override
            public void sessionDestroyed(IoSession arg0) throws Exception {
                //connector.dispose();
                Thread.sleep(3000);
                System.out.println("Connection closed - reconnect");
                go();
            }
           
        });
    }
   
   
    public void go() throws InterruptedException{
        future = connector.connect(new InetSocketAddress("192.168.1.1", 
23));
        future.awaitUninterruptibly();
        if (!future.isConnected()){
//            connector.dispose();
            System.out.println("Connection failed");
            Thread.sleep(3000);
            go();
        } else {
            session = future.getSession();
            System.out.println("Connection established!");
            session.write("hello");
            session.close(true);
        }
    }
   
}

___

Thanks for help!

Bye,
Markus

Re: Automatically reestablish connection - two nio processors problem

Posted by Markus Feindler <Ma...@gmx.de>.
Here it is:
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;

public class SSHClientHandler extends IoHandlerAdapter{
    @Override
    public void sessionOpened(IoSession session){
       
    }
   
    @Override
    public void sessionClosed(IoSession session){
       
    }
   
    @Override
    public void messageReceived(IoSession session, Object message){
        System.out.println("Message received: " + message.toString());
    }
}



> Markus Feindler wrote:
>> Hello,
> hi,
>
> sorry for the delay...
>
> I have looked at the code, but I can't run it without the 
> SSHClientHandler.java class ...
>
> Could you paste it ?
>
> Thanks !
>


Re: Automatically reestablish connection - two nio processors problem

Posted by Emmanuel Lecharny <el...@gmail.com>.
Markus Feindler wrote:
> Hello,
hi,

sorry for the delay...

I have looked at the code, but I can't run it without the 
SSHClientHandler.java class ...

Could you paste it ?

Thanks !

-- 
--
cordialement, regards,
Emmanuel Lécharny
www.iktek.com
directory.apache.org