public class ExecutorFilter extends IoFilterAdapter
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
.
Executor
.
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.
OrderedThreadPoolExecutor
instance. Therefore, the order of event is
maintained like the following:
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.
OrderedThreadPoolExecutor
instance or use the convenience constructors.
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));
OutOfMemoryError
IoEventQueueThrottle
, which is specified as
a parameter of the convenience constructors.OrderedThreadPoolExecutor
,
UnorderedThreadPoolExecutor
IoFilter.NextFilter
Constructor and Description |
---|
ExecutorFilter()
(Convenience constructor) Creates a new instance with a new
OrderedThreadPoolExecutor , no thread in the pool, and a
maximum of 16 threads in the pool. |
ExecutorFilter(java.util.concurrent.Executor executor)
Creates a new instance with the specified
Executor . |
ExecutorFilter(java.util.concurrent.Executor executor,
IoEventType... eventTypes)
Creates a new instance with the specified
Executor . |
ExecutorFilter(int maximumPoolSize)
(Convenience constructor) Creates a new instance with a new
OrderedThreadPoolExecutor , 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 new
OrderedThreadPoolExecutor , a number of thread to start with, a
maximum of threads the pool can contain. |
ExecutorFilter(int corePoolSize,
int maximumPoolSize,
IoEventType... eventTypes)
(Convenience constructor) Creates a new instance with a new
OrderedThreadPoolExecutor . |
ExecutorFilter(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
java.util.concurrent.TimeUnit unit)
(Convenience constructor) Creates a new instance with a new
OrderedThreadPoolExecutor . |
ExecutorFilter(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
java.util.concurrent.TimeUnit unit,
IoEventQueueHandler queueHandler)
(Convenience constructor) Creates a new instance with a new
OrderedThreadPoolExecutor . |
ExecutorFilter(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
java.util.concurrent.TimeUnit unit,
IoEventQueueHandler queueHandler,
IoEventType... eventTypes)
(Convenience constructor) Creates a new instance with a new
OrderedThreadPoolExecutor . |
ExecutorFilter(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
java.util.concurrent.TimeUnit unit,
IoEventType... eventTypes)
(Convenience constructor) Creates a new instance with a new
OrderedThreadPoolExecutor . |
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 new
OrderedThreadPoolExecutor . |
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 new
OrderedThreadPoolExecutor . |
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 new
OrderedThreadPoolExecutor . |
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 new
OrderedThreadPoolExecutor . |
ExecutorFilter(int maximumPoolSize,
IoEventType... eventTypes)
(Convenience constructor) Creates a new instance with a new
OrderedThreadPoolExecutor . |
ExecutorFilter(IoEventType... eventTypes)
(Convenience constructor) Creates a new instance with a new
OrderedThreadPoolExecutor . |
Modifier and Type | Method and Description |
---|---|
void |
destroy()
Shuts down the underlying executor if this filter hase been created via
a convenience constructor.
|
void |
exceptionCaught(IoFilter.NextFilter nextFilter,
IoSession session,
java.lang.Throwable cause)
Filters
IoHandler.exceptionCaught(IoSession,Throwable)
event. |
void |
filterClose(IoFilter.NextFilter nextFilter,
IoSession session)
Filters
IoSession.close() method invocation. |
void |
filterWrite(IoFilter.NextFilter nextFilter,
IoSession session,
WriteRequest writeRequest)
Filters
IoSession.write(Object) method invocation. |
protected void |
fireEvent(IoFilterEvent event)
Fires the specified event through the underlying executor.
|
java.util.concurrent.Executor |
getExecutor()
Returns the underlying
Executor instance this filter uses. |
void |
messageReceived(IoFilter.NextFilter nextFilter,
IoSession session,
java.lang.Object message)
Filters
IoHandler.messageReceived(IoSession,Object)
event. |
void |
messageSent(IoFilter.NextFilter nextFilter,
IoSession session,
WriteRequest writeRequest)
Filters
IoHandler.messageSent(IoSession,Object)
event. |
void |
onPreAdd(IoFilterChain parent,
java.lang.String name,
IoFilter.NextFilter nextFilter)
Invoked before this filter is added to the specified parent.
|
void |
sessionClosed(IoFilter.NextFilter nextFilter,
IoSession session)
Filters
IoHandler.sessionClosed(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. |
init, onPostAdd, onPostRemove, onPreRemove, sessionCreated, toString
public ExecutorFilter()
OrderedThreadPoolExecutor
, no thread in the pool, and a
maximum of 16 threads in the pool. All the event will be handled
by this default executor.public ExecutorFilter(int maximumPoolSize)
OrderedThreadPoolExecutor
, 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.maximumPoolSize
- The maximum pool sizepublic ExecutorFilter(int corePoolSize, int maximumPoolSize)
OrderedThreadPoolExecutor
, 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.corePoolSize
- The initial pool sizemaximumPoolSize
- The maximum pool sizepublic ExecutorFilter(int corePoolSize, int maximumPoolSize, long keepAliveTime, java.util.concurrent.TimeUnit unit)
OrderedThreadPoolExecutor
.corePoolSize
- The initial pool sizemaximumPoolSize
- The maximum pool sizekeepAliveTime
- Default duration for a threadunit
- Time unit used for the keepAlive valuepublic ExecutorFilter(int corePoolSize, int maximumPoolSize, long keepAliveTime, java.util.concurrent.TimeUnit unit, IoEventQueueHandler queueHandler)
OrderedThreadPoolExecutor
.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 eventspublic ExecutorFilter(int corePoolSize, int maximumPoolSize, long keepAliveTime, java.util.concurrent.TimeUnit unit, java.util.concurrent.ThreadFactory threadFactory)
OrderedThreadPoolExecutor
.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 threadspublic ExecutorFilter(int corePoolSize, int maximumPoolSize, long keepAliveTime, java.util.concurrent.TimeUnit unit, java.util.concurrent.ThreadFactory threadFactory, IoEventQueueHandler queueHandler)
OrderedThreadPoolExecutor
.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 eventspublic ExecutorFilter(IoEventType... eventTypes)
OrderedThreadPoolExecutor
.eventTypes
- The event for which the executor will be usedpublic ExecutorFilter(int maximumPoolSize, IoEventType... eventTypes)
OrderedThreadPoolExecutor
.maximumPoolSize
- The maximum pool sizeeventTypes
- The event for which the executor will be usedpublic ExecutorFilter(int corePoolSize, int maximumPoolSize, IoEventType... eventTypes)
OrderedThreadPoolExecutor
.corePoolSize
- The initial pool sizemaximumPoolSize
- The maximum pool sizeeventTypes
- The event for which the executor will be usedpublic ExecutorFilter(int corePoolSize, int maximumPoolSize, long keepAliveTime, java.util.concurrent.TimeUnit unit, IoEventType... eventTypes)
OrderedThreadPoolExecutor
.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 usedpublic ExecutorFilter(int corePoolSize, int maximumPoolSize, long keepAliveTime, java.util.concurrent.TimeUnit unit, IoEventQueueHandler queueHandler, IoEventType... eventTypes)
OrderedThreadPoolExecutor
.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 usedpublic ExecutorFilter(int corePoolSize, int maximumPoolSize, long keepAliveTime, java.util.concurrent.TimeUnit unit, java.util.concurrent.ThreadFactory threadFactory, IoEventType... eventTypes)
OrderedThreadPoolExecutor
.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 usedpublic ExecutorFilter(int corePoolSize, int maximumPoolSize, long keepAliveTime, java.util.concurrent.TimeUnit unit, java.util.concurrent.ThreadFactory threadFactory, IoEventQueueHandler queueHandler, IoEventType... eventTypes)
OrderedThreadPoolExecutor
.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 usedpublic ExecutorFilter(java.util.concurrent.Executor executor)
Executor
.executor
- the user's managed Executor to use in this filterpublic ExecutorFilter(java.util.concurrent.Executor executor, IoEventType... eventTypes)
Executor
.executor
- the user's managed Executor to use in this filtereventTypes
- The event for which the executor will be usedpublic void destroy()
destroy
in interface IoFilter
destroy
in class IoFilterAdapter
public final java.util.concurrent.Executor getExecutor()
Executor
instance this filter uses.Executor
protected void fireEvent(IoFilterEvent event)
event
- The filtered eventpublic void onPreAdd(IoFilterChain parent, java.lang.String name, IoFilter.NextFilter nextFilter) throws java.lang.Exception
IoFilter.init()
is invoked.onPreAdd
in interface IoFilter
onPreAdd
in class IoFilterAdapter
parent
- the parent who called this methodname
- the name assigned to this filternextFilter
- the IoFilter.NextFilter
for this filter. You can reuse
this object until this filter is removed from the chain.java.lang.Exception
public final void sessionOpened(IoFilter.NextFilter nextFilter, IoSession session)
IoHandler.sessionOpened(IoSession)
event.sessionOpened
in interface IoFilter
sessionOpened
in class IoFilterAdapter
public final void sessionClosed(IoFilter.NextFilter nextFilter, IoSession session)
IoHandler.sessionClosed(IoSession)
event.sessionClosed
in interface IoFilter
sessionClosed
in class IoFilterAdapter
public final void sessionIdle(IoFilter.NextFilter nextFilter, IoSession session, IdleStatus status)
IoHandler.sessionIdle(IoSession,IdleStatus)
event.sessionIdle
in interface IoFilter
sessionIdle
in class IoFilterAdapter
public final void exceptionCaught(IoFilter.NextFilter nextFilter, IoSession session, java.lang.Throwable cause)
IoHandler.exceptionCaught(IoSession,Throwable)
event.exceptionCaught
in interface IoFilter
exceptionCaught
in class IoFilterAdapter
public final void messageReceived(IoFilter.NextFilter nextFilter, IoSession session, java.lang.Object message)
IoHandler.messageReceived(IoSession,Object)
event.messageReceived
in interface IoFilter
messageReceived
in class IoFilterAdapter
public final void messageSent(IoFilter.NextFilter nextFilter, IoSession session, WriteRequest writeRequest)
IoHandler.messageSent(IoSession,Object)
event.messageSent
in interface IoFilter
messageSent
in class IoFilterAdapter
public final void filterWrite(IoFilter.NextFilter nextFilter, IoSession session, WriteRequest writeRequest)
IoSession.write(Object)
method invocation.filterWrite
in interface IoFilter
filterWrite
in class IoFilterAdapter
public final void filterClose(IoFilter.NextFilter nextFilter, IoSession session) throws java.lang.Exception
IoSession.close()
method invocation.filterClose
in interface IoFilter
filterClose
in class IoFilterAdapter
java.lang.Exception
Copyright © 2004-2019 Apache MINA Project. All Rights Reserved.