Class EventHandler


  • public class EventHandler
    extends java.lang.Object
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.util.function.Consumer<java.lang.Exception> exceptionHandler  
    • Constructor Summary

      Constructors 
      Constructor Description
      EventHandler​(java.util.function.Consumer<java.lang.Exception> exceptionHandler, java.util.function.Supplier<NioSelector> selectorSupplier)  
    • Method Summary

      Modifier and Type Method Description
      protected void acceptChannel​(ServerChannelContext context)
      This method is called when a server channel signals it is ready to accept a connection.
      protected void acceptException​(ServerChannelContext context, java.lang.Exception exception)
      This method is called when an attempt to accept a connection throws an exception.
      protected void closeException​(ChannelContext<?> context, java.lang.Exception exception)
      This method is called when an attempt to close a channel throws an exception.
      protected void connectException​(SocketChannelContext context, java.lang.Exception exception)
      This method is called when an attempt to connect a channel throws an exception.
      protected void genericChannelException​(ChannelContext<?> context, java.lang.Exception exception)
      This method is called when handling an event from a channel fails due to an unexpected exception.
      protected void handleClose​(ChannelContext<?> context)
      This method handles the closing of an NioChannel
      protected void handleConnect​(SocketChannelContext context)
      This method is called when a NioSocketChannel has just been accepted or if it has receive an OP_CONNECT event.
      protected void handleRead​(SocketChannelContext context)
      This method is called when a channel signals it is ready for be read.
      protected void handleRegistration​(ChannelContext<?> context)
      This method is called when a NioChannel is being registered with the selector.
      protected void handleTask​(java.lang.Runnable task)
      This method is called when a task or listener attached to a channel is available to run.
      protected void handleWrite​(SocketChannelContext context)
      This method is called when a channel signals it is ready to receive writes.
      protected void postHandling​(SocketChannelContext context)
      This method is called after events (READ, WRITE, CONNECT) have been handled for a channel.
      protected void readException​(SocketChannelContext context, java.lang.Exception exception)
      This method is called when an attempt to read from a channel throws an exception.
      protected void registrationException​(ChannelContext<?> context, java.lang.Exception exception)
      This method is called when an attempt to register a channel throws an exception.
      protected void selectorException​(java.io.IOException exception)
      This method handles an IOException that was thrown during a call to Selector.select(long) or Selector.close().
      protected void taskException​(java.lang.Exception exception)
      This method is called when a task or listener attached to a channel operation throws an exception.
      protected void uncaughtException​(java.lang.Exception exception)
      This method handles an exception that was uncaught during a select loop.
      protected void writeException​(SocketChannelContext context, java.lang.Exception exception)
      This method is called when an attempt to write to a channel throws an exception.
      • Methods inherited from class java.lang.Object

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

      • exceptionHandler

        protected final java.util.function.Consumer<java.lang.Exception> exceptionHandler
    • Constructor Detail

      • EventHandler

        public EventHandler​(java.util.function.Consumer<java.lang.Exception> exceptionHandler,
                            java.util.function.Supplier<NioSelector> selectorSupplier)
    • Method Detail

      • acceptChannel

        protected void acceptChannel​(ServerChannelContext context)
                              throws java.io.IOException
        This method is called when a server channel signals it is ready to accept a connection. All of the accept logic should occur in this call.
        Parameters:
        context - that can accept a connection
        Throws:
        java.io.IOException
      • acceptException

        protected void acceptException​(ServerChannelContext context,
                                       java.lang.Exception exception)
        This method is called when an attempt to accept a connection throws an exception.
        Parameters:
        context - that accepting a connection
        exception - that occurred
      • handleRegistration

        protected void handleRegistration​(ChannelContext<?> context)
                                   throws java.io.IOException
        This method is called when a NioChannel is being registered with the selector. It should only be called once per channel.
        Parameters:
        context - that was registered
        Throws:
        java.io.IOException
      • registrationException

        protected void registrationException​(ChannelContext<?> context,
                                             java.lang.Exception exception)
        This method is called when an attempt to register a channel throws an exception.
        Parameters:
        context - that was registered
        exception - that occurred
      • handleConnect

        protected void handleConnect​(SocketChannelContext context)
                              throws java.io.IOException
        This method is called when a NioSocketChannel has just been accepted or if it has receive an OP_CONNECT event.
        Parameters:
        context - that was registered
        Throws:
        java.io.IOException
      • connectException

        protected void connectException​(SocketChannelContext context,
                                        java.lang.Exception exception)
        This method is called when an attempt to connect a channel throws an exception.
        Parameters:
        context - that was connecting
        exception - that occurred
      • handleRead

        protected void handleRead​(SocketChannelContext context)
                           throws java.io.IOException
        This method is called when a channel signals it is ready for be read. All of the read logic should occur in this call.
        Parameters:
        context - that can be read
        Throws:
        java.io.IOException
      • readException

        protected void readException​(SocketChannelContext context,
                                     java.lang.Exception exception)
        This method is called when an attempt to read from a channel throws an exception.
        Parameters:
        context - that was being read
        exception - that occurred
      • handleWrite

        protected void handleWrite​(SocketChannelContext context)
                            throws java.io.IOException
        This method is called when a channel signals it is ready to receive writes. All of the write logic should occur in this call.
        Parameters:
        context - that can be written to
        Throws:
        java.io.IOException
      • writeException

        protected void writeException​(SocketChannelContext context,
                                      java.lang.Exception exception)
        This method is called when an attempt to write to a channel throws an exception.
        Parameters:
        context - that was being written to
        exception - that occurred
      • handleTask

        protected void handleTask​(java.lang.Runnable task)
        This method is called when a task or listener attached to a channel is available to run.
        Parameters:
        task - to handle
      • taskException

        protected void taskException​(java.lang.Exception exception)
        This method is called when a task or listener attached to a channel operation throws an exception.
        Parameters:
        exception - that occurred
      • postHandling

        protected void postHandling​(SocketChannelContext context)
        This method is called after events (READ, WRITE, CONNECT) have been handled for a channel.
        Parameters:
        context - that was handled
      • selectorException

        protected void selectorException​(java.io.IOException exception)
        This method handles an IOException that was thrown during a call to Selector.select(long) or Selector.close().
        Parameters:
        exception - the exception
      • uncaughtException

        protected void uncaughtException​(java.lang.Exception exception)
        This method handles an exception that was uncaught during a select loop.
        Parameters:
        exception - that was uncaught
      • handleClose

        protected void handleClose​(ChannelContext<?> context)
                            throws java.io.IOException
        This method handles the closing of an NioChannel
        Parameters:
        context - that should be closed
        Throws:
        java.io.IOException
      • closeException

        protected void closeException​(ChannelContext<?> context,
                                      java.lang.Exception exception)
        This method is called when an attempt to close a channel throws an exception.
        Parameters:
        context - that was being closed
        exception - that occurred
      • genericChannelException

        protected void genericChannelException​(ChannelContext<?> context,
                                               java.lang.Exception exception)
        This method is called when handling an event from a channel fails due to an unexpected exception. An example would be if checking ready ops on a SelectionKey threw CancelledKeyException.
        Parameters:
        context - that caused the exception
        exception - that was thrown