Package org.apache.mina.filter.executor
Class ExecutorFilter
- java.lang.Object
-
- org.apache.mina.core.filterchain.IoFilterAdapter
-
- org.apache.mina.filter.executor.ExecutorFilter
-
- All Implemented Interfaces:
IoFilter
public class ExecutorFilter extends IoFilterAdapter
A filter that forwards I/O events toExecutorto enforce a certain thread model while allowing the events per session to be processed simultaneously. You can apply various thread model by inserting this filter to aIoFilterChain.Life Cycle Management
Please note that this filter doesn't manage the life cycle of theExecutor. If you created this filter usingExecutorFilter(Executor)or similar constructor that accepts anExecutorthat you've instantiated, you have full control and responsibility of managing its life cycle (e.g. callingExecutorService.shutdown().If you created this filter using convenience constructors like
ExecutorFilter(int), then you can shut down the executor by callingdestroy()explicitly.Event Ordering
All convenience constructors of this filter creates a newOrderedThreadPoolExecutorinstance. Therefore, the order of event is maintained like the following:- All event handler methods are called exclusively. (e.g. messageReceived and messageSent can't be invoked at the same time.)
- The event order is never mixed up. (e.g. messageReceived is always invoked before sessionClosed or messageSent.)
Executorinstance in the constructor, the order of events are not maintained at all. This means more than one event handler methods can be invoked at the same time with mixed order. For example, let's assume that messageReceived, messageSent, and sessionClosed events are fired.- All event handler methods can be called simultaneously. (e.g. messageReceived and messageSent can be invoked at the same time.)
- The event order can be mixed up. (e.g. sessionClosed or messageSent can be invoked before messageReceived is invoked.)
OrderedThreadPoolExecutorinstance or use the convenience constructors.Selective Filtering
By default, all event types butsessionCreated,filterWrite,filterCloseandfilterSetTrafficMaskare submitted to the underlying executor, which is most common setting.If you want to submit only a certain set of event types, you can specify them in the constructor. For example, you could configure a thread pool for write operation for the maximum performance:
IoService service = ...; DefaultIoFilterChainBuilder chain = service.getFilterChain(); chain.addLast("codec", new ProtocolCodecFilter(...)); // Use one thread pool for most events. chain.addLast("executor1", new ExecutorFilter()); // and another dedicated thread pool for 'filterWrite' events. chain.addLast("executor2", new ExecutorFilter(IoEventType.WRITE));Preventing
Please refer toOutOfMemoryErrorIoEventQueueThrottle, which is specified as a parameter of the convenience constructors.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.apache.mina.core.filterchain.IoFilter
IoFilter.NextFilter
-
-
Field Summary
Fields Modifier and Type Field Description private static intBASE_THREAD_NUMBERThe number of thread to create at startupprivate static IoEventType[]DEFAULT_EVENT_SETA list of default EventTypes to be handled by the executorprivate static longDEFAULT_KEEPALIVE_TIMEThe default KeepAlive time, in secondsprivate static intDEFAULT_MAX_POOL_SIZEThe default pool sizeprivate java.util.EnumSet<IoEventType>eventTypesThe list of handled eventsprivate java.util.concurrent.ExecutorexecutorThe associated executorprivate static booleanMANAGEABLE_EXECUTORA set of flags used to tell if the Executor has been created in the constructor or passed as an argument.private booleanmanageableExecutorA flag set if the executor can be managedprivate static booleanNOT_MANAGEABLE_EXECUTOR
-
Constructor Summary
Constructors Constructor Description ExecutorFilter()(Convenience constructor) Creates a new instance with a newOrderedThreadPoolExecutor, no thread in the pool, and a maximum of 16 threads in the pool.ExecutorFilter(int maximumPoolSize)(Convenience constructor) Creates a new instance with a newOrderedThreadPoolExecutor, no thread in the pool, but a maximum of threads in the pool is given.ExecutorFilter(int corePoolSize, int maximumPoolSize)(Convenience constructor) Creates a new instance with a newOrderedThreadPoolExecutor, a number of thread to start with, a maximum of threads the pool can contain.ExecutorFilter(int corePoolSize, int maximumPoolSize, long keepAliveTime, java.util.concurrent.TimeUnit unit)(Convenience constructor) Creates a new instance with a newOrderedThreadPoolExecutor.ExecutorFilter(int corePoolSize, int maximumPoolSize, long keepAliveTime, java.util.concurrent.TimeUnit unit, java.util.concurrent.ThreadFactory threadFactory)(Convenience constructor) Creates a new instance with a newOrderedThreadPoolExecutor.ExecutorFilter(int corePoolSize, int maximumPoolSize, long keepAliveTime, java.util.concurrent.TimeUnit unit, java.util.concurrent.ThreadFactory threadFactory, IoEventType... eventTypes)(Convenience constructor) Creates a new instance with a newOrderedThreadPoolExecutor.ExecutorFilter(int corePoolSize, int maximumPoolSize, long keepAliveTime, java.util.concurrent.TimeUnit unit, java.util.concurrent.ThreadFactory threadFactory, IoEventQueueHandler queueHandler)(Convenience constructor) Creates a new instance with a newOrderedThreadPoolExecutor.ExecutorFilter(int corePoolSize, int maximumPoolSize, long keepAliveTime, java.util.concurrent.TimeUnit unit, java.util.concurrent.ThreadFactory threadFactory, IoEventQueueHandler queueHandler, IoEventType... eventTypes)(Convenience constructor) Creates a new instance with a newOrderedThreadPoolExecutor.ExecutorFilter(int corePoolSize, int maximumPoolSize, long keepAliveTime, java.util.concurrent.TimeUnit unit, IoEventType... eventTypes)(Convenience constructor) Creates a new instance with a newOrderedThreadPoolExecutor.ExecutorFilter(int corePoolSize, int maximumPoolSize, long keepAliveTime, java.util.concurrent.TimeUnit unit, IoEventQueueHandler queueHandler)(Convenience constructor) Creates a new instance with a newOrderedThreadPoolExecutor.ExecutorFilter(int corePoolSize, int maximumPoolSize, long keepAliveTime, java.util.concurrent.TimeUnit unit, IoEventQueueHandler queueHandler, IoEventType... eventTypes)(Convenience constructor) Creates a new instance with a newOrderedThreadPoolExecutor.ExecutorFilter(int corePoolSize, int maximumPoolSize, IoEventType... eventTypes)(Convenience constructor) Creates a new instance with a newOrderedThreadPoolExecutor.ExecutorFilter(int maximumPoolSize, IoEventType... eventTypes)(Convenience constructor) Creates a new instance with a newOrderedThreadPoolExecutor.ExecutorFilter(java.util.concurrent.Executor executor)Creates a new instance with the specifiedExecutor.ExecutorFilter(java.util.concurrent.Executor executor, IoEventType... eventTypes)Creates a new instance with the specifiedExecutor.ExecutorFilter(IoEventType... eventTypes)(Convenience constructor) Creates a new instance with a newOrderedThreadPoolExecutor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voiddestroy()Shuts down the underlying executor if this filter hase been created via a convenience constructor.voidexceptionCaught(IoFilter.NextFilter nextFilter, IoSession session, java.lang.Throwable cause)FiltersIoHandler.exceptionCaught(IoSession,Throwable)event.voidfilterClose(IoFilter.NextFilter nextFilter, IoSession session)FiltersIoSession.closeNow()or aIoSession.closeOnFlush()method invocations.voidfilterWrite(IoFilter.NextFilter nextFilter, IoSession session, WriteRequest writeRequest)FiltersIoSession.write(Object)method invocation.protected voidfireEvent(IoFilterEvent event)Fires the specified event through the underlying executor.java.util.concurrent.ExecutorgetExecutor()private voidinit(java.util.concurrent.Executor executor, boolean manageableExecutor, IoEventType... eventTypes)Creates a new instance of ExecutorFilter.private voidinitEventTypes(IoEventType... eventTypes)Create an EnumSet from an array of EventTypes, and set the associated eventTypes field.voidmessageReceived(IoFilter.NextFilter nextFilter, IoSession session, java.lang.Object message)FiltersIoHandler.messageReceived(IoSession,Object)event.voidmessageSent(IoFilter.NextFilter nextFilter, IoSession session, WriteRequest writeRequest)FiltersIoHandler.messageSent(IoSession,Object)event.voidonPreAdd(IoFilterChain parent, java.lang.String name, IoFilter.NextFilter nextFilter)Invoked before this filter is added to the specifiedparent.voidsessionClosed(IoFilter.NextFilter nextFilter, IoSession session)FiltersIoHandler.sessionClosed(IoSession)event.voidsessionIdle(IoFilter.NextFilter nextFilter, IoSession session, IdleStatus status)FiltersIoHandler.sessionIdle(IoSession,IdleStatus)event.voidsessionOpened(IoFilter.NextFilter nextFilter, IoSession session)FiltersIoHandler.sessionOpened(IoSession)event.-
Methods inherited from class org.apache.mina.core.filterchain.IoFilterAdapter
event, init, inputClosed, onPostAdd, onPostRemove, onPreRemove, sessionCreated, toString
-
-
-
-
Field Detail
-
eventTypes
private java.util.EnumSet<IoEventType> eventTypes
The list of handled events
-
executor
private java.util.concurrent.Executor executor
The associated executor
-
manageableExecutor
private boolean manageableExecutor
A flag set if the executor can be managed
-
DEFAULT_MAX_POOL_SIZE
private static final int DEFAULT_MAX_POOL_SIZE
The default pool size- See Also:
- Constant Field Values
-
BASE_THREAD_NUMBER
private static final int BASE_THREAD_NUMBER
The number of thread to create at startup- See Also:
- Constant Field Values
-
DEFAULT_KEEPALIVE_TIME
private static final long DEFAULT_KEEPALIVE_TIME
The default KeepAlive time, in seconds- See Also:
- Constant Field Values
-
MANAGEABLE_EXECUTOR
private static final boolean MANAGEABLE_EXECUTOR
A set of flags used to tell if the Executor has been created in the constructor or passed as an argument. In the second case, the executor state can be managed.- See Also:
- Constant Field Values
-
NOT_MANAGEABLE_EXECUTOR
private static final boolean NOT_MANAGEABLE_EXECUTOR
- See Also:
- Constant Field Values
-
DEFAULT_EVENT_SET
private static final IoEventType[] DEFAULT_EVENT_SET
A list of default EventTypes to be handled by the executor
-
-
Constructor Detail
-
ExecutorFilter
public ExecutorFilter()
(Convenience constructor) Creates a new instance with a newOrderedThreadPoolExecutor, no thread in the pool, and a maximum of 16 threads in the pool. All the event will be handled by this default executor.
-
ExecutorFilter
public ExecutorFilter(int maximumPoolSize)
(Convenience constructor) Creates a new instance with a newOrderedThreadPoolExecutor, no thread in the pool, but a maximum of threads in the pool is given. All the event will be handled by this default executor.- Parameters:
maximumPoolSize- The maximum pool size
-
ExecutorFilter
public ExecutorFilter(int corePoolSize, int maximumPoolSize)(Convenience constructor) Creates a new instance with a newOrderedThreadPoolExecutor, a number of thread to start with, a maximum of threads the pool can contain. All the event will be handled by this default executor.- Parameters:
corePoolSize- The initial pool sizemaximumPoolSize- The maximum pool size
-
ExecutorFilter
public ExecutorFilter(int corePoolSize, int maximumPoolSize, long keepAliveTime, java.util.concurrent.TimeUnit unit)(Convenience constructor) Creates a new instance with a newOrderedThreadPoolExecutor.- Parameters:
corePoolSize- The initial pool sizemaximumPoolSize- The maximum pool sizekeepAliveTime- Default duration for a threadunit- Time unit used for the keepAlive value
-
ExecutorFilter
public ExecutorFilter(int corePoolSize, int maximumPoolSize, long keepAliveTime, java.util.concurrent.TimeUnit unit, IoEventQueueHandler queueHandler)(Convenience constructor) Creates a new instance with a newOrderedThreadPoolExecutor.- Parameters:
corePoolSize- The initial pool sizemaximumPoolSize- The maximum pool sizekeepAliveTime- Default duration for a threadunit- Time unit used for the keepAlive valuequeueHandler- The queue used to store events
-
ExecutorFilter
public ExecutorFilter(int corePoolSize, int maximumPoolSize, long keepAliveTime, java.util.concurrent.TimeUnit unit, java.util.concurrent.ThreadFactory threadFactory)(Convenience constructor) Creates a new instance with a newOrderedThreadPoolExecutor.- Parameters:
corePoolSize- The initial pool sizemaximumPoolSize- The maximum pool sizekeepAliveTime- Default duration for a threadunit- Time unit used for the keepAlive valuethreadFactory- The factory used to create threads
-
ExecutorFilter
public ExecutorFilter(int corePoolSize, int maximumPoolSize, long keepAliveTime, java.util.concurrent.TimeUnit unit, java.util.concurrent.ThreadFactory threadFactory, IoEventQueueHandler queueHandler)(Convenience constructor) Creates a new instance with a newOrderedThreadPoolExecutor.- Parameters:
corePoolSize- The initial pool sizemaximumPoolSize- The maximum pool sizekeepAliveTime- Default duration for a threadunit- Time unit used for the keepAlive valuethreadFactory- The factory used to create threadsqueueHandler- The queue used to store events
-
ExecutorFilter
public ExecutorFilter(IoEventType... eventTypes)
(Convenience constructor) Creates a new instance with a newOrderedThreadPoolExecutor.- Parameters:
eventTypes- The event for which the executor will be used
-
ExecutorFilter
public ExecutorFilter(int maximumPoolSize, IoEventType... eventTypes)(Convenience constructor) Creates a new instance with a newOrderedThreadPoolExecutor.- Parameters:
maximumPoolSize- The maximum pool sizeeventTypes- The event for which the executor will be used
-
ExecutorFilter
public ExecutorFilter(int corePoolSize, int maximumPoolSize, IoEventType... eventTypes)(Convenience constructor) Creates a new instance with a newOrderedThreadPoolExecutor.- Parameters:
corePoolSize- The initial pool sizemaximumPoolSize- The maximum pool sizeeventTypes- The event for which the executor will be used
-
ExecutorFilter
public ExecutorFilter(int corePoolSize, int maximumPoolSize, long keepAliveTime, java.util.concurrent.TimeUnit unit, IoEventType... eventTypes)(Convenience constructor) Creates a new instance with a newOrderedThreadPoolExecutor.- Parameters:
corePoolSize- The initial pool sizemaximumPoolSize- The maximum pool sizekeepAliveTime- Default duration for a threadunit- Time unit used for the keepAlive valueeventTypes- The event for which the executor will be used
-
ExecutorFilter
public ExecutorFilter(int corePoolSize, int maximumPoolSize, long keepAliveTime, java.util.concurrent.TimeUnit unit, IoEventQueueHandler queueHandler, IoEventType... eventTypes)(Convenience constructor) Creates a new instance with a newOrderedThreadPoolExecutor.- Parameters:
corePoolSize- The initial pool sizemaximumPoolSize- The maximum pool sizekeepAliveTime- Default duration for a threadunit- Time unit used for the keepAlive valuequeueHandler- The queue used to store eventseventTypes- The event for which the executor will be used
-
ExecutorFilter
public ExecutorFilter(int corePoolSize, int maximumPoolSize, long keepAliveTime, java.util.concurrent.TimeUnit unit, java.util.concurrent.ThreadFactory threadFactory, IoEventType... eventTypes)(Convenience constructor) Creates a new instance with a newOrderedThreadPoolExecutor.- Parameters:
corePoolSize- The initial pool sizemaximumPoolSize- The maximum pool sizekeepAliveTime- Default duration for a threadunit- Time unit used for the keepAlive valuethreadFactory- The factory used to create threadseventTypes- The event for which the executor will be used
-
ExecutorFilter
public ExecutorFilter(int corePoolSize, int maximumPoolSize, long keepAliveTime, java.util.concurrent.TimeUnit unit, java.util.concurrent.ThreadFactory threadFactory, IoEventQueueHandler queueHandler, IoEventType... eventTypes)(Convenience constructor) Creates a new instance with a newOrderedThreadPoolExecutor.- Parameters:
corePoolSize- The initial pool sizemaximumPoolSize- The maximum pool sizekeepAliveTime- Default duration for a threadunit- Time unit used for the keepAlive valuethreadFactory- The factory used to create threadsqueueHandler- The queue used to store eventseventTypes- The event for which the executor will be used
-
ExecutorFilter
public ExecutorFilter(java.util.concurrent.Executor executor)
Creates a new instance with the specifiedExecutor.- Parameters:
executor- the user's managed Executor to use in this filter
-
ExecutorFilter
public ExecutorFilter(java.util.concurrent.Executor executor, IoEventType... eventTypes)Creates a new instance with the specifiedExecutor.- Parameters:
executor- the user's managed Executor to use in this filtereventTypes- The event for which the executor will be used
-
-
Method Detail
-
initEventTypes
private void initEventTypes(IoEventType... eventTypes)
Create an EnumSet from an array of EventTypes, and set the associated eventTypes field.- Parameters:
eventTypes- The array of handled events
-
init
private void init(java.util.concurrent.Executor executor, boolean manageableExecutor, IoEventType... eventTypes)Creates a new instance of ExecutorFilter. This private constructor is called by all the public constructor.- Parameters:
executor- The underlyingExecutorin charge of managing the Thread pool.manageableExecutor- Tells if the Executor's Life Cycle can be managed or noteventTypes- The lit of event which are handled by the executor
-
destroy
public void destroy()
Shuts down the underlying executor if this filter hase been created via a convenience constructor.- Specified by:
destroyin interfaceIoFilter- Overrides:
destroyin classIoFilterAdapter
-
getExecutor
public final java.util.concurrent.Executor getExecutor()
- Returns:
- the underlying
Executorinstance this filter uses.
-
fireEvent
protected void fireEvent(IoFilterEvent event)
Fires the specified event through the underlying executor.- Parameters:
event- The filtered event
-
onPreAdd
public void onPreAdd(IoFilterChain parent, java.lang.String name, IoFilter.NextFilter nextFilter) throws java.lang.Exception
Invoked before this filter is added to the specifiedparent. 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 beforeIoFilter.init()is invoked.- Specified by:
onPreAddin interfaceIoFilter- Overrides:
onPreAddin classIoFilterAdapter- Parameters:
parent- the parent who called this methodname- the name assigned to this filternextFilter- theIoFilter.NextFilterfor this filter. You can reuse this object until this filter is removed from the chain.- Throws:
java.lang.Exception- If an error occurred while processing the event
-
sessionOpened
public final void sessionOpened(IoFilter.NextFilter nextFilter, IoSession session)
FiltersIoHandler.sessionOpened(IoSession)event.- Specified by:
sessionOpenedin interfaceIoFilter- Overrides:
sessionOpenedin classIoFilterAdapter- Parameters:
nextFilter- theIoFilter.NextFilterfor this filter. You can reuse this object until this filter is removed from the chain.session- TheIoSessionwhich has received this event
-
sessionClosed
public final void sessionClosed(IoFilter.NextFilter nextFilter, IoSession session)
FiltersIoHandler.sessionClosed(IoSession)event.- Specified by:
sessionClosedin interfaceIoFilter- Overrides:
sessionClosedin classIoFilterAdapter- Parameters:
nextFilter- theIoFilter.NextFilterfor this filter. You can reuse this object until this filter is removed from the chain.session- TheIoSessionwhich has received this event
-
sessionIdle
public final void sessionIdle(IoFilter.NextFilter nextFilter, IoSession session, IdleStatus status)
FiltersIoHandler.sessionIdle(IoSession,IdleStatus)event.- Specified by:
sessionIdlein interfaceIoFilter- Overrides:
sessionIdlein classIoFilterAdapter- Parameters:
nextFilter- theIoFilter.NextFilterfor this filter. You can reuse this object until this filter is removed from the chain.session- TheIoSessionwhich has received this eventstatus- TheIdleStatustype
-
exceptionCaught
public final void exceptionCaught(IoFilter.NextFilter nextFilter, IoSession session, java.lang.Throwable cause)
FiltersIoHandler.exceptionCaught(IoSession,Throwable)event.- Specified by:
exceptionCaughtin interfaceIoFilter- Overrides:
exceptionCaughtin classIoFilterAdapter- Parameters:
nextFilter- theIoFilter.NextFilterfor this filter. You can reuse this object until this filter is removed from the chain.session- TheIoSessionwhich has received this eventcause- The exception that cause this event to be received
-
messageReceived
public final void messageReceived(IoFilter.NextFilter nextFilter, IoSession session, java.lang.Object message)
FiltersIoHandler.messageReceived(IoSession,Object)event.- Specified by:
messageReceivedin interfaceIoFilter- Overrides:
messageReceivedin classIoFilterAdapter- Parameters:
nextFilter- theIoFilter.NextFilterfor this filter. You can reuse this object until this filter is removed from the chain.session- TheIoSessionwhich has received this eventmessage- The received message
-
messageSent
public final void messageSent(IoFilter.NextFilter nextFilter, IoSession session, WriteRequest writeRequest)
FiltersIoHandler.messageSent(IoSession,Object)event.- Specified by:
messageSentin interfaceIoFilter- Overrides:
messageSentin classIoFilterAdapter- Parameters:
nextFilter- theIoFilter.NextFilterfor this filter. You can reuse this object until this filter is removed from the chain.session- TheIoSessionwhich has received this eventwriteRequest- TheWriteRequestthat contains the sent message
-
filterWrite
public final void filterWrite(IoFilter.NextFilter nextFilter, IoSession session, WriteRequest writeRequest)
FiltersIoSession.write(Object)method invocation.- Specified by:
filterWritein interfaceIoFilter- Overrides:
filterWritein classIoFilterAdapter- Parameters:
nextFilter- theIoFilter.NextFilterfor this filter. You can reuse this object until this filter is removed from the chain.session- TheIoSessionwhich has to process this invocationwriteRequest- TheWriteRequestto process
-
filterClose
public final void filterClose(IoFilter.NextFilter nextFilter, IoSession session) throws java.lang.Exception
FiltersIoSession.closeNow()or aIoSession.closeOnFlush()method invocations.- Specified by:
filterClosein interfaceIoFilter- Overrides:
filterClosein classIoFilterAdapter- Parameters:
nextFilter- theIoFilter.NextFilterfor this filter. You can reuse this object until this filter is removed from the chain.session- TheIoSessionwhich has to process this method invocation- Throws:
java.lang.Exception- If an error occurred while processing the event
-
-