public abstract class SelectorManager extends AbstractLifeCycle implements Dumpable
SelectorManager manages a number of SelectorManager.ManagedSelectors that
simplify the non-blocking primitives provided by the JVM via the java.nio package.
SelectorManager subclasses implement methods to return protocol-specific
EndPoints and Connections.
| Modifier and Type | Class and Description |
|---|---|
class |
SelectorManager.ManagedSelector
SelectorManager.ManagedSelector wraps a Selector simplifying non-blocking operations on channels. |
static interface |
SelectorManager.SelectableEndPoint
A
SelectorManager.SelectableEndPoint is an EndPoint that wish to be
notified of non-blocking events by the SelectorManager.ManagedSelector. |
AbstractLifeCycle.AbstractLifeCycleListenerLifeCycle.Listener| Modifier and Type | Field and Description |
|---|---|
static int |
DEFAULT_CONNECT_TIMEOUT |
protected static Logger |
LOG |
| Modifier | Constructor and Description |
|---|---|
protected |
SelectorManager(Executor executor,
Scheduler scheduler) |
protected |
SelectorManager(Executor executor,
Scheduler scheduler,
int selectors) |
| Modifier and Type | Method and Description |
|---|---|
void |
accept(SocketChannel channel) |
void |
accept(SocketChannel channel,
Object attachment)
Registers a channel to perform non-blocking read/write operations.
|
protected void |
accepted(SocketChannel channel)
Callback method when a channel is accepted from the
ServerSocketChannel
passed to acceptor(ServerSocketChannel). |
void |
acceptor(ServerSocketChannel server)
Registers a server channel for accept operations.
|
void |
connect(SocketChannel channel,
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(SocketChannel channel,
Throwable ex,
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.
|
protected void |
doStart() |
protected void |
doStop() |
String |
dump() |
void |
dump(Appendable out,
String indent) |
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(Runnable task)
Executes the given task in a different thread.
|
protected boolean |
finishConnect(SocketChannel channel) |
long |
getConnectTimeout()
Get the connect timeout
|
Executor |
getExecutor() |
Scheduler |
getScheduler() |
int |
getSelectorCount() |
int |
getSelectorPriorityDelta() |
boolean |
isSubmitKeyUpdates() |
abstract Connection |
newConnection(SocketChannel channel,
EndPoint endpoint,
Object attachment)
Factory method to create
Connection. |
protected abstract EndPoint |
newEndPoint(SocketChannel channel,
SelectorManager.ManagedSelector selector,
SelectionKey selectionKey)
Factory method to create
EndPoint. |
protected SelectorManager.ManagedSelector |
newSelector(int id)
Factory method for
SelectorManager.ManagedSelector. |
void |
setConnectTimeout(long milliseconds)
Set the connect timeout (in milliseconds)
|
void |
setSelectorPriorityDelta(int selectorPriorityDelta)
Sets the selector thread priority delta to the given amount.
|
void |
setSubmitKeyUpdates(boolean submitKeyUpdates)
Controls whether
SelectionKey updates should be submitted for
execution in the selector thread, or directly executed by the updating
thread. |
addLifeCycleListener, getState, getState, getStopTimeout, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, removeLifeCycleListener, setStopTimeout, start, stoppublic static final int DEFAULT_CONNECT_TIMEOUT
protected static final Logger LOG
public Executor getExecutor()
public Scheduler getScheduler()
public long getConnectTimeout()
public void setConnectTimeout(long milliseconds)
milliseconds - the number of milliseconds for the timeout@ManagedAttribute(value="The priority delta to apply to selector threads") public int getSelectorPriorityDelta()
public void setSelectorPriorityDelta(int selectorPriorityDelta)
This allows the selector threads to run at a different priority. Typically this would be used to lower the priority to give preference to handling previously accepted connections rather than accepting new connections.
selectorPriorityDelta - the amount to change the thread priority
delta to (may be negative)Thread.getPriority()public boolean isSubmitKeyUpdates()
SelectionKeys are submitted
to the selector thread or executed by the updater.setSubmitKeyUpdates(boolean)public void setSubmitKeyUpdates(boolean submitKeyUpdates)
SelectionKey updates should be submitted for
execution in the selector thread, or directly executed by the updating
thread.
Submission incur is possible queueing and wakeup of the selector, while
direct execution incurs in lock contention in JDK classes.submitKeyUpdates - whether updates to SelectionKeys are submitted
to the selector thread or executed by the updater.SelectorManager.ManagedSelector.updateKey(Runnable)protected void execute(Runnable task)
task - the task to executepublic int getSelectorCount()
public void connect(SocketChannel channel, 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(SocketChannel, Object)public void accept(SocketChannel channel)
accept(SocketChannel, Object)public void accept(SocketChannel channel, 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 void acceptor(ServerSocketChannel server)
Registers a server channel for accept operations.
When a SocketChannel is accepted from the given ServerSocketChannel
then the accepted(SocketChannel) 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(SocketChannel channel) throws IOException
ServerSocketChannel
passed to acceptor(ServerSocketChannel).
The default impl throws an UnsupportedOperationException, so it must
be overridden by subclasses if a server channel is provided.channel - theIOExceptionprotected void doStart()
throws Exception
doStart in class AbstractLifeCycleExceptionprotected SelectorManager.ManagedSelector newSelector(int id)
Factory method for SelectorManager.ManagedSelector.
id - an identifier for the to createSelectorManager.ManagedSelectorprotected void doStop()
throws Exception
doStop in class AbstractLifeCycleExceptionprotected 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 finishConnect(SocketChannel channel) throws IOException
IOExceptionprotected void connectionFailed(SocketChannel channel, Throwable ex, 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 abstract EndPoint newEndPoint(SocketChannel channel, SelectorManager.ManagedSelector selector, SelectionKey selectionKey) throws IOException
Factory method to create EndPoint.
This method is invoked as a result of the registration of a channel via connect(SocketChannel, Object)
or accept(SocketChannel).
channel - the channel associated to the endpointselector - the selector the channel is registered toselectionKey - the selection keyIOException - if the endPoint cannot be creatednewConnection(SocketChannel, EndPoint, Object)public abstract Connection newConnection(SocketChannel channel, EndPoint endpoint, Object attachment) throws IOException
Factory method to create Connection.
channel - the channel associated to the connectionendpoint - the endpointattachment - the attachmentIOExceptionnewEndPoint(SocketChannel, ManagedSelector, SelectionKey)public void dump(Appendable out, String indent) throws IOException
dump in interface DumpableIOExceptionCopyright © 1995-2014 Mort Bay Consulting. All Rights Reserved.