Class NioSelectorGroup

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, NioGroup

    public class NioSelectorGroup
    extends java.lang.Object
    implements NioGroup
    The NioSelectorGroup is a group of selectors for interfacing with java nio. When it is started it will create the configured number of selectors. Each selector will be running in a dedicated thread. Server connections can be bound using the bindServerChannel(InetSocketAddress, ChannelFactory) method. Client connections can be opened using the openChannel(InetSocketAddress, ChannelFactory) method.

    The logic specific to a particular channel is provided by the ChannelFactory passed to the method when the channel is created. This is what allows an NioSelectorGroup to support different channel types.

    • Constructor Summary

      Constructors 
      Constructor Description
      NioSelectorGroup​(java.util.concurrent.ThreadFactory acceptorThreadFactory, int dedicatedAcceptorCount, java.util.concurrent.ThreadFactory selectorThreadFactory, int selectorCount, java.util.function.Function<java.util.function.Supplier<NioSelector>,​EventHandler> eventHandlerFunction)
      This will create an NioSelectorGroup with dedicated acceptors.
      NioSelectorGroup​(java.util.concurrent.ThreadFactory threadFactory, int selectorCount, java.util.function.Function<java.util.function.Supplier<NioSelector>,​EventHandler> eventHandlerFunction)
      This will create an NioSelectorGroup with no dedicated acceptors.
    • Method Summary

      Modifier and Type Method Description
      <S extends NioServerSocketChannel>
      S
      bindServerChannel​(java.net.InetSocketAddress address, ChannelFactory<S,​?> factory)
      Opens and binds a server channel to accept incoming connections.
      void close()  
      <S extends NioSocketChannel>
      S
      openChannel​(java.net.InetSocketAddress address, ChannelFactory<?,​S> factory)
      Opens a outgoing client channel.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • NioSelectorGroup

        public NioSelectorGroup​(java.util.concurrent.ThreadFactory threadFactory,
                                int selectorCount,
                                java.util.function.Function<java.util.function.Supplier<NioSelector>,​EventHandler> eventHandlerFunction)
                         throws java.io.IOException
        This will create an NioSelectorGroup with no dedicated acceptors. All server channels will be handled by the same selectors that are handling child channels.
        Parameters:
        threadFactory - factory to create selector threads
        selectorCount - the number of selectors to be created
        eventHandlerFunction - function for creating event handlers
        Throws:
        java.io.IOException - occurs if there is a problem while opening a java.nio.Selector
      • NioSelectorGroup

        public NioSelectorGroup​(java.util.concurrent.ThreadFactory acceptorThreadFactory,
                                int dedicatedAcceptorCount,
                                java.util.concurrent.ThreadFactory selectorThreadFactory,
                                int selectorCount,
                                java.util.function.Function<java.util.function.Supplier<NioSelector>,​EventHandler> eventHandlerFunction)
                         throws java.io.IOException
        This will create an NioSelectorGroup with dedicated acceptors. All server channels will be handled by a group of selectors dedicated to accepting channels. These accepted channels will be handed off the non-server selectors.
        Parameters:
        acceptorThreadFactory - factory to create acceptor selector threads
        dedicatedAcceptorCount - the number of dedicated acceptor selectors to be created
        selectorThreadFactory - factory to create non-acceptor selector threads
        selectorCount - the number of non-acceptor selectors to be created
        eventHandlerFunction - function for creating event handlers
        Throws:
        java.io.IOException - occurs if there is a problem while opening a java.nio.Selector
    • Method Detail

      • bindServerChannel

        public <S extends NioServerSocketChannel> S bindServerChannel​(java.net.InetSocketAddress address,
                                                                      ChannelFactory<S,​?> factory)
                                                               throws java.io.IOException
        Description copied from interface: NioGroup
        Opens and binds a server channel to accept incoming connections.
        Specified by:
        bindServerChannel in interface NioGroup
        Throws:
        java.io.IOException
      • openChannel

        public <S extends NioSocketChannel> S openChannel​(java.net.InetSocketAddress address,
                                                          ChannelFactory<?,​S> factory)
                                                   throws java.io.IOException
        Description copied from interface: NioGroup
        Opens a outgoing client channel.
        Specified by:
        openChannel in interface NioGroup
        Throws:
        java.io.IOException
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Specified by:
        close in interface NioGroup
        Throws:
        java.io.IOException