Package org.apache.mina.core.service
Class SimpleIoProcessorPool<S extends AbstractIoSession>
- java.lang.Object
-
- org.apache.mina.core.service.SimpleIoProcessorPool<S>
-
- Type Parameters:
S- the type of theIoSessionto be managed by the specifiedIoProcessor.
- All Implemented Interfaces:
IoProcessor<S>
public class SimpleIoProcessorPool<S extends AbstractIoSession> extends java.lang.Object implements IoProcessor<S>
AnIoProcessorpool that distributesIoSessions into one or moreIoProcessors. 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 multipleIoServices 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 newSimpleIoProcessorPoolinstance by yourself and provide the pool as a constructor parameter when you create the services.This pool uses Java reflection API to create multiple
IoProcessorinstances. It tries to instantiate the processor in the following order:- A public constructor with one
ExecutorServiceparameter. - A public constructor with one
Executorparameter. - A public default constructor
// 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();
-
-
Field Summary
Fields Modifier and Type Field Description private booleancreatedExecutorA flag set to true if we had to create an executorprivate static intDEFAULT_SIZEThe default pool size, when no size is provided.private java.lang.ObjectdisposalLockA lock to protect the disposal against concurrent callsprivate booleandisposedA flag set to true if all the IoProcessor contained in the pool have been disposedprivate booleandisposingA flg set to true if the IoProcessor in the pool are being disposedprivate java.util.concurrent.ExecutorexecutorThe contained which is passed to the IoProcessor when they are createdprivate static org.slf4j.LoggerLOGGERA logger for this classprivate IoProcessor<S>[]poolThe pool tableprivate static AttributeKeyPROCESSORA key used to store the processor pool in the session's Attributes
-
Constructor Summary
Constructors Constructor 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, int size)Creates a new instance of SimpleIoProcessorPool with a defined number of IoProcessors in the poolSimpleIoProcessorPool(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 poolSimpleIoProcessorPool(java.lang.Class<? extends IoProcessor<S>> processorType, java.util.concurrent.Executor executor)Creates a new instance of SimpleIoProcessorPool with an executorSimpleIoProcessorPool(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
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(S session)Adds the specifiedsessionto the I/O processor so that the I/O processor starts to perform any I/O operations related with thesession.voiddispose()Releases any resources allocated by this processor.voidflush(S session)Flushes the internal write request queue of the specifiedsession.private IoProcessor<S>getProcessor(S session)Find the processor associated to a session.booleanisDisposed()booleanisDisposing()voidremove(S session)Removes and closes the specifiedsessionfrom the I/O processor so that the I/O processor closes the connection associated with thesessionand releases any other related resources.voidupdateTrafficControl(S session)Controls the traffic of the specifiedsessiondepending of theIoSession.isReadSuspended()andIoSession.isWriteSuspended()flagsvoidwrite(S session, WriteRequest writeRequest)Writes the WriteRequest for the specifiedsession.
-
-
-
Field Detail
-
LOGGER
private static final org.slf4j.Logger LOGGER
A logger for this class
-
DEFAULT_SIZE
private static final int DEFAULT_SIZE
The default pool size, when no size is provided.
-
PROCESSOR
private static final AttributeKey PROCESSOR
A key used to store the processor pool in the session's Attributes
-
pool
private final IoProcessor<S extends AbstractIoSession>[] pool
The pool table
-
executor
private final java.util.concurrent.Executor executor
The contained which is passed to the IoProcessor when they are created
-
createdExecutor
private final boolean createdExecutor
A flag set to true if we had to create an executor
-
disposalLock
private final java.lang.Object disposalLock
A lock to protect the disposal against concurrent calls
-
disposing
private volatile boolean disposing
A flg set to true if the IoProcessor in the pool are being disposed
-
disposed
private volatile boolean disposed
A flag set to true if all the IoProcessor contained in the pool have been disposed
-
-
Constructor Detail
-
SimpleIoProcessorPool
public SimpleIoProcessorPool(java.lang.Class<? extends IoProcessor<S>> processorType)
Creates a new instance of SimpleIoProcessorPool with a default size of NbCPUs +1.- Parameters:
processorType- The type of IoProcessor to use
-
SimpleIoProcessorPool
public 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- Parameters:
processorType- The type of IoProcessor to usesize- The number of IoProcessor in the pool
-
SimpleIoProcessorPool
public 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- Parameters:
processorType- The type of IoProcessor to usesize- The number of IoProcessor in the poolselectorProvider- The SelectorProvider to use
-
SimpleIoProcessorPool
public SimpleIoProcessorPool(java.lang.Class<? extends IoProcessor<S>> processorType, java.util.concurrent.Executor executor)
Creates a new instance of SimpleIoProcessorPool with an executor- Parameters:
processorType- The type of IoProcessor to useexecutor- TheExecutor
-
SimpleIoProcessorPool
public 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- Parameters:
processorType- The type of IoProcessor to useexecutor- TheExecutorsize- The number of IoProcessor in the poolselectorProvider- The SelectorProvider to used
-
-
Method Detail
-
add
public final void add(S session)
Adds the specifiedsessionto the I/O processor so that the I/O processor starts to perform any I/O operations related with thesession.- Specified by:
addin interfaceIoProcessor<S extends AbstractIoSession>- Parameters:
session- The added session
-
flush
public final void flush(S session)
Flushes the internal write request queue of the specifiedsession.- Specified by:
flushin interfaceIoProcessor<S extends AbstractIoSession>- Parameters:
session- The session we want the message to be written
-
write
public final void write(S session, WriteRequest writeRequest)
Writes the WriteRequest for the specifiedsession.- Specified by:
writein interfaceIoProcessor<S extends AbstractIoSession>- Parameters:
session- The session we want the message to be writtenwriteRequest- the WriteRequest to write
-
remove
public final void remove(S session)
Removes and closes the specifiedsessionfrom the I/O processor so that the I/O processor closes the connection associated with thesessionand releases any other related resources.- Specified by:
removein interfaceIoProcessor<S extends AbstractIoSession>- Parameters:
session- The session to be removed
-
updateTrafficControl
public final void updateTrafficControl(S session)
Controls the traffic of the specifiedsessiondepending of theIoSession.isReadSuspended()andIoSession.isWriteSuspended()flags- Specified by:
updateTrafficControlin interfaceIoProcessor<S extends AbstractIoSession>- Parameters:
session- The session to be updated
-
isDisposed
public boolean isDisposed()
- Specified by:
isDisposedin interfaceIoProcessor<S extends AbstractIoSession>- Returns:
trueif and if only all resources of this processor have been disposed.
-
isDisposing
public boolean isDisposing()
- Specified by:
isDisposingin interfaceIoProcessor<S extends AbstractIoSession>- Returns:
trueif and if onlyIoProcessor.dispose()method has been called. Please note that this method will returntrueeven after all the related resources are released.
-
dispose
public final void dispose()
Releases any resources allocated by this processor. Please note that the resources might not be released as long as there are any sessions managed by this processor. Most implementations will close all sessions immediately and release the related resources.- Specified by:
disposein interfaceIoProcessor<S extends AbstractIoSession>
-
getProcessor
private IoProcessor<S> getProcessor(S session)
Find the processor associated to a session. If it hasen't be stored into the session's attributes, pick a new processor and stores it.
-
-