Package org.elasticsearch.nio
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.ObjectImplements 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 SrawChannel
-
Method Summary
Modifier and Type Method Description voidaddCloseListener(java.util.function.BiConsumer<java.lang.Void,java.lang.Exception> listener)Add a listener that will be called when the channel is closed.abstract voidcloseChannel()Schedules a channel to be closed by the selector event loop with which it is registered.voidcloseFromSelector()This method cleans up any context resources that need to be released when a channel is closed.abstract NioChannelgetChannel()abstract NioSelectorgetSelector()protected voidhandleException(java.lang.Exception e)booleanisOpen()protected voidregister()protected voidsetSelectionKey(java.nio.channels.SelectionKey selectionKey)
-
-
-
Field Detail
-
rawChannel
protected final S extends java.nio.channels.SelectableChannel & java.nio.channels.NetworkChannel rawChannel
-
-
Method Detail
-
register
protected void register() throws java.io.IOException- Throws:
java.io.IOException
-
setSelectionKey
protected void setSelectionKey(java.nio.channels.SelectionKey selectionKey)
-
closeFromSelector
public void closeFromSelector() throws java.io.IOExceptionThis 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()
-
-