Class SimpleIoProcessorPool<S extends AbstractIoSession>

java.lang.Object
org.apache.mina.core.service.SimpleIoProcessorPool<S>
Type Parameters:
S - the type of the IoSession to be managed by the specified IoProcessor.
All Implemented Interfaces:
IoProcessor<S>

public class SimpleIoProcessorPool<S extends AbstractIoSession> extends Object implements IoProcessor<S>
An 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:

  1. A public constructor with one ExecutorService parameter.
  2. A public constructor with one Executor parameter.
  3. A public default constructor
The following is an example for the NIO socket transport:
// 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 final boolean
    A flag set to true if we had to create an executor
    private static final int
    The default pool size, when no size is provided.
    private final Object
    A lock to protect the disposal against concurrent calls
    private boolean
    A flag set to true if all the IoProcessor contained in the pool have been disposed
    private boolean
    A flg set to true if the IoProcessor in the pool are being disposed
    private final Executor
    The contained which is passed to the IoProcessor when they are created
    private static final org.slf4j.Logger
    A logger for this class
    private final IoProcessor<S>[]
    The pool table
    private static final AttributeKey
    A key used to store the processor pool in the session's Attributes
  • Constructor Summary

    Constructors
    Constructor
    Description
    SimpleIoProcessorPool(Class<? extends IoProcessor<S>> processorType)
    Creates a new instance of SimpleIoProcessorPool with a default size of NbCPUs +1.
    SimpleIoProcessorPool(Class<? extends IoProcessor<S>> processorType, int size)
    Creates a new instance of SimpleIoProcessorPool with a defined number of IoProcessors in the pool
    SimpleIoProcessorPool(Class<? extends IoProcessor<S>> processorType, int size, SelectorProvider selectorProvider)
    Creates a new instance of SimpleIoProcessorPool with a defined number of IoProcessors in the pool
    SimpleIoProcessorPool(Class<? extends IoProcessor<S>> processorType, Executor executor)
    Creates a new instance of SimpleIoProcessorPool with an executor
    SimpleIoProcessorPool(Class<? extends IoProcessor<S>> processorType, Executor executor, int size, SelectorProvider selectorProvider)
    Creates a new instance of SimpleIoProcessorPool with an executor
  • Method Summary

    Modifier and Type
    Method
    Description
    final 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.
    final void
    Releases any resources allocated by this processor.
    final 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
    boolean
    final 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.
    final void
    Controls the traffic of the specified session depending of the IoSession.isReadSuspended() and IoSession.isWriteSuspended() flags
    final void
    write(S session, WriteRequest writeRequest)
    Writes the WriteRequest for the specified session.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • 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 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 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 Details

    • SimpleIoProcessorPool

      public SimpleIoProcessorPool(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(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 use
      size - The number of IoProcessor in the pool
    • SimpleIoProcessorPool

      public SimpleIoProcessorPool(Class<? extends IoProcessor<S>> processorType, int size, SelectorProvider selectorProvider)
      Creates a new instance of SimpleIoProcessorPool with a defined number of IoProcessors in the pool
      Parameters:
      processorType - The type of IoProcessor to use
      size - The number of IoProcessor in the pool
      selectorProvider - The SelectorProvider to use
    • SimpleIoProcessorPool

      public SimpleIoProcessorPool(Class<? extends IoProcessor<S>> processorType, Executor executor)
      Creates a new instance of SimpleIoProcessorPool with an executor
      Parameters:
      processorType - The type of IoProcessor to use
      executor - The Executor
    • SimpleIoProcessorPool

      public SimpleIoProcessorPool(Class<? extends IoProcessor<S>> processorType, Executor executor, int size, SelectorProvider selectorProvider)
      Creates a new instance of SimpleIoProcessorPool with an executor
      Parameters:
      processorType - The type of IoProcessor to use
      executor - The Executor
      size - The number of IoProcessor in the pool
      selectorProvider - The SelectorProvider to used
  • Method Details

    • add

      public final 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.
      Specified by:
      add in interface IoProcessor<S extends AbstractIoSession>
      Parameters:
      session - The added session
    • flush

      public final void flush(S session)
      Flushes the internal write request queue of the specified session.
      Specified by:
      flush in interface IoProcessor<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 specified session.
      Specified by:
      write in interface IoProcessor<S extends AbstractIoSession>
      Parameters:
      session - The session we want the message to be written
      writeRequest - the WriteRequest to write
    • remove

      public final 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.
      Specified by:
      remove in interface IoProcessor<S extends AbstractIoSession>
      Parameters:
      session - The session to be removed
    • updateTrafficControl

      public final void updateTrafficControl(S session)
      Controls the traffic of the specified session depending of the IoSession.isReadSuspended() and IoSession.isWriteSuspended() flags
      Specified by:
      updateTrafficControl in interface IoProcessor<S extends AbstractIoSession>
      Parameters:
      session - The session to be updated
    • isDisposed

      public boolean isDisposed()
      Specified by:
      isDisposed in interface IoProcessor<S extends AbstractIoSession>
      Returns:
      true if and if only all resources of this processor have been disposed.
    • isDisposing

      public boolean isDisposing()
      Specified by:
      isDisposing in interface IoProcessor<S extends AbstractIoSession>
      Returns:
      true if and if only IoProcessor.dispose() method has been called. Please note that this method will return true even 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:
      dispose in interface IoProcessor<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.