@ManagedObject(value="Manager of the NIO Selectors") public abstract class SelectorManager extends ContainerLifeCycle implements Dumpable
SelectorManager
manages a number of ManagedSelector
s that
simplify the non-blocking primitives provided by the JVM via the java.nio
package.
SelectorManager
subclasses implement methods to return protocol-specific
EndPoint
s and Connection
s.
AbstractLifeCycle.AbstractLifeCycleListener
Container.InheritedListener, Container.Listener
LifeCycle.Listener
Modifier and Type | Field and Description |
---|---|
private long |
_connectTimeout |
private ThreadPoolBudget.Lease |
_lease |
private java.util.concurrent.atomic.AtomicInteger |
_selectorIndex |
private java.util.function.IntUnaryOperator |
_selectorIndexUpdate |
private ManagedSelector[] |
_selectors |
static int |
DEFAULT_CONNECT_TIMEOUT |
private java.util.concurrent.Executor |
executor |
protected static Logger |
LOG |
private Scheduler |
scheduler |
FAILED, RUNNING, STARTED, STARTING, STOP_ON_FAILURE, STOPPED, STOPPING
Modifier | Constructor and Description |
---|---|
protected |
SelectorManager(java.util.concurrent.Executor executor,
Scheduler scheduler) |
protected |
SelectorManager(java.util.concurrent.Executor executor,
Scheduler scheduler,
int selectors) |
Modifier and Type | Method and Description |
---|---|
void |
accept(java.nio.channels.SelectableChannel channel) |
void |
accept(java.nio.channels.SelectableChannel channel,
java.lang.Object attachment)
Registers a channel to perform non-blocking read/write operations.
|
protected void |
accepted(java.nio.channels.SelectableChannel channel)
Callback method when a channel is accepted from the
ServerSocketChannel
passed to acceptor(SelectableChannel) . |
java.io.Closeable |
acceptor(java.nio.channels.SelectableChannel server)
Registers a server channel for accept operations.
|
private ManagedSelector |
chooseSelector() |
void |
connect(java.nio.channels.SelectableChannel channel,
java.lang.Object attachment)
Registers a channel to perform a non-blocking connect.
|
void |
connectionClosed(Connection connection)
Callback method invoked when a connection is closed.
|
protected void |
connectionFailed(java.nio.channels.SelectableChannel channel,
java.lang.Throwable ex,
java.lang.Object attachment)
Callback method invoked when a non-blocking connect cannot be completed.
|
void |
connectionOpened(Connection connection)
Callback method invoked when a connection is opened.
|
private static int |
defaultSelectors(java.util.concurrent.Executor executor) |
protected java.nio.channels.SelectableChannel |
doAccept(java.nio.channels.SelectableChannel server) |
protected boolean |
doFinishConnect(java.nio.channels.SelectableChannel channel) |
protected void |
doStart()
Starts the managed lifecycle beans in the order they were added.
|
protected void |
doStop()
Stops the managed lifecycle beans in the reverse order they were added.
|
protected void |
endPointClosed(EndPoint endpoint)
Callback method invoked when an endpoint is closed.
|
protected void |
endPointOpened(EndPoint endpoint)
Callback method invoked when an endpoint is opened.
|
protected void |
execute(java.lang.Runnable task)
Executes the given task in a different thread.
|
long |
getConnectTimeout()
Get the connect timeout
|
java.util.concurrent.Executor |
getExecutor() |
int |
getReservedThreads()
Deprecated.
|
Scheduler |
getScheduler() |
int |
getSelectorCount() |
protected boolean |
isConnectionPending(java.nio.channels.SelectableChannel channel) |
abstract Connection |
newConnection(java.nio.channels.SelectableChannel channel,
EndPoint endpoint,
java.lang.Object attachment)
Factory method to create
Connection . |
protected abstract EndPoint |
newEndPoint(java.nio.channels.SelectableChannel channel,
ManagedSelector selector,
java.nio.channels.SelectionKey selectionKey)
Factory method to create
EndPoint . |
protected java.nio.channels.Selector |
newSelector() |
protected ManagedSelector |
newSelector(int id)
Factory method for
ManagedSelector . |
void |
setConnectTimeout(long milliseconds)
Set the connect timeout (in milliseconds)
|
void |
setReservedThreads(int threads)
Deprecated.
|
addBean, addBean, addBean, addEventListener, addManaged, contains, destroy, dump, dump, dump, dump, dump, dumpBeans, dumpObject, dumpStdErr, dumpThis, getBean, getBeans, getBeans, getContainedBeans, getContainedBeans, isManaged, manage, removeBean, removeBeans, removeEventListener, setBeans, setStopTimeout, start, stop, unmanage, updateBean, updateBean, updateBeans
addLifeCycleListener, getState, getState, getStopTimeout, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, removeLifeCycleListener, start, stop
public static final int DEFAULT_CONNECT_TIMEOUT
protected static final Logger LOG
private final java.util.concurrent.Executor executor
private final Scheduler scheduler
private final ManagedSelector[] _selectors
private final java.util.concurrent.atomic.AtomicInteger _selectorIndex
private final java.util.function.IntUnaryOperator _selectorIndexUpdate
private long _connectTimeout
private ThreadPoolBudget.Lease _lease
protected SelectorManager(java.util.concurrent.Executor executor, Scheduler scheduler)
protected SelectorManager(java.util.concurrent.Executor executor, Scheduler scheduler, int selectors)
executor
- The executor to use for handling selected EndPoint
sscheduler
- The scheduler to use for timing eventsselectors
- The number of selectors to use, or -1 for a default derived
from a heuristic over available CPUs and thread pool size.private static int defaultSelectors(java.util.concurrent.Executor executor)
@ManagedAttribute(value="The Executor") public java.util.concurrent.Executor getExecutor()
@ManagedAttribute(value="The Scheduler") public Scheduler getScheduler()
@ManagedAttribute(value="The Connection timeout (ms)") public long getConnectTimeout()
public void setConnectTimeout(long milliseconds)
milliseconds
- the number of milliseconds for the timeout@Deprecated public int getReservedThreads()
@Deprecated public void setReservedThreads(int threads)
threads
- ignoredprotected void execute(java.lang.Runnable task)
task
- the task to execute@ManagedAttribute(value="The number of NIO Selectors") public int getSelectorCount()
private ManagedSelector chooseSelector()
public void connect(java.nio.channels.SelectableChannel channel, java.lang.Object attachment)
Registers a channel to perform a non-blocking connect.
The channel must be set in non-blocking mode, SocketChannel.connect(SocketAddress)
must be called prior to calling this method, and the connect operation must not be completed
(the return value of SocketChannel.connect(SocketAddress)
must be false).
channel
- the channel to registerattachment
- the attachment objectaccept(SelectableChannel, Object)
public void accept(java.nio.channels.SelectableChannel channel)
channel
- the channel to acceptaccept(SelectableChannel, Object)
public void accept(java.nio.channels.SelectableChannel channel, java.lang.Object attachment)
Registers a channel to perform non-blocking read/write operations.
This method is called just after a channel has been accepted by ServerSocketChannel.accept()
,
or just after having performed a blocking connect via Socket.connect(SocketAddress, int)
, or
just after a non-blocking connect via SocketChannel.connect(SocketAddress)
that completed
successfully.
channel
- the channel to registerattachment
- the attachment objectpublic java.io.Closeable acceptor(java.nio.channels.SelectableChannel server)
Registers a server channel for accept operations.
When a SocketChannel
is accepted from the given ServerSocketChannel
then the accepted(SelectableChannel)
method is called, which must be
overridden by a derivation of this class to handle the accepted channel
server
- the server channel to registerprotected void accepted(java.nio.channels.SelectableChannel channel) throws java.io.IOException
ServerSocketChannel
passed to acceptor(SelectableChannel)
.
The default impl throws an UnsupportedOperationException
, so it must
be overridden by subclasses if a server channel is provided.channel
- thejava.io.IOException
- if unable to accept channelprotected void doStart() throws java.lang.Exception
ContainerLifeCycle
doStart
in class ContainerLifeCycle
java.lang.Exception
protected ManagedSelector newSelector(int id)
Factory method for ManagedSelector
.
id
- an identifier for the to create
ManagedSelector
protected void doStop() throws java.lang.Exception
ContainerLifeCycle
doStop
in class ContainerLifeCycle
java.lang.Exception
protected void endPointOpened(EndPoint endpoint)
Callback method invoked when an endpoint is opened.
endpoint
- the endpoint being openedprotected void endPointClosed(EndPoint endpoint)
Callback method invoked when an endpoint is closed.
endpoint
- the endpoint being closedpublic void connectionOpened(Connection connection)
Callback method invoked when a connection is opened.
connection
- the connection just openedpublic void connectionClosed(Connection connection)
Callback method invoked when a connection is closed.
connection
- the connection just closedprotected boolean doFinishConnect(java.nio.channels.SelectableChannel channel) throws java.io.IOException
java.io.IOException
protected boolean isConnectionPending(java.nio.channels.SelectableChannel channel)
protected java.nio.channels.SelectableChannel doAccept(java.nio.channels.SelectableChannel server) throws java.io.IOException
java.io.IOException
protected void connectionFailed(java.nio.channels.SelectableChannel channel, java.lang.Throwable ex, java.lang.Object attachment)
Callback method invoked when a non-blocking connect cannot be completed.
By default it just logs with level warning.
channel
- the channel that attempted the connectex
- the exception that caused the connect to failattachment
- the attachment object associated at registrationprotected java.nio.channels.Selector newSelector() throws java.io.IOException
java.io.IOException
protected abstract EndPoint newEndPoint(java.nio.channels.SelectableChannel channel, ManagedSelector selector, java.nio.channels.SelectionKey selectionKey) throws java.io.IOException
Factory method to create EndPoint
.
This method is invoked as a result of the registration of a channel via connect(SelectableChannel, Object)
or accept(SelectableChannel)
.
channel
- the channel associated to the endpointselector
- the selector the channel is registered toselectionKey
- the selection keyjava.io.IOException
- if the endPoint cannot be creatednewConnection(SelectableChannel, EndPoint, Object)
public abstract Connection newConnection(java.nio.channels.SelectableChannel channel, EndPoint endpoint, java.lang.Object attachment) throws java.io.IOException
Factory method to create Connection
.
channel
- the channel associated to the connectionendpoint
- the endpointattachment
- the attachmentjava.io.IOException
- if unable to create new connection