Class Reactor


  • public class Reactor
    extends Object
    Implementation of reactor pattern using java.nio.channels.Selector.
    Version:
    $id$
    Author:
    Matej Sekoranja
    • Constructor Detail

    • Method Detail

      • process

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

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

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

        public void disableSelectionKey​(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​(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​(AbstractSelectableChannel channel,
                                   int interestOps)
        Change SelectionKey operations of interest.
        Parameters:
        channel -
        interestOps -
      • shutdown

        public void shutdown()
        Shutdown the reactor.
      • isShutdown

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