Class NioSelector

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

    public class NioSelector
    extends java.lang.Object
    implements java.io.Closeable
    This is a nio selector implementation. This selector wraps a raw nio Selector. When you call runLoop(), the selector will run until close() is called. This instance handles closing of channels. Users should call queueChannelClose(NioChannel) to schedule a channel for close by this selector.
    • Constructor Detail

      • NioSelector

        public NioSelector​(EventHandler eventHandler)
                    throws java.io.IOException
        Throws:
        java.io.IOException
      • NioSelector

        public NioSelector​(EventHandler eventHandler,
                           java.nio.channels.Selector selector)
    • Method Detail

      • getIoBuffer

        public java.nio.ByteBuffer getIoBuffer()
        Returns a cached direct byte buffer for network operations. It is cleared on every get call.
        Returns:
        the byte buffer
      • rawSelector

        public java.nio.channels.Selector rawSelector()
      • isOpen

        public boolean isOpen()
      • isRunning

        public boolean isRunning()
      • isOnCurrentThread

        public boolean isOnCurrentThread()
      • assertOnSelectorThread

        public void assertOnSelectorThread()
      • runLoop

        public void runLoop()
        Starts this selector. The selector will run until close() is called.
      • close

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

        public void queueWrite​(WriteOperation writeOperation)
        Queues a write operation to be handled by the event loop. This can be called by any thread and is the api available for non-selector threads to schedule writes. When invoked from the selector thread the write will be executed right away.
        Parameters:
        writeOperation - to be queued
      • queueChannelClose

        public void queueChannelClose​(NioChannel channel)
      • scheduleForRegistration

        public void scheduleForRegistration​(NioChannel channel)
        Schedules a NioChannel to be registered with this selector. The channel will by queued and eventually registered next time through the event loop.
        Parameters:
        channel - to register
      • executeListener

        public <V> void executeListener​(java.util.function.BiConsumer<V,​java.lang.Exception> listener,
                                        V value)
        Executes a success listener with consistent exception handling. This can only be called from current selector thread.
        Parameters:
        listener - to be executed
        value - to provide to listener
      • executeFailedListener

        public <V> void executeFailedListener​(java.util.function.BiConsumer<V,​java.lang.Exception> listener,
                                              java.lang.Exception exception)
        Executes a failed listener with consistent exception handling. This can only be called from current selector thread.
        Parameters:
        listener - to be executed
        exception - to provide to listener