KeepAliveFilter questions

View: New views
3 Messages — Rating Filter:   Alert me  

KeepAliveFilter questions

by Aleksandar Lazic-6 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Dear List,

what is 'better' the

KeepAliveFilter

or in the handler the sessionIdle() method?

I have try to use the keepalive filter but I wasn't very successful ;-(

### main
KeepAliveFilter filter = new KeepAliveFilter(new
CommendKeepAliveMessageFactory());
        filter.setForwardEvent(true);
               
// Create TCP/IP connector.
NioSocketConnector connector = new NioSocketConnector();
connector.getFilterChain().addFirst("logging", new LoggingFilter());
connector.getFilterChain().addLast("keep-alive", filter);
connector.setConnectTimeoutMillis(20);
connector.setHandler(new Commend2SNAPHandler());

ConnectFuture future = connector.connect(
                        new InetSocketAddress("10.10.10.199",17000));
   future.await();
   System.out.println("fu-isConn " + future.isConnected());
   if(future.isConnected()){
    IoSession sessin = future.getSession();
   }else{
     future.cancel();
     connector.dispose();
     System.exit(0);
   }
###

attached the CommendKeepAliveMessageFactory file.

Many thanks for help.

BR

Aleks
/**
 *
 */
package org.ala;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.keepalive.KeepAliveMessageFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author allazic
 *
 */
public class CommendKeepAliveMessageFactory implements KeepAliveMessageFactory {

        private final Logger log;
       
        public CommendKeepAliveMessageFactory() {
          super();
          log = LoggerFactory.getLogger(CommendKeepAliveMessageFactory.class);
          log.debug("CKAMF created "+ this.getClass().getName());
        }
        /* (non-Javadoc)
         * @see org.apache.mina.filter.keepalive.KeepAliveMessageFactory#getRequest(org.apache.mina.core.session.IoSession)
         */
        @Override
        public Object getRequest(IoSession session) {
               
                return null;
        }

        /* (non-Javadoc)
         * @see org.apache.mina.filter.keepalive.KeepAliveMessageFactory#getResponse(org.apache.mina.core.session.IoSession, java.lang.Object)
         */
        @Override
        public Object getResponse(IoSession session, Object request) {
                // TODO Auto-generated method stub
                return null;
        }

        /* (non-Javadoc)
         * @see org.apache.mina.filter.keepalive.KeepAliveMessageFactory#isRequest(org.apache.mina.core.session.IoSession, java.lang.Object)
         */
        @Override
        public boolean isRequest(IoSession session, Object message) {
                log.debug("<< is Request");
                if (message instanceof IoBuffer){
                        return CommendMessage.getIdleMessage() == (IoBuffer) message;
                }
                return false;
        }

        /* (non-Javadoc)
         * @see org.apache.mina.filter.keepalive.KeepAliveMessageFactory#isResponse(org.apache.mina.core.session.IoSession, java.lang.Object)
         */
        @Override
        public boolean isResponse(IoSession session, Object message) {
                log.debug("<< is Response");
                if (message instanceof IoBuffer){
                        return CommendMessage.getIdleMessage() == (IoBuffer) message;
                }
                return false;
        }

}

Re: KeepAliveFilter questions

by Ashish-24 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Can you elaborate a bit on the problem/issue you are facing?

thanks
ashish

On Sat, Oct 31, 2009 at 8:51 PM, Aleksandar Lazic <al-mina@...> wrote:

> Dear List,
>
> what is 'better' the
>
> KeepAliveFilter
>
> or in the handler the sessionIdle() method?
>
> I have try to use the keepalive filter but I wasn't very successful ;-(
>
> ### main
> KeepAliveFilter filter = new KeepAliveFilter(new
> CommendKeepAliveMessageFactory());
>        filter.setForwardEvent(true);
>
> // Create TCP/IP connector.
> NioSocketConnector connector = new NioSocketConnector();
> connector.getFilterChain().addFirst("logging", new LoggingFilter());
> connector.getFilterChain().addLast("keep-alive", filter);
> connector.setConnectTimeoutMillis(20);
> connector.setHandler(new Commend2SNAPHandler());
>
> ConnectFuture future = connector.connect(
>                       new InetSocketAddress("10.10.10.199",17000));
>  future.await();
>  System.out.println("fu-isConn " + future.isConnected());
>  if(future.isConnected()){
>   IoSession sessin = future.getSession();
>  }else{
>    future.cancel();
>    connector.dispose();
>    System.exit(0);
>  }
> ###
>
> attached the CommendKeepAliveMessageFactory file.
>
> Many thanks for help.
>
> BR
>
> Aleks



--
thanks
ashish

Blog: http://www.ashishpaliwal.com/blog
My Photo Galleries: http://www.pbase.com/ashishpaliwal

Re: KeepAliveFilter questions

by Aleksandar Lazic-6 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Mon 02.11.2009 18:33, Ashish wrote:
>Can you elaborate a bit on the problem/issue you are facing?

I was falling in the trap of

session.getConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);

and

KeepAliveFilter

After I have remove the setIdleTime in the handler the filter works.

Sorry for the rush.

BR

Aleks