org.apache.mina.filter.traffic
Class ReadThrottleFilter

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

public class ReadThrottleFilter
extends IoFilterAdapter

An IoFilter that throttles incoming traffic to prevent a unwanted OutOfMemoryError under heavy load.

This filter will automatically disable reads on an IoSession once the amount of the read data batched for that session in the ExecutorFilter reaches a defined threshold. It accomplishes this by adding one filter before the ExecutorFilter.

The size of the received data is calculated by MessageSizeEstimator. If you are using a transport whose envelope is not an IoBuffer, you could write your own MessageSizeEstimator for better traffic calculation. However, the DefaultMessageSizeEstimator will suffice in most cases.

It is recommended to add this filter at the end of your filter chain configuration because it is possible to subvert the behavior of the added filters by adding a filter immediately before/after the ExecutorFilter after inserting this builder, consequently leading to a unexpected behavior.

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
ReadThrottleFilter(ScheduledExecutorService executor)
          Creates a new instance with 64KB maxSessionBufferSize, 128MB maxGlobalBufferSize and a new DefaultMessageSizeEstimator.
ReadThrottleFilter(ScheduledExecutorService executor, int maxSessionBufferSize, int maxServiceBufferSize, int maxGlobalBufferSize)
          Creates a new instance with the specified maxSessionBufferSize, maxGlobalBufferSize and a new DefaultMessageSizeEstimator.
ReadThrottleFilter(ScheduledExecutorService executor, ReadThrottlePolicy policy)
           
ReadThrottleFilter(ScheduledExecutorService executor, ReadThrottlePolicy policy, int maxSessionBufferSize, int maxServiceBufferSize, int maxGlobalBufferSize)
           
ReadThrottleFilter(ScheduledExecutorService executor, ReadThrottlePolicy policy, MessageSizeEstimator messageSizeEstimator)
           
ReadThrottleFilter(ScheduledExecutorService executor, ReadThrottlePolicy policy, MessageSizeEstimator messageSizeEstimator, int maxSessionBufferSize, int maxServiceBufferSize, int maxGlobalBufferSize)
          Creates a new instance with the specified maxSessionBufferSize, maxGlobalBufferSize and MessageSizeEstimator.
 
Method Summary
 void filterSetTrafficMask(IoFilter.NextFilter nextFilter, IoSession session, TrafficMask trafficMask)
          Filters IoSession.setTrafficMask(TrafficMask) method invocation.
static int getGlobalBufferSize()
          Returns the current amount of data in the buffer of the ExecutorFilter for all IoSession whose IoFilterChain has been configured by this builder.
 int getMaxGlobalBufferSize()
          Returns the maximum amount of data in the buffer of the ExecutorFilter for all IoSession whose IoFilterChain has been configured by this builder.
 int getMaxServiceBufferSize()
           
 int getMaxSessionBufferSize()
          Returns the maximum amount of data in the buffer of the ExecutorFilter per IoSession.
 MessageSizeEstimator getMessageSizeEstimator()
          Returns the size estimator currently in use.
 ReadThrottlePolicy getPolicy()
           
static int getServiceBufferSize(IoService service)
           
 int getSessionBufferSize(IoSession session)
          Returns the current amount of data in the buffer of the ExecutorFilter for the specified IoSession.
 void messageReceived(IoFilter.NextFilter nextFilter, IoSession session, Object message)
          Filters IoHandler.messageReceived(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 setMaxGlobalBufferSize(int maxGlobalBufferSize)
          Sets the maximum amount of data in the buffer of the ExecutorFilter for all IoSession whose IoFilterChain has been configured by this builder.
 void setMaxServiceBufferSize(int maxServiceBufferSize)
           
 void setMaxSessionBufferSize(int maxSessionBufferSize)
          Sets the maximum amount of data in the buffer of the ExecutorFilter per IoSession.
 void setPolicy(ReadThrottlePolicy policy)
           
 String toString()
           
 
Methods inherited from class org.apache.mina.common.IoFilterAdapter
destroy, exceptionCaught, filterClose, filterWrite, init, messageSent, onPreRemove, sessionClosed, sessionCreated, sessionIdle, sessionOpened
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ReadThrottleFilter

public ReadThrottleFilter(ScheduledExecutorService executor)
Creates a new instance with 64KB maxSessionBufferSize, 128MB maxGlobalBufferSize and a new DefaultMessageSizeEstimator.


ReadThrottleFilter

public ReadThrottleFilter(ScheduledExecutorService executor,
                          ReadThrottlePolicy policy)

ReadThrottleFilter

public ReadThrottleFilter(ScheduledExecutorService executor,
                          ReadThrottlePolicy policy,
                          MessageSizeEstimator messageSizeEstimator)

ReadThrottleFilter

public ReadThrottleFilter(ScheduledExecutorService executor,
                          int maxSessionBufferSize,
                          int maxServiceBufferSize,
                          int maxGlobalBufferSize)
Creates a new instance with the specified maxSessionBufferSize, maxGlobalBufferSize and a new DefaultMessageSizeEstimator.


ReadThrottleFilter

public ReadThrottleFilter(ScheduledExecutorService executor,
                          ReadThrottlePolicy policy,
                          int maxSessionBufferSize,
                          int maxServiceBufferSize,
                          int maxGlobalBufferSize)

ReadThrottleFilter

public ReadThrottleFilter(ScheduledExecutorService executor,
                          ReadThrottlePolicy policy,
                          MessageSizeEstimator messageSizeEstimator,
                          int maxSessionBufferSize,
                          int maxServiceBufferSize,
                          int maxGlobalBufferSize)
Creates a new instance with the specified maxSessionBufferSize, maxGlobalBufferSize and MessageSizeEstimator.

Parameters:
maxSessionBufferSize - the maximum amount of data in the buffer of the ExecutorFilter per IoSession. Specify 0 or a smaller value to disable.
maxGlobalBufferSize - the maximum amount of data in the buffer of the ExecutorFilter for all IoSession whose IoFilterChain has been configured by this builder. Specify 0 or a smaller value to disable.
messageSizeEstimator - the message size estimator. If null, a new DefaultMessageSizeEstimator is created.
Method Detail

getGlobalBufferSize

public static int getGlobalBufferSize()
Returns the current amount of data in the buffer of the ExecutorFilter for all IoSession whose IoFilterChain has been configured by this builder.


getServiceBufferSize

public static int getServiceBufferSize(IoService service)

getPolicy

public ReadThrottlePolicy getPolicy()

setPolicy

public void setPolicy(ReadThrottlePolicy policy)

getMaxSessionBufferSize

public int getMaxSessionBufferSize()
Returns the maximum amount of data in the buffer of the ExecutorFilter per IoSession. 0 means 'disabled'.


getMaxServiceBufferSize

public int getMaxServiceBufferSize()

getMaxGlobalBufferSize

public int getMaxGlobalBufferSize()
Returns the maximum amount of data in the buffer of the ExecutorFilter for all IoSession whose IoFilterChain has been configured by this builder. 0 means 'disabled'.


setMaxSessionBufferSize

public void setMaxSessionBufferSize(int maxSessionBufferSize)
Sets the maximum amount of data in the buffer of the ExecutorFilter per IoSession. Specify 0 or a smaller value to disable.


setMaxServiceBufferSize

public void setMaxServiceBufferSize(int maxServiceBufferSize)

setMaxGlobalBufferSize

public void setMaxGlobalBufferSize(int maxGlobalBufferSize)
Sets the maximum amount of data in the buffer of the ExecutorFilter for all IoSession whose IoFilterChain has been configured by this builder. Specify 0 or a smaller value to disable.


getMessageSizeEstimator

public MessageSizeEstimator getMessageSizeEstimator()
Returns the size estimator currently in use.


getSessionBufferSize

public int getSessionBufferSize(IoSession session)
Returns the current amount of data in the buffer of the ExecutorFilter for the specified IoSession.


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

filterSetTrafficMask

public void filterSetTrafficMask(IoFilter.NextFilter nextFilter,
                                 IoSession session,
                                 TrafficMask trafficMask)
                          throws Exception
Description copied from interface: IoFilter
Filters IoSession.setTrafficMask(TrafficMask) method invocation.

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

toString

public String toString()
Overrides:
toString in class Object


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