Package org.elasticsearch.nio
Class NioSelector
- java.lang.Object
-
- org.elasticsearch.nio.NioSelector
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
public class NioSelector extends java.lang.Object implements java.io.CloseableThis is a nio selector implementation. This selector wraps a raw nioSelector. When you callrunLoop(), the selector will run untilclose()is called. This instance handles closing of channels. Users should callqueueChannelClose(NioChannel)to schedule a channel for close by this selector.
-
-
Constructor Summary
Constructors Constructor Description NioSelector(EventHandler eventHandler)NioSelector(EventHandler eventHandler, java.nio.channels.Selector selector)
-
Method Summary
Modifier and Type Method Description voidassertOnSelectorThread()voidclose()<V> voidexecuteFailedListener(java.util.function.BiConsumer<V,java.lang.Exception> listener, java.lang.Exception exception)Executes a failed listener with consistent exception handling.<V> voidexecuteListener(java.util.function.BiConsumer<V,java.lang.Exception> listener, V value)Executes a success listener with consistent exception handling.java.nio.ByteBuffergetIoBuffer()Returns a cached direct byte buffer for network operations.TaskSchedulergetTaskScheduler()booleanisOnCurrentThread()booleanisOpen()booleanisRunning()voidqueueChannelClose(NioChannel channel)voidqueueWrite(WriteOperation writeOperation)Queues a write operation to be handled by the event loop.java.nio.channels.SelectorrawSelector()voidrunLoop()Starts this selector.voidscheduleForRegistration(NioChannel channel)Schedules a NioChannel to be registered with this selector.
-
-
-
Constructor Detail
-
NioSelector
public NioSelector(EventHandler eventHandler) throws java.io.IOException
- Throws:
java.io.IOException
-
NioSelector
public NioSelector(EventHandler eventHandler, java.nio.channels.Selector selector)
-
-
Method Detail
-
getIoBuffer
public java.nio.ByteBuffer getIoBuffer()
Returns a cached direct byte buffer for network operations. It is cleared on every get call.- Returns:
- the byte buffer
-
getTaskScheduler
public TaskScheduler getTaskScheduler()
-
rawSelector
public java.nio.channels.Selector rawSelector()
-
isOpen
public boolean isOpen()
-
isRunning
public boolean isRunning()
-
isOnCurrentThread
public boolean isOnCurrentThread()
-
assertOnSelectorThread
public void assertOnSelectorThread()
-
runLoop
public void runLoop()
Starts this selector. The selector will run untilclose()is called.
-
close
public void close() throws java.io.IOException- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Throws:
java.io.IOException
-
queueWrite
public void queueWrite(WriteOperation writeOperation)
Queues a write operation to be handled by the event loop. This can be called by any thread and is the api available for non-selector threads to schedule writes. When invoked from the selector thread the write will be executed right away.- Parameters:
writeOperation- to be queued
-
queueChannelClose
public void queueChannelClose(NioChannel channel)
-
scheduleForRegistration
public void scheduleForRegistration(NioChannel channel)
Schedules a NioChannel to be registered with this selector. The channel will by queued and eventually registered next time through the event loop.- Parameters:
channel- to register
-
executeListener
public <V> void executeListener(java.util.function.BiConsumer<V,java.lang.Exception> listener, V value)Executes a success listener with consistent exception handling. This can only be called from current selector thread.- Parameters:
listener- to be executedvalue- to provide to listener
-
executeFailedListener
public <V> void executeFailedListener(java.util.function.BiConsumer<V,java.lang.Exception> listener, java.lang.Exception exception)Executes a failed listener with consistent exception handling. This can only be called from current selector thread.- Parameters:
listener- to be executedexception- to provide to listener
-
-