Class NioServerSocketChannelFactory

  • All Implemented Interfaces:
    ChannelFactory, ServerChannelFactory, ServerSocketChannelFactory, ExternalResourceReleasable

    public class NioServerSocketChannelFactory
    extends java.lang.Object
    implements ServerSocketChannelFactory
    A ServerSocketChannelFactory which creates a server-side NIO-based ServerSocketChannel. It utilizes the non-blocking I/O mode which was introduced with NIO to serve many number of concurrent connections efficiently.

    How threads work

    There are two types of threads in a NioServerSocketChannelFactory; one is boss thread and the other is worker thread.

    Boss threads

    Each bound ServerSocketChannel has its own boss thread. For example, if you opened two server ports such as 80 and 443, you will have two boss threads. A boss thread accepts incoming connections until the port is unbound. Once a connection is accepted successfully, the boss thread passes the accepted Channel to one of the worker threads that the NioServerSocketChannelFactory manages.

    Worker threads

    One NioServerSocketChannelFactory can have one or more worker threads. A worker thread performs non-blocking read and write for one or more Channels in a non-blocking mode.

    Life cycle of threads and graceful shutdown

    All threads are acquired from the Executors which were specified when a NioServerSocketChannelFactory was created. Boss threads are acquired from the bossExecutor, and worker threads are acquired from the workerExecutor. Therefore, you should make sure the specified Executors are able to lend the sufficient number of threads. It is the best bet to specify a cached thread pool.

    Both boss and worker threads are acquired lazily, and then released when there's nothing left to process. All the related resources such as Selector are also released when the boss and worker threads are released. Therefore, to shut down a service gracefully, you should do the following:

    1. unbind all channels created by the factory,
    2. close all child channels accepted by the unbound channels, and (these two steps so far is usually done using ChannelGroup.close())
    3. call releaseExternalResources().
    Please make sure not to shut down the executor until all channels are closed. Otherwise, you will end up with a RejectedExecutionException and the related resources might not be released properly.
    • Constructor Detail

      • NioServerSocketChannelFactory

        public NioServerSocketChannelFactory​(java.util.concurrent.Executor bossExecutor,
                                             java.util.concurrent.Executor workerExecutor)
        Creates a new instance. Calling this constructor is same with calling NioServerSocketChannelFactory(Executor, Executor, int) with the worker executor passed into getMaxThreads(Executor).
        Parameters:
        bossExecutor - the Executor which will execute the boss threads
        workerExecutor - the Executor which will execute the I/O worker threads
      • NioServerSocketChannelFactory

        public NioServerSocketChannelFactory​(java.util.concurrent.Executor bossExecutor,
                                             java.util.concurrent.Executor workerExecutor,
                                             int workerCount)
        Creates a new instance.
        Parameters:
        bossExecutor - the Executor which will execute the boss threads
        workerExecutor - the Executor which will execute the I/O worker threads
        workerCount - the maximum number of I/O worker threads
      • NioServerSocketChannelFactory

        public NioServerSocketChannelFactory​(java.util.concurrent.Executor bossExecutor,
                                             int bossCount,
                                             java.util.concurrent.Executor workerExecutor,
                                             int workerCount)
        Create a new instance.
        Parameters:
        bossExecutor - the Executor which will execute the boss threads
        bossCount - the number of boss threads
        workerExecutor - the Executor which will execute the I/O worker threads
        workerCount - the maximum number of I/O worker threads
      • NioServerSocketChannelFactory

        public NioServerSocketChannelFactory​(java.util.concurrent.Executor bossExecutor,
                                             WorkerPool<NioWorker> workerPool)
        Creates a new instance.
        Parameters:
        bossExecutor - the Executor which will execute the boss threads
        workerPool - the WorkerPool which will be used to obtain the NioWorker that execute the I/O worker threads
      • NioServerSocketChannelFactory

        public NioServerSocketChannelFactory​(java.util.concurrent.Executor bossExecutor,
                                             int bossCount,
                                             WorkerPool<NioWorker> workerPool)
        Create a new instance.
        Parameters:
        bossExecutor - the Executor which will execute the boss threads
        bossCount - the number of boss threads
        workerPool - the WorkerPool which will be used to obtain the NioWorker that execute the I/O worker threads
      • NioServerSocketChannelFactory

        public NioServerSocketChannelFactory​(BossPool<NioServerBoss> bossPool,
                                             WorkerPool<NioWorker> workerPool)
        Create a new instance.
        Parameters:
        bossPool - the BossPool which will be used to obtain the NioServerBoss that execute the I/O for accept new connections
        workerPool - the WorkerPool which will be used to obtain the NioWorker that execute the I/O worker threads
    • Method Detail

      • shutdown

        public void shutdown()
        Description copied from interface: ChannelFactory
        Shudown the ChannelFactory and all the resource it created internal.
        Specified by:
        shutdown in interface ChannelFactory
      • releaseExternalResources

        public void releaseExternalResources()
        Description copied from interface: ChannelFactory
        Releases the external resources that this factory depends on to function. An external resource is a resource that this factory didn't create by itself. For example, Executors that you specified in the factory constructor are external resources. You can call this method to release all external resources conveniently when the resources are not used by this factory or any other part of your application. An unexpected behavior will be resulted in if the resources are released when there's an open channel which is managed by this factory. This will also call ChannelFactory.shutdown() before do any action
        Specified by:
        releaseExternalResources in interface ChannelFactory
        Specified by:
        releaseExternalResources in interface ExternalResourceReleasable
      • releasePools

        private void releasePools()
      • getMaxThreads

        private static int getMaxThreads​(java.util.concurrent.Executor executor)
        Returns number of max threads for the NioWorkerPool to use. If the * Executor is a ThreadPoolExecutor, check its maximum * pool size and return either it's maximum or SelectorUtil.DEFAULT_IO_THREADS, whichever is lower. Note that SelectorUtil.DEFAULT_IO_THREADS is 2 * the number of available processors in the machine. The number of available processors is obtained by Runtime.availableProcessors().
        Parameters:
        executor - the Executor which will execute the I/O worker threads
        Returns:
        number of maximum threads the NioWorkerPool should use