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 toExecutor
to 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 anExecutor
that 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 newOrderedThreadPoolExecutor
instance. 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.)
Executor
instance 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.)
OrderedThreadPoolExecutor
instance or use the convenience constructors.Selective Filtering
By default, all event types butsessionCreated
,filterWrite
,filterClose
andfilterSetTrafficMask
are 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 toOutOfMemoryError
IoEventQueueThrottle
, which is specified as a parameter of the convenience constructors.- Author:
- Apache MINA Project
- See Also:
OrderedThreadPoolExecutor
,UnorderedThreadPoolExecutor
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.apache.mina.core.filterchain.IoFilter
IoFilter.NextFilter
-
-
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, TimeUnit unit)
(Convenience constructor) Creates a new instance with a newOrderedThreadPoolExecutor
.ExecutorFilter(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, ThreadFactory threadFactory)
(Convenience constructor) Creates a new instance with a newOrderedThreadPoolExecutor
.ExecutorFilter(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, ThreadFactory threadFactory, IoEventType... eventTypes)
(Convenience constructor) Creates a new instance with a newOrderedThreadPoolExecutor
.ExecutorFilter(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, ThreadFactory threadFactory, IoEventQueueHandler queueHandler)
(Convenience constructor) Creates a new instance with a newOrderedThreadPoolExecutor
.ExecutorFilter(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, ThreadFactory threadFactory, IoEventQueueHandler queueHandler, IoEventType... eventTypes)
(Convenience constructor) Creates a new instance with a newOrderedThreadPoolExecutor
.ExecutorFilter(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, IoEventType... eventTypes)
(Convenience constructor) Creates a new instance with a newOrderedThreadPoolExecutor
.ExecutorFilter(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, IoEventQueueHandler queueHandler)
(Convenience constructor) Creates a new instance with a newOrderedThreadPoolExecutor
.ExecutorFilter(int corePoolSize, int maximumPoolSize, long keepAliveTime, 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(Executor executor)
Creates a new instance with the specifiedExecutor
.ExecutorFilter(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
-
Methods inherited from class org.apache.mina.core.filterchain.IoFilterAdapter
event, init, inputClosed, onPostAdd, onPostRemove, onPreRemove, sessionCreated, toString
-
-
-
-
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, 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, 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, TimeUnit unit, 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, TimeUnit unit, 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, 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, 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, TimeUnit unit, 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, TimeUnit unit, 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(Executor executor)
Creates a new instance with the specifiedExecutor
.- Parameters:
executor
- the user's managed Executor to use in this filter
-
ExecutorFilter
public ExecutorFilter(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
-
destroy
public void destroy()
Shuts down the underlying executor if this filter hase been created via a convenience constructor.- Specified by:
destroy
in interfaceIoFilter
- Overrides:
destroy
in classIoFilterAdapter
-
getExecutor
public final Executor getExecutor()
- Returns:
- the underlying
Executor
instance 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, String name, IoFilter.NextFilter nextFilter) throws 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:
onPreAdd
in interfaceIoFilter
- Overrides:
onPreAdd
in classIoFilterAdapter
- Parameters:
parent
- the parent who called this methodname
- the name assigned to this filternextFilter
- theIoFilter.NextFilter
for this filter. You can reuse this object until this filter is removed from the chain.- Throws:
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:
sessionOpened
in interfaceIoFilter
- Overrides:
sessionOpened
in classIoFilterAdapter
- Parameters:
nextFilter
- theIoFilter.NextFilter
for this filter. You can reuse this object until this filter is removed from the chain.session
- TheIoSession
which has received this event
-
sessionClosed
public final void sessionClosed(IoFilter.NextFilter nextFilter, IoSession session)
FiltersIoHandler.sessionClosed(IoSession)
event.- Specified by:
sessionClosed
in interfaceIoFilter
- Overrides:
sessionClosed
in classIoFilterAdapter
- Parameters:
nextFilter
- theIoFilter.NextFilter
for this filter. You can reuse this object until this filter is removed from the chain.session
- TheIoSession
which has received this event
-
sessionIdle
public final void sessionIdle(IoFilter.NextFilter nextFilter, IoSession session, IdleStatus status)
FiltersIoHandler.sessionIdle(IoSession,IdleStatus)
event.- Specified by:
sessionIdle
in interfaceIoFilter
- Overrides:
sessionIdle
in classIoFilterAdapter
- Parameters:
nextFilter
- theIoFilter.NextFilter
for this filter. You can reuse this object until this filter is removed from the chain.session
- TheIoSession
which has received this eventstatus
- TheIdleStatus
type
-
exceptionCaught
public final void exceptionCaught(IoFilter.NextFilter nextFilter, IoSession session, Throwable cause)
FiltersIoHandler.exceptionCaught(IoSession,Throwable)
event.- Specified by:
exceptionCaught
in interfaceIoFilter
- Overrides:
exceptionCaught
in classIoFilterAdapter
- Parameters:
nextFilter
- theIoFilter.NextFilter
for this filter. You can reuse this object until this filter is removed from the chain.session
- TheIoSession
which has received this eventcause
- The exception that cause this event to be received
-
messageReceived
public final void messageReceived(IoFilter.NextFilter nextFilter, IoSession session, Object message)
FiltersIoHandler.messageReceived(IoSession,Object)
event.- Specified by:
messageReceived
in interfaceIoFilter
- Overrides:
messageReceived
in classIoFilterAdapter
- Parameters:
nextFilter
- theIoFilter.NextFilter
for this filter. You can reuse this object until this filter is removed from the chain.session
- TheIoSession
which 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:
messageSent
in interfaceIoFilter
- Overrides:
messageSent
in classIoFilterAdapter
- Parameters:
nextFilter
- theIoFilter.NextFilter
for this filter. You can reuse this object until this filter is removed from the chain.session
- TheIoSession
which has received this eventwriteRequest
- TheWriteRequest
that contains the sent message
-
filterWrite
public final void filterWrite(IoFilter.NextFilter nextFilter, IoSession session, WriteRequest writeRequest)
FiltersIoSession.write(Object)
method invocation.- Specified by:
filterWrite
in interfaceIoFilter
- Overrides:
filterWrite
in classIoFilterAdapter
- Parameters:
nextFilter
- theIoFilter.NextFilter
for this filter. You can reuse this object until this filter is removed from the chain.session
- TheIoSession
which has to process this invocationwriteRequest
- TheWriteRequest
to process
-
filterClose
public final void filterClose(IoFilter.NextFilter nextFilter, IoSession session) throws Exception
FiltersIoSession.closeNow()
or aIoSession.closeOnFlush()
method invocations.- Specified by:
filterClose
in interfaceIoFilter
- Overrides:
filterClose
in classIoFilterAdapter
- Parameters:
nextFilter
- theIoFilter.NextFilter
for this filter. You can reuse this object until this filter is removed from the chain.session
- TheIoSession
which has to process this method invocation- Throws:
Exception
- If an error occurred while processing the event
-
-