Package org.omegazero.net.common
Class SelectorHandler
java.lang.Object
org.omegazero.net.common.SelectorHandler
- Direct Known Subclasses:
ConnectionSelectorHandler
Wraps a java.nio
A thread must call
This class defines the following system properties:
Selector
.A thread must call
initSelector()
followed by runSelectorLoop()
to start selecting keys, which can then be handled in the abstract method
handleSelectedKey(SelectionKey)
. Additional keys may be added safely while running using registerChannel(SelectableChannel, int, Object)
.This class defines the following system properties:
org.omegazero.net.nioselector.rebuildThreshold
(integer, default 1024) - The maximum number of times the selection operation may return in a row without having any keys selected. If this threshold is exceeded, the selector will be rebuilt. This feature exists to mitigate the selector/epoll immediate return bug, which may exist on older JVM distributionsorg.omegazero.net.nioselector.maxRebuilds
(integer, default 8) - The maximum number of times the selector may be rebuilt in a row (see above). If this threshold is exceeded, the selector loop will exit with anIOException
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected void
Stops the selector loop, closes all channels registered with this selector and closes theSelector
instance.protected abstract void
Called when a key was selected in a select call in therunSelectorLoop()
method.protected void
Initializes theSelector
and sets this instance as running.
After this method has been called successfully,SelectorHandler#selectorLoop()
should be called to start performing IO operations.boolean
protected void
Called every loop iteration inrunSelectorLoop()
, which is every timeSelector.select()
returns, either because aSelectionKey
was selected or the selector was woken up for any other reason (for example after a call toselectorWakeup()
).
This method does nothing by default.protected SelectionKey
registerChannel
(SelectableChannel channel, int ops) protected SelectionKey
registerChannel
(SelectableChannel channel, int ops, Object attachment) Registers the given channel with thisSelectorHandler
'sSelector
.protected void
Runs the loop that continuously selects channels using theSelector
.
Will not return untilcloseSelector()
is called.protected Set<SelectionKey>
protected void
Wakes up the selector, causing a selection loop iteration.
-
Constructor Details
-
SelectorHandler
public SelectorHandler()
-
-
Method Details
-
handleSelectedKey
Called when a key was selected in a select call in therunSelectorLoop()
method.- Parameters:
key
- The selected key- Throws:
IOException
- If an IO error occurs
-
loopIteration
Called every loop iteration inrunSelectorLoop()
, which is every timeSelector.select()
returns, either because aSelectionKey
was selected or the selector was woken up for any other reason (for example after a call toselectorWakeup()
).
This method does nothing by default. Subclasses may override this method to perform additional required operations.- Throws:
IOException
- If an IO error occurs
-
initSelector
Initializes theSelector
and sets this instance as running.
After this method has been called successfully,SelectorHandler#selectorLoop()
should be called to start performing IO operations.- Throws:
IOException
-
closeSelector
Stops the selector loop, closes all channels registered with this selector and closes theSelector
instance.- Throws:
IOException
-
registerChannel
SeeregisterChannel(SelectableChannel, int, Object)
.
An invocation of this method of the formregisterChannel(channel, ops)
is exactly the same as the invocationregisterChannel
(channel, ops, null)- Parameters:
channel
- The channel to be registeredops
- The interest set for the resulting key- Returns:
- The resulting
SelectionKey
- Throws:
IOException
-
registerChannel
protected SelectionKey registerChannel(SelectableChannel channel, int ops, Object attachment) throws IOException Registers the given channel with thisSelectorHandler
'sSelector
. This function is safe to be called while another thread is in a selection operation in#selectorLoop()
, in which case the selection operation will be interrupted to allow the new channel to be registered.- Parameters:
channel
- The channel to be registeredops
- The interest set for the resulting keyattachment
- The attachment for the resulting key- Returns:
- The resulting
SelectionKey
- Throws:
IOException
- See Also:
-
selectorWakeup
protected void selectorWakeup()Wakes up the selector, causing a selection loop iteration.- See Also:
-
selectorKeys
- Returns:
- The set of registered
SelectionKey
s. SeeSelector.keys()
-
runSelectorLoop
Runs the loop that continuously selects channels using theSelector
.
Will not return untilcloseSelector()
is called. IfinitSelector()
was never called, this method returns immediately.- Throws:
IOException
- If an IO error occurs, or ifhandleSelectedKey(SelectionKey)
orloopIteration()
throw anIOException
-
isRunning
public boolean isRunning()- Returns:
true
when thisSelectorHandler
is running; this is the case in the time between calls toinitSelector()
andcloseSelector()
-