org.apache.mina.filter
Class ThreadPoolFilter

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

public class ThreadPoolFilter
extends IoFilterAdapter

A Thread-pooling filter. This filter forwards IoHandler events to its thread pool.

This is an implementation of Leader/Followers thread pool by Douglas C. Schmidt et al.

Use the start() and stop() methods to force this filter to start/stop processing events. Alternatively, start() will be called automatically the first time an instance of this filter is added to a filter chain. Calling stop() is not required either since all workers are daemon threads which means that any workers still alive when the JVM terminates will die automatically.

Version:
$Rev: 397284 $, $Date: 2006-04-27 05:09:11 +0900 (Thu, 27 Apr 2006) $
Author:
The Apache Directory Project (mina-dev@directory.apache.org)

Nested Class Summary
protected static class ThreadPoolFilter.Event
           
protected static class ThreadPoolFilter.EventType
           
protected static class ThreadPoolFilter.SessionBuffer
           
 
Nested classes/interfaces inherited from interface org.apache.mina.common.IoFilter
IoFilter.NextFilter, IoFilter.WriteRequest
 
Field Summary
static int DEFAULT_KEEP_ALIVE_TIME
          Default keep-alive time of thread pool (1 min).
static int DEFAULT_MAXIMUM_POOL_SIZE
          Default maximum size of thread pool (16).
 
Constructor Summary
ThreadPoolFilter()
          Creates a new instance of this filter with default thread pool settings.
ThreadPoolFilter(String threadNamePrefix)
          Creates a new instance of this filter with the specified thread name prefix and other default settings.
ThreadPoolFilter(String threadNamePrefix, int maximumPoolSize)
          Creates a new instance of this filter with the specified thread name prefix and other default settings.
 
Method Summary
 void exceptionCaught(IoFilter.NextFilter nextFilter, IoSession session, Throwable cause)
          Filters IoHandler.exceptionCaught(IoSession,Throwable) event.
protected  ThreadPoolFilter.SessionBuffer fetchSessionBuffer(Queue unfetchedSessionBuffers)
          Implement this method to fetch (or pop) a ThreadPoolFilter.SessionBuffer from the given unfetchedSessionBuffers.
 void filterClose(IoFilter.NextFilter nextFilter, IoSession session)
          Filters IoSession.close() method invocation.
 void filterWrite(IoFilter.NextFilter nextFilter, IoSession session, IoFilter.WriteRequest writeRequest)
          Filters IoSession.write(Object) method invocation.
 int getKeepAliveTime()
           
 int getMaximumPoolSize()
           
 int getPoolSize()
           
 String getThreadNamePrefix()
           
 void messageReceived(IoFilter.NextFilter nextFilter, IoSession session, Object message)
          Filters IoHandler.messageReceived(IoSession,Object) event.
 void messageSent(IoFilter.NextFilter nextFilter, IoSession session, Object message)
          Filters IoHandler.messageSent(IoSession,Object) event.
 void onPreAdd(IoFilterChain parent, String name, IoFilter.NextFilter nextFilter)
          Invoked before this filter is added to the specified parent.
protected  void processEvent(IoFilter.NextFilter nextFilter, IoSession session, ThreadPoolFilter.EventType type, Object data)
           
 void sessionClosed(IoFilter.NextFilter nextFilter, IoSession session)
          Filters IoHandler.sessionClosed(IoSession) event.
 void sessionCreated(IoFilter.NextFilter nextFilter, IoSession session)
          Filters IoHandler.sessionCreated(IoSession) event.
 void sessionIdle(IoFilter.NextFilter nextFilter, IoSession session, IdleStatus status)
          Filters IoHandler.sessionIdle(IoSession,IdleStatus) event.
 void sessionOpened(IoFilter.NextFilter nextFilter, IoSession session)
          Filters IoHandler.sessionOpened(IoSession) event.
 void setKeepAliveTime(int keepAliveTime)
           
 void setMaximumPoolSize(int maximumPoolSize)
           
 void setThreadNamePrefix(String threadNamePrefix)
           
 void start()
          Starts handling new events.
 void stop()
          Stops handling events and terminates all active worker threads.
 
Methods inherited from class org.apache.mina.common.IoFilterAdapter
destroy, init, onPostAdd, onPostRemove, onPreRemove
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_MAXIMUM_POOL_SIZE

public static final int DEFAULT_MAXIMUM_POOL_SIZE
Default maximum size of thread pool (16).

See Also:
Constant Field Values

DEFAULT_KEEP_ALIVE_TIME

public static final int DEFAULT_KEEP_ALIVE_TIME
Default keep-alive time of thread pool (1 min).

See Also:
Constant Field Values
Constructor Detail

ThreadPoolFilter

public ThreadPoolFilter()
Creates a new instance of this filter with default thread pool settings.


ThreadPoolFilter

public ThreadPoolFilter(String threadNamePrefix)
Creates a new instance of this filter with the specified thread name prefix and other default settings.

Parameters:
threadNamePrefix - the prefix of the thread names this pool will create.

ThreadPoolFilter

public ThreadPoolFilter(String threadNamePrefix,
                        int maximumPoolSize)
Creates a new instance of this filter with the specified thread name prefix and other default settings.

Parameters:
threadNamePrefix - the prefix of the thread names this pool will create.
maximumPoolSize - Maximum size of thread pool
Method Detail

getThreadNamePrefix

public String getThreadNamePrefix()

setThreadNamePrefix

public void setThreadNamePrefix(String threadNamePrefix)

getPoolSize

public int getPoolSize()

getMaximumPoolSize

public int getMaximumPoolSize()

getKeepAliveTime

public int getKeepAliveTime()

setMaximumPoolSize

public void setMaximumPoolSize(int maximumPoolSize)

setKeepAliveTime

public void setKeepAliveTime(int keepAliveTime)

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

start

public void start()
Starts handling new events. This will be called automatically if not already called the first time this filter is added to a filter chain.


stop

public void stop()
Stops handling events and terminates all active worker threads.


fetchSessionBuffer

protected ThreadPoolFilter.SessionBuffer fetchSessionBuffer(Queue unfetchedSessionBuffers)
Implement this method to fetch (or pop) a ThreadPoolFilter.SessionBuffer from the given unfetchedSessionBuffers. The default implementation simply pops the buffer from it. You could prioritize the fetch order.

Returns:
A non-null ThreadPoolFilter.SessionBuffer

sessionCreated

public void sessionCreated(IoFilter.NextFilter nextFilter,
                           IoSession session)
Description copied from interface: IoFilter
Filters IoHandler.sessionCreated(IoSession) event.

Specified by:
sessionCreated in interface IoFilter
Overrides:
sessionCreated in class IoFilterAdapter

sessionOpened

public void sessionOpened(IoFilter.NextFilter nextFilter,
                          IoSession session)
Description copied from interface: IoFilter
Filters IoHandler.sessionOpened(IoSession) event.

Specified by:
sessionOpened in interface IoFilter
Overrides:
sessionOpened in class IoFilterAdapter

sessionClosed

public void sessionClosed(IoFilter.NextFilter nextFilter,
                          IoSession session)
Description copied from interface: IoFilter
Filters IoHandler.sessionClosed(IoSession) event.

Specified by:
sessionClosed in interface IoFilter
Overrides:
sessionClosed in class IoFilterAdapter

sessionIdle

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

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

exceptionCaught

public void exceptionCaught(IoFilter.NextFilter nextFilter,
                            IoSession session,
                            Throwable cause)
Description copied from interface: IoFilter
Filters IoHandler.exceptionCaught(IoSession,Throwable) event.

Specified by:
exceptionCaught in interface IoFilter
Overrides:
exceptionCaught in class IoFilterAdapter

messageReceived

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

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

messageSent

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

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

processEvent

protected void processEvent(IoFilter.NextFilter nextFilter,
                            IoSession session,
                            ThreadPoolFilter.EventType type,
                            Object data)

filterWrite

public void filterWrite(IoFilter.NextFilter nextFilter,
                        IoSession session,
                        IoFilter.WriteRequest writeRequest)
Description copied from interface: IoFilter
Filters IoSession.write(Object) method invocation.

Specified by:
filterWrite in interface IoFilter
Overrides:
filterWrite in class IoFilterAdapter

filterClose

public void filterClose(IoFilter.NextFilter nextFilter,
                        IoSession session)
                 throws Exception
Description copied from interface: IoFilter
Filters IoSession.close() method invocation.

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