Class ChannelContext<S extends java.nio.channels.SelectableChannel & java.nio.channels.NetworkChannel>

java.lang.Object
org.elasticsearch.nio.ChannelContext<S>
Type Parameters:
S - the type of channel
Direct Known Subclasses:
ServerChannelContext, SocketChannelContext

public abstract class ChannelContext<S extends java.nio.channels.SelectableChannel & java.nio.channels.NetworkChannel>
extends java.lang.Object
Implements the logic related to interacting with a java.nio channel. For example: registering with a selector, managing the selection key, closing, etc is implemented by this class or its subclasses.
  • Field Summary

    Fields 
    Modifier and Type Field Description
    protected S rawChannel  
  • Method Summary

    Modifier and Type Method Description
    void addCloseListener​(java.util.function.BiConsumer<java.lang.Void,​java.lang.Exception> listener)
    Add a listener that will be called when the channel is closed.
    protected void channelActive()  
    abstract void closeChannel()
    Schedules a channel to be closed by the selector event loop with which it is registered.
    void closeFromSelector()
    This method cleans up any context resources that need to be released when a channel is closed.
    abstract NioChannel getChannel()  
    protected java.nio.channels.SelectionKey getSelectionKey()  
    abstract NioSelector getSelector()  
    protected void handleException​(java.lang.Exception e)  
    boolean isOpen()  
    protected void register()  
    void setSelectionKey​(java.nio.channels.SelectionKey selectionKey)  

    Methods inherited from class java.lang.Object

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

    • rawChannel

      protected final S extends java.nio.channels.SelectableChannel & java.nio.channels.NetworkChannel rawChannel
  • Method Details

    • register

      protected void register() throws java.io.IOException
      Throws:
      java.io.IOException
    • channelActive

      protected void channelActive() throws java.io.IOException
      Throws:
      java.io.IOException
    • getSelectionKey

      protected java.nio.channels.SelectionKey getSelectionKey()
    • setSelectionKey

      public void setSelectionKey​(java.nio.channels.SelectionKey selectionKey)
    • closeFromSelector

      public void closeFromSelector() throws java.io.IOException
      This method cleans up any context resources that need to be released when a channel is closed. It should only be called by the selector thread.
      Throws:
      java.io.IOException - during channel / context close
    • addCloseListener

      public void addCloseListener​(java.util.function.BiConsumer<java.lang.Void,​java.lang.Exception> listener)
      Add a listener that will be called when the channel is closed.
      Parameters:
      listener - to be called
    • isOpen

      public boolean isOpen()
    • handleException

      protected void handleException​(java.lang.Exception e)
    • closeChannel

      public abstract void closeChannel()
      Schedules a channel to be closed by the selector event loop with which it is registered. If the channel is open and the state can be transitioned to closed, the close operation will be scheduled with the event loop. Depending on the underlying protocol of the channel, a close operation might simply close the socket channel or may involve reading and writing messages.
    • getSelector

      public abstract NioSelector getSelector()
    • getChannel

      public abstract NioChannel getChannel()