org.apache.mina.filter.keepalive
Class KeepAliveFilter

java.lang.Object
  extended by org.apache.mina.common.IoFilterAdapter
      extended by org.apache.mina.filter.keepalive.KeepAliveFilter
All Implemented Interfaces:
IoFilter

public class KeepAliveFilter
extends IoFilterAdapter

An IoFilter that sends a keep-alive request on sessionIdle event with IdleStatus.READER_IDLE and sends back the response for the keep-alive request.

Interference with IoSessionConfig.setIdleTime(IdleStatus, int)

This filter adjusts idleTime property for IdleStatus.READER_IDLE automatically. Changing the idleTime property for IdleStatus.READER_IDLE will lead this filter to a unexpected behavior.

Implementing KeepAliveMessageFactory

To use this filter, you have to provide an implementation of KeepAliveMessageFactory, which determines a received or sent message is a keep-alive message or not and creates a new keep-alive message:
NameDescriptionImplementation
Active You want a keep-alive request is sent when the reader is idle. Once the request is sent, the response for the request should be received within keepAliveRequestTimeout seconds. Otherwise, the specified KeepAlivePolicy will be enforced. If a keep-alive request is received, its response also should be sent back. Both KeepAliveMessageFactory.getRequest(IoSession) and KeepAliveMessageFactory.getResponse(IoSession, Object) must return a non-null.
Semi-active You want a keep-alive request to be sent when the reader is idle. However, you don't really care if the response is received or not. If a keep-alive request is received, its response should also be sent back. Both KeepAliveMessageFactory.getRequest(IoSession) and KeepAliveMessageFactory.getResponse(IoSession, Object) must return a non-null, and the policy property should be set to KeepAlivePolicy.OFF or KeepAlivePolicy.LOG.
Passive You don't want to send a keep-alive request by yourself, but the response should be sent back if a keep-alive request is received. KeepAliveMessageFactory.getRequest(IoSession) must return null and KeepAliveMessageFactory.getResponse(IoSession, Object) must return a non-null.
Deaf Speaker You want a keep-alive request to be sent when the reader is idle, but you don't want to send any response back. KeepAliveMessageFactory.getRequest(IoSession) must return a non-null and KeepAliveMessageFactory.getResponse(IoSession, Object) must return null.
Silent Listener You don't want to send a keep-alive request by yourself nor send any response back. Both KeepAliveMessageFactory.getRequest(IoSession) and KeepAliveMessageFactory.getResponse(IoSession, Object) must return null.
Please note that you must implement KeepAliveMessageFactory.isRequest(IoSession, Object) and KeepAliveMessageFactory.isResponse(IoSession, Object) properly whatever case you chose.

Enforcing a policy

You can enforce a predefined policy by specifying a KeepAlivePolicy. The default policy is KeepAlivePolicy.CLOSE. Setting the policy to KeepAlivePolicy.OFF stops this filter from waiting for response messages and therefore disables keepAliveRequestTimeout property.

Version:
$Rev: 616100 $, $Date: 2008-01-28 23:58:32 +0100 (Mon, 28 Jan 2008) $
Author:
The Apache MINA Project (dev@mina.apache.org)

Nested Class Summary
 
Nested classes/interfaces inherited from interface org.apache.mina.common.IoFilter
IoFilter.NextFilter
 
Constructor Summary
KeepAliveFilter(KeepAliveMessageFactory messageFactory)
          Creates a new instance with the default KeepAlivePolicy and the default timeout values (policy = KeepAlivePolicy.CLOSE, keepAliveRequestInterval = 60 and keepAliveRequestTimeout = 30).
KeepAliveFilter(KeepAliveMessageFactory messageFactory, KeepAlivePolicy policy)
          Creates a new instance with the default timeout values (keepAliveRequestInterval = 60 and keepAliveRequestTimeout = 30).
KeepAliveFilter(KeepAliveMessageFactory messageFactory, KeepAlivePolicy policy, int keepAliveRequestInterval, int keepAliveRequestTimeout)
          Creates a new instance.
 
Method Summary
 int getKeepAliveRequestInterval()
           
 int getKeepAliveRequestTimeout()
           
 KeepAliveMessageFactory getMessageFactory()
           
 KeepAlivePolicy getPolicy()
           
 void messageReceived(IoFilter.NextFilter nextFilter, IoSession session, Object message)
          Filters IoHandler.messageReceived(IoSession,Object) event.
 void messageSent(IoFilter.NextFilter nextFilter, IoSession session, WriteRequest writeRequest)
          Filters IoHandler.messageSent(IoSession,Object) event.
 void onPostAdd(IoFilterChain parent, String name, IoFilter.NextFilter nextFilter)
          Invoked after this filter is added to the specified parent.
 void onPostRemove(IoFilterChain parent, String name, IoFilter.NextFilter nextFilter)
          Invoked after this filter is removed from the specified parent.
 void onPreAdd(IoFilterChain parent, String name, IoFilter.NextFilter nextFilter)
          Invoked before this filter is added to the specified parent.
 void sessionIdle(IoFilter.NextFilter nextFilter, IoSession session, IdleStatus status)
          Filters IoHandler.sessionIdle(IoSession,IdleStatus) event.
 void setKeepAliveRequestInterval(int keepAliveRequestInterval)
           
 void setKeepAliveRequestTimeout(int keepAliveRequestTimeout)
           
 void setPolicy(KeepAlivePolicy policy)
           
 
Methods inherited from class org.apache.mina.common.IoFilterAdapter
destroy, exceptionCaught, filterClose, filterSetTrafficMask, filterWrite, init, onPreRemove, sessionClosed, sessionCreated, sessionOpened
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

KeepAliveFilter

public KeepAliveFilter(KeepAliveMessageFactory messageFactory)
Creates a new instance with the default KeepAlivePolicy and the default timeout values (policy = KeepAlivePolicy.CLOSE, keepAliveRequestInterval = 60 and keepAliveRequestTimeout = 30).


KeepAliveFilter

public KeepAliveFilter(KeepAliveMessageFactory messageFactory,
                       KeepAlivePolicy policy)
Creates a new instance with the default timeout values (keepAliveRequestInterval = 60 and keepAliveRequestTimeout = 30).


KeepAliveFilter

public KeepAliveFilter(KeepAliveMessageFactory messageFactory,
                       KeepAlivePolicy policy,
                       int keepAliveRequestInterval,
                       int keepAliveRequestTimeout)
Creates a new instance.

Method Detail

getPolicy

public KeepAlivePolicy getPolicy()

setPolicy

public void setPolicy(KeepAlivePolicy policy)

getKeepAliveRequestInterval

public int getKeepAliveRequestInterval()

setKeepAliveRequestInterval

public void setKeepAliveRequestInterval(int keepAliveRequestInterval)

getKeepAliveRequestTimeout

public int getKeepAliveRequestTimeout()

setKeepAliveRequestTimeout

public void setKeepAliveRequestTimeout(int keepAliveRequestTimeout)

getMessageFactory

public KeepAliveMessageFactory getMessageFactory()

onPreAdd

public void onPreAdd(IoFilterChain parent,
                     String name,
                     IoFilter.NextFilter nextFilter)
              throws Exception
Description copied from interface: IoFilter
Invoked before this filter is added to the specified parent. Please note that this method can be invoked more than once if this filter is added to more than one parents. This method is not invoked before IoFilter.init() is invoked.

Specified by:
onPreAdd in interface IoFilter
Overrides:
onPreAdd in class IoFilterAdapter
Parameters:
parent - the parent who called this method
name - the name assigned to this filter
nextFilter - the IoFilter.NextFilter for this filter. You can reuse this object until this filter is removed from the chain.
Throws:
Exception

onPostAdd

public void onPostAdd(IoFilterChain parent,
                      String name,
                      IoFilter.NextFilter nextFilter)
               throws Exception
Description copied from interface: IoFilter
Invoked after this filter is added to the specified parent. Please note that this method can be invoked more than once if this filter is added to more than one parents. This method is not invoked before IoFilter.init() is invoked.

Specified by:
onPostAdd in interface IoFilter
Overrides:
onPostAdd in class IoFilterAdapter
Parameters:
parent - the parent who called this method
name - the name assigned to this filter
nextFilter - the IoFilter.NextFilter for this filter. You can reuse this object until this filter is removed from the chain.
Throws:
Exception

onPostRemove

public void onPostRemove(IoFilterChain parent,
                         String name,
                         IoFilter.NextFilter nextFilter)
                  throws Exception
Description copied from interface: IoFilter
Invoked after this filter is removed from the specified parent. Please note that this method can be invoked more than once if this filter is removed from more than one parents. This method is always invoked before IoFilter.destroy() is invoked.

Specified by:
onPostRemove in interface IoFilter
Overrides:
onPostRemove in class IoFilterAdapter
Parameters:
parent - the parent who called this method
name - the name assigned to this filter
nextFilter - the IoFilter.NextFilter for this filter. You can reuse this object until this filter is removed from the chain.
Throws:
Exception

messageReceived

public void messageReceived(IoFilter.NextFilter nextFilter,
                            IoSession session,
                            Object message)
                     throws Exception
Description copied from interface: IoFilter
Filters IoHandler.messageReceived(IoSession,Object) event.

Specified by:
messageReceived in interface IoFilter
Overrides:
messageReceived in class IoFilterAdapter
Throws:
Exception

messageSent

public void messageSent(IoFilter.NextFilter nextFilter,
                        IoSession session,
                        WriteRequest writeRequest)
                 throws Exception
Description copied from interface: IoFilter
Filters IoHandler.messageSent(IoSession,Object) event.

Specified by:
messageSent in interface IoFilter
Overrides:
messageSent in class IoFilterAdapter
Throws:
Exception

sessionIdle

public void sessionIdle(IoFilter.NextFilter nextFilter,
                        IoSession session,
                        IdleStatus status)
                 throws Exception
Description copied from interface: IoFilter
Filters IoHandler.sessionIdle(IoSession,IdleStatus) event.

Specified by:
sessionIdle in interface IoFilter
Overrides:
sessionIdle in class IoFilterAdapter
Throws:
Exception


Copyright © 2004-2009 Apache MINA Project. All Rights Reserved.