Class ExecutorFilter
java.lang.Object
org.apache.mina.core.filterchain.IoFilterAdapter
org.apache.mina.filter.executor.ExecutorFilter
- All Implemented Interfaces:
IoFilter
A filter that forwards I/O events to Preventing
Please refer to
Executor 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 a IoFilterChain.
Life Cycle Management
Please note that this filter doesn't manage the life cycle of theExecutor.
If you created this filter using ExecutorFilter(Executor) or similar
constructor that accepts an Executor that you've instantiated, you have
full control and responsibility of managing its life cycle (e.g. calling
ExecutorService.shutdown().
If you created this filter using convenience constructors like
ExecutorFilter(int), then you can shut down the executor by calling
destroy() 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 and filterSetTrafficMask 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 OutOfMemoryError
Please refer to IoEventQueueThrottle, which is specified as
a parameter of the convenience constructors.- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface IoFilter
IoFilter.NextFilter -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final intThe number of thread to create at startupprivate static final IoEventType[]A list of default EventTypes to be handled by the executorprivate static final longThe default KeepAlive time, in secondsprivate static final intThe default pool sizeprivate EnumSet<IoEventType> The list of handled eventsprivate ExecutorThe associated executorprivate static final booleanA set of flags used to tell if the Executor has been created in the constructor or passed as an argument.private booleanA flag set if the executor can be managedprivate static final boolean -
Constructor Summary
ConstructorsConstructorDescription(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
Modifier and TypeMethodDescriptionvoiddestroy()Shuts down the underlying executor if this filter hase been created via a convenience constructor.final voidexceptionCaught(IoFilter.NextFilter nextFilter, IoSession session, Throwable cause) FiltersIoHandler.exceptionCaught(IoSession,Throwable)event.final voidfilterClose(IoFilter.NextFilter nextFilter, IoSession session) FiltersIoSession.closeNow()or aIoSession.closeOnFlush()method invocations.final 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.final Executorprivate voidinit(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.final voidmessageReceived(IoFilter.NextFilter nextFilter, IoSession session, Object message) FiltersIoHandler.messageReceived(IoSession,Object)event.final voidmessageSent(IoFilter.NextFilter nextFilter, IoSession session, WriteRequest writeRequest) FiltersIoHandler.messageSent(IoSession,Object)event.voidonPreAdd(IoFilterChain parent, String name, IoFilter.NextFilter nextFilter) Invoked before this filter is added to the specifiedparent.final voidsessionClosed(IoFilter.NextFilter nextFilter, IoSession session) FiltersIoHandler.sessionClosed(IoSession)event.final voidsessionIdle(IoFilter.NextFilter nextFilter, IoSession session, IdleStatus status) FiltersIoHandler.sessionIdle(IoSession,IdleStatus)event.final voidsessionOpened(IoFilter.NextFilter nextFilter, IoSession session) FiltersIoHandler.sessionOpened(IoSession)event.Methods inherited from class IoFilterAdapter
event, init, inputClosed, onPostAdd, onPostRemove, onPreRemove, sessionCreated, toString
-
Field Details
-
eventTypes
The list of handled events -
executor
The associated executor -
manageableExecutor
private boolean manageableExecutorA flag set if the executor can be managed -
DEFAULT_MAX_POOL_SIZE
private static final int DEFAULT_MAX_POOL_SIZEThe default pool size- See Also:
-
BASE_THREAD_NUMBER
private static final int BASE_THREAD_NUMBERThe number of thread to create at startup- See Also:
-
DEFAULT_KEEPALIVE_TIME
private static final long DEFAULT_KEEPALIVE_TIMEThe default KeepAlive time, in seconds- See Also:
-
MANAGEABLE_EXECUTOR
private static final boolean MANAGEABLE_EXECUTORA 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:
-
NOT_MANAGEABLE_EXECUTOR
private static final boolean NOT_MANAGEABLE_EXECUTOR- See Also:
-
DEFAULT_EVENT_SET
A list of default EventTypes to be handled by the executor
-
-
Constructor Details
-
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
(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
(Convenience constructor) Creates a new instance with a newOrderedThreadPoolExecutor.- Parameters:
eventTypes- The event for which the executor will be used
-
ExecutorFilter
(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
(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
-
ExecutorFilter
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 Details
-
initEventTypes
Create an EnumSet from an array of EventTypes, and set the associated eventTypes field.- Parameters:
eventTypes- The array of handled events
-
init
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
-
fireEvent
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:
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:
Exception- If an error occurred while processing the event
-
sessionOpened
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
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
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, 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, 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
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:
Exception- If an error occurred while processing the event
-