S - the type of the IoSession to be managed by the specified
IoProcessor.public class SimpleIoProcessorPool<S extends AbstractIoSession> extends java.lang.Object implements IoProcessor<S>
IoProcessor pool that distributes IoSessions into one or more
IoProcessors. Most current transport implementations use this pool internally
to perform better in a multi-core environment, and therefore, you won't need to
use this pool directly unless you are running multiple IoServices in the
same JVM.
If you are running multiple IoServices, you could want to share the pool
among all services. To do so, you can create a new SimpleIoProcessorPool
instance by yourself and provide the pool as a constructor parameter when you
create the services.
This pool uses Java reflection API to create multiple IoProcessor instances.
It tries to instantiate the processor in the following order:
ExecutorService parameter.Executor parameter.
// Create a shared pool.
SimpleIoProcessorPool<NioSession> pool =
new SimpleIoProcessorPool<NioSession>(NioProcessor.class, 16);
// Create two services that share the same pool.
SocketAcceptor acceptor = new NioSocketAcceptor(pool);
SocketConnector connector = new NioSocketConnector(pool);
...
// Release related resources.
connector.dispose();
acceptor.dispose();
pool.dispose();
| Modifier and Type | Field and Description |
|---|---|
private boolean |
createdExecutor
A flag set to true if we had to create an executor
|
private static int |
DEFAULT_SIZE
The default pool size, when no size is provided.
|
private java.lang.Object |
disposalLock
A lock to protect the disposal against concurrent calls
|
private boolean |
disposed
A flag set to true if all the IoProcessor contained in the pool have been disposed
|
private boolean |
disposing
A flg set to true if the IoProcessor in the pool are being disposed
|
private java.util.concurrent.Executor |
executor
The contained which is passed to the IoProcessor when they are created
|
private static org.slf4j.Logger |
LOGGER
A logger for this class
|
private IoProcessor<S>[] |
pool
The pool table
|
private static AttributeKey |
PROCESSOR
A key used to store the processor pool in the session's Attributes
|
| Constructor and Description |
|---|
SimpleIoProcessorPool(java.lang.Class<? extends IoProcessor<S>> processorType)
Creates a new instance of SimpleIoProcessorPool with a default
size of NbCPUs +1.
|
SimpleIoProcessorPool(java.lang.Class<? extends IoProcessor<S>> processorType,
java.util.concurrent.Executor executor)
Creates a new instance of SimpleIoProcessorPool with an executor
|
SimpleIoProcessorPool(java.lang.Class<? extends IoProcessor<S>> processorType,
java.util.concurrent.Executor executor,
int size,
java.nio.channels.spi.SelectorProvider selectorProvider)
Creates a new instance of SimpleIoProcessorPool with an executor
|
SimpleIoProcessorPool(java.lang.Class<? extends IoProcessor<S>> processorType,
int size)
Creates a new instance of SimpleIoProcessorPool with a defined
number of IoProcessors in the pool
|
SimpleIoProcessorPool(java.lang.Class<? extends IoProcessor<S>> processorType,
int size,
java.nio.channels.spi.SelectorProvider selectorProvider)
Creates a new instance of SimpleIoProcessorPool with a defined
number of IoProcessors in the pool
|
| Modifier and Type | Method and Description |
|---|---|
void |
add(S session)
Adds the specified
session to the I/O processor so that
the I/O processor starts to perform any I/O operations related
with the session. |
void |
dispose()
Releases any resources allocated by this processor.
|
void |
flush(S session)
Flushes the internal write request queue of the specified
session. |
private IoProcessor<S> |
getProcessor(S session)
Find the processor associated to a session.
|
boolean |
isDisposed()
Returns true if and if only all resources of this processor
have been disposed.
|
boolean |
isDisposing()
Returns true if and if only
IoProcessor.dispose() method has
been called. |
void |
remove(S session)
Removes and closes the specified
session from the I/O
processor so that the I/O processor closes the connection
associated with the session and releases any other related
resources. |
void |
updateTrafficControl(S session)
Controls the traffic of the specified
session depending of the
IoSession.isReadSuspended() and IoSession.isWriteSuspended()
flags |
void |
write(S session,
WriteRequest writeRequest)
Writes the WriteRequest for the specified
session. |
private static final org.slf4j.Logger LOGGER
private static final int DEFAULT_SIZE
private static final AttributeKey PROCESSOR
private final IoProcessor<S extends AbstractIoSession>[] pool
private final java.util.concurrent.Executor executor
private final boolean createdExecutor
private final java.lang.Object disposalLock
private volatile boolean disposing
private volatile boolean disposed
public SimpleIoProcessorPool(java.lang.Class<? extends IoProcessor<S>> processorType)
processorType - The type of IoProcessor to usepublic SimpleIoProcessorPool(java.lang.Class<? extends IoProcessor<S>> processorType, int size)
processorType - The type of IoProcessor to usesize - The number of IoProcessor in the poolpublic SimpleIoProcessorPool(java.lang.Class<? extends IoProcessor<S>> processorType, int size, java.nio.channels.spi.SelectorProvider selectorProvider)
processorType - The type of IoProcessor to usesize - The number of IoProcessor in the poolselectorProvider - The SelectorProvider to usepublic SimpleIoProcessorPool(java.lang.Class<? extends IoProcessor<S>> processorType, java.util.concurrent.Executor executor)
processorType - The type of IoProcessor to useexecutor - The Executorpublic SimpleIoProcessorPool(java.lang.Class<? extends IoProcessor<S>> processorType, java.util.concurrent.Executor executor, int size, java.nio.channels.spi.SelectorProvider selectorProvider)
processorType - The type of IoProcessor to useexecutor - The Executorsize - The number of IoProcessor in the poolpublic final void add(S session)
session to the I/O processor so that
the I/O processor starts to perform any I/O operations related
with the session.add in interface IoProcessor<S extends AbstractIoSession>session - The added sessionpublic final void flush(S session)
session.flush in interface IoProcessor<S extends AbstractIoSession>session - The session we want the message to be writtenpublic final void write(S session, WriteRequest writeRequest)
session.write in interface IoProcessor<S extends AbstractIoSession>session - The session we want the message to be writtenwriteRequest - the WriteRequest to writepublic final void remove(S session)
session from the I/O
processor so that the I/O processor closes the connection
associated with the session and releases any other related
resources.remove in interface IoProcessor<S extends AbstractIoSession>public final void updateTrafficControl(S session)
session depending of the
IoSession.isReadSuspended() and IoSession.isWriteSuspended()
flagsupdateTrafficControl in interface IoProcessor<S extends AbstractIoSession>public boolean isDisposed()
isDisposed in interface IoProcessor<S extends AbstractIoSession>public boolean isDisposing()
IoProcessor.dispose() method has
been called. Please note that this method will return true
even after all the related resources are released.isDisposing in interface IoProcessor<S extends AbstractIoSession>public final void dispose()
dispose in interface IoProcessor<S extends AbstractIoSession>private IoProcessor<S> getProcessor(S session)