Class Reactor


  • public class Reactor
    extends java.lang.Object
    Implementation of reactor pattern using java.nio.channels.Selector.
    • Constructor Summary

      Constructors 
      Constructor Description
      Reactor()
      Creates a new instance of reactor.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void disableSelectionKey​(java.nio.channels.SelectionKey key)
      Disable selection key - sets its interest ops to 0 and stores its current interest ops (to be restored).
      void enableSelectionKey​(java.nio.channels.SelectionKey key)
      Enable selection key - restore its interest ops.
      boolean isShutdown()
      Get shutdown status.
      boolean process()
      Process requests.
      java.nio.channels.SelectionKey register​(java.nio.channels.SelectableChannel selectableChannel, int interestOps, ReactorHandler handler)
      Registers SelectableChannel to the reactor.
      void setInterestOps​(java.nio.channels.spi.AbstractSelectableChannel channel, int interestOps)
      Change SelectionKey operations of interest.
      void shutdown()
      Shutdown the reactor.
      void unregisterAndClose​(java.nio.channels.SelectableChannel selectableChannel)
      Unregisters SelectableChannel from the reactor.
      • Methods inherited from class java.lang.Object

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

      • Reactor

        public Reactor()
                throws java.io.IOException
        Creates a new instance of reactor.
        Throws:
        java.io.IOException - IO Exception
    • Method Detail

      • process

        public boolean process()
        Process requests. NOTE: this method has to be called until false is returned.
        Returns:
        true if selector is still active, false when shutdown.
      • unregisterAndClose

        public void unregisterAndClose​(java.nio.channels.SelectableChannel selectableChannel)
        Unregisters SelectableChannel from the reactor.
        Parameters:
        selectableChannel - channel to be unregistered.
      • register

        public java.nio.channels.SelectionKey register​(java.nio.channels.SelectableChannel selectableChannel,
                                                       int interestOps,
                                                       ReactorHandler handler)
                                                throws java.nio.channels.ClosedChannelException
        Registers SelectableChannel to the reactor.
        Parameters:
        selectableChannel - channel to be registered.
        interestOps - operations supported by channel (i.e. operations of interest).
        handler - handle to process requests.
        Returns:
        selector selection key.
        Throws:
        java.nio.channels.ClosedChannelException - Channel closed
      • disableSelectionKey

        public void disableSelectionKey​(java.nio.channels.SelectionKey key)
        Disable selection key - sets its interest ops to 0 and stores its current interest ops (to be restored). NOTE: to be called only (otherwise it can block) when select is not pending
        Parameters:
        key - selection key to be disabled.
      • enableSelectionKey

        public void enableSelectionKey​(java.nio.channels.SelectionKey key)
        Enable selection key - restore its interest ops. NOTE: can be called when select is pending
        Parameters:
        key - selection key to be enabled.
      • setInterestOps

        public void setInterestOps​(java.nio.channels.spi.AbstractSelectableChannel channel,
                                   int interestOps)
        Change SelectionKey operations of interest.
        Parameters:
        channel - channel
        interestOps - interest ops
      • shutdown

        public void shutdown()
        Shutdown the reactor.
      • isShutdown

        public boolean isShutdown()
        Get shutdown status.
        Returns:
        shutdown status.