- java.lang.Object
-
- org.eclipse.jetty.util.component.AbstractLifeCycle
-
- org.eclipse.jetty.util.component.ContainerLifeCycle
-
- org.eclipse.jetty.io.SelectorManager
-
- All Implemented Interfaces:
Container,Destroyable,Dumpable,LifeCycle
- Direct Known Subclasses:
ClientConnector.ClientSelectorManager,ConnectHandler.ConnectManager,ServerConnector.ServerConnectorManager,UnixSocketConnector.UnixSocketConnectorManager
@ManagedObject("Manager of the NIO Selectors") public abstract class SelectorManager extends ContainerLifeCycle implements Dumpable
SelectorManagermanages a number ofManagedSelectors that simplify the non-blocking primitives provided by the JVM via thejava.niopackage.SelectorManagersubclasses implement methods to return protocol-specificEndPoints andConnections.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceSelectorManager.AcceptListenerA listener for accept events.static interfaceSelectorManager.SelectorManagerListener-
Nested classes/interfaces inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
AbstractLifeCycle.AbstractLifeCycleListener, AbstractLifeCycle.StopException
-
Nested classes/interfaces inherited from interface org.eclipse.jetty.util.component.Container
Container.InheritedListener, Container.Listener
-
Nested classes/interfaces inherited from interface org.eclipse.jetty.util.component.LifeCycle
LifeCycle.Listener
-
-
Field Summary
Fields Modifier and Type Field Description static intDEFAULT_CONNECT_TIMEOUTprotected static org.slf4j.LoggerLOG
-
Constructor Summary
Constructors Modifier Constructor Description protectedSelectorManager(java.util.concurrent.Executor executor, Scheduler scheduler)protectedSelectorManager(java.util.concurrent.Executor executor, Scheduler scheduler, int selectors)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidaccept(java.nio.channels.SelectableChannel channel)voidaccept(java.nio.channels.SelectableChannel channel, java.lang.Object attachment)Registers a channel to perform non-blocking read/write operations.protected voidaccepted(java.nio.channels.SelectableChannel channel)Callback method when a channel is accepted from theServerSocketChannelpassed toacceptor(SelectableChannel).java.io.Closeableacceptor(java.nio.channels.SelectableChannel server)Registers a server channel for accept operations.booleanaddEventListener(java.util.EventListener listener)Add an event listener.voidconnect(java.nio.channels.SelectableChannel channel, java.lang.Object attachment)Registers a channel to perform a non-blocking connect.voidconnectionClosed(Connection connection, java.lang.Throwable cause)Callback method invoked when a connection is closed.protected voidconnectionFailed(java.nio.channels.SelectableChannel channel, java.lang.Throwable ex, java.lang.Object attachment)Callback method invoked when a non-blocking connect cannot be completed.voidconnectionOpened(Connection connection, java.lang.Object context)Callback method invoked when a connection is opened.protected java.nio.channels.SelectableChanneldoAccept(java.nio.channels.SelectableChannel server)protected booleandoFinishConnect(java.nio.channels.SelectableChannel channel)protected voiddoStart()Starts the managed lifecycle beans in the order they were added.protected voiddoStop()Stops the managed lifecycle beans in the reverse order they were added.protected voidendPointClosed(EndPoint endpoint)Callback method invoked when an endpoint is closed.protected voidendPointOpened(EndPoint endpoint)Callback method invoked when an endpoint is opened.protected voidexecute(java.lang.Runnable task)Executes the given task in a different thread.longgetConnectTimeout()Get the connect timeoutjava.util.concurrent.ExecutorgetExecutor()SchedulergetScheduler()intgetSelectorCount()protected booleanisConnectionPending(java.nio.channels.SelectableChannel channel)abstract ConnectionnewConnection(java.nio.channels.SelectableChannel channel, EndPoint endpoint, java.lang.Object attachment)Factory method to createConnection.protected abstract EndPointnewEndPoint(java.nio.channels.SelectableChannel channel, ManagedSelector selector, java.nio.channels.SelectionKey selectionKey)Factory method to createEndPoint.protected java.nio.channels.SelectornewSelector()protected ManagedSelectornewSelector(int id)Factory method forManagedSelector.protected voidonAccepted(java.nio.channels.SelectableChannel channel)protected voidonAcceptFailed(java.nio.channels.SelectableChannel channel, java.lang.Throwable cause)protected voidonAccepting(java.nio.channels.SelectableChannel channel)booleanremoveEventListener(java.util.EventListener listener)Remove an event listener.voidsetConnectTimeout(long milliseconds)Set the connect timeout (in milliseconds)-
Methods inherited from class org.eclipse.jetty.util.component.ContainerLifeCycle
addBean, addBean, addManaged, contains, destroy, dump, dump, dump, dumpObjects, dumpStdErr, getBean, getBeans, getBeans, getContainedBeans, getContainedBeans, isAuto, isManaged, isUnmanaged, manage, removeBean, removeBeans, setBeans, start, stop, unmanage, updateBean, updateBean, updateBeans
-
Methods inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle
getEventListeners, getState, getState, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, setEventListeners, start, stop, toString
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.eclipse.jetty.util.component.Container
getCachedBeans, getEventListeners
-
-
-
-
Field Detail
-
DEFAULT_CONNECT_TIMEOUT
public static final int DEFAULT_CONNECT_TIMEOUT
- See Also:
- Constant Field Values
-
LOG
protected static final org.slf4j.Logger LOG
-
-
Constructor Detail
-
SelectorManager
protected SelectorManager(java.util.concurrent.Executor executor, Scheduler scheduler)
-
SelectorManager
protected SelectorManager(java.util.concurrent.Executor executor, Scheduler scheduler, int selectors)- Parameters:
executor- The executor to use for handling selectedEndPointsscheduler- 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.
-
-
Method Detail
-
getExecutor
@ManagedAttribute("The Executor") public java.util.concurrent.Executor getExecutor()
-
getScheduler
@ManagedAttribute("The Scheduler") public Scheduler getScheduler()
-
getConnectTimeout
@ManagedAttribute("The Connection timeout (ms)") public long getConnectTimeout()
Get the connect timeout- Returns:
- the connect timeout (in milliseconds)
-
setConnectTimeout
public void setConnectTimeout(long milliseconds)
Set the connect timeout (in milliseconds)- Parameters:
milliseconds- the number of milliseconds for the timeout
-
execute
protected void execute(java.lang.Runnable task)
Executes the given task in a different thread.- Parameters:
task- the task to execute
-
getSelectorCount
@ManagedAttribute("The number of NIO Selectors") public int getSelectorCount()
- Returns:
- the number of selectors in use
-
connect
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 ofSocketChannel.connect(SocketAddress)must be false).- Parameters:
channel- the channel to registerattachment- the attachment object- See Also:
accept(SelectableChannel, Object)
-
accept
public void accept(java.nio.channels.SelectableChannel channel)
- Parameters:
channel- the channel to accept- See Also:
accept(SelectableChannel, Object)
-
accept
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 viaSocket.connect(SocketAddress, int), or just after a non-blocking connect viaSocketChannel.connect(SocketAddress)that completed successfully.- Parameters:
channel- the channel to registerattachment- the attachment object
-
acceptor
public java.io.Closeable acceptor(java.nio.channels.SelectableChannel server)
Registers a server channel for accept operations. When a
SocketChannelis accepted from the givenServerSocketChannelthen theaccepted(SelectableChannel)method is called, which must be overridden by a derivation of this class to handle the accepted channel- Parameters:
server- the server channel to register- Returns:
- A Closable that allows the acceptor to be cancelled
-
accepted
protected void accepted(java.nio.channels.SelectableChannel channel) throws java.io.IOExceptionCallback method when a channel is accepted from theServerSocketChannelpassed toacceptor(SelectableChannel). The default impl throws anUnsupportedOperationException, so it must be overridden by subclasses if a server channel is provided.- Parameters:
channel- the- Throws:
java.io.IOException- if unable to accept channel
-
doStart
protected void doStart() throws java.lang.ExceptionDescription copied from class:ContainerLifeCycleStarts the managed lifecycle beans in the order they were added.- Overrides:
doStartin classContainerLifeCycle- Throws:
AbstractLifeCycle.StopException- If thrown, the lifecycle will immediately be stopped.java.lang.Exception- If there was a problem starting. Will cause a transition to FAILED state
-
newSelector
protected ManagedSelector newSelector(int id)
Factory method for
ManagedSelector.- Parameters:
id- an identifier for theto create- Returns:
- a new
ManagedSelector
-
newSelector
protected java.nio.channels.Selector newSelector() throws java.io.IOException- Throws:
java.io.IOException
-
doStop
protected void doStop() throws java.lang.ExceptionDescription copied from class:ContainerLifeCycleStops the managed lifecycle beans in the reverse order they were added.- Overrides:
doStopin classContainerLifeCycle- Throws:
java.lang.Exception- If there was a problem stopping. Will cause a transition to FAILED state
-
endPointOpened
protected void endPointOpened(EndPoint endpoint)
Callback method invoked when an endpoint is opened.
- Parameters:
endpoint- the endpoint being opened
-
endPointClosed
protected void endPointClosed(EndPoint endpoint)
Callback method invoked when an endpoint is closed.
- Parameters:
endpoint- the endpoint being closed
-
connectionOpened
public void connectionOpened(Connection connection, java.lang.Object context)
Callback method invoked when a connection is opened.
- Parameters:
connection- the connection just openedcontext- the attachment associated with the creation of the connection- See Also:
newConnection(SelectableChannel, EndPoint, Object)
-
connectionClosed
public void connectionClosed(Connection connection, java.lang.Throwable cause)
Callback method invoked when a connection is closed.
- Parameters:
connection- the connection just closedcause- the cause of the close or null for normal close
-
doFinishConnect
protected boolean doFinishConnect(java.nio.channels.SelectableChannel channel) throws java.io.IOException- Throws:
java.io.IOException
-
isConnectionPending
protected boolean isConnectionPending(java.nio.channels.SelectableChannel channel)
-
doAccept
protected java.nio.channels.SelectableChannel doAccept(java.nio.channels.SelectableChannel server) throws java.io.IOException- Throws:
java.io.IOException
-
connectionFailed
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.
- Parameters:
channel- the channel that attempted the connectex- the exception that caused the connect to failattachment- the attachment object associated at registration
-
newEndPoint
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)oraccept(SelectableChannel).- Parameters:
channel- the channel associated to the endpointselector- the selector the channel is registered toselectionKey- the selection key- Returns:
- a new endpoint
- Throws:
java.io.IOException- if the endPoint cannot be created- See Also:
newConnection(SelectableChannel, EndPoint, Object)
-
newConnection
public abstract Connection newConnection(java.nio.channels.SelectableChannel channel, EndPoint endpoint, java.lang.Object attachment) throws java.io.IOException
Factory method to create
Connection.- Parameters:
channel- the channel associated to the connectionendpoint- the endpointattachment- the attachment- Returns:
- a new connection
- Throws:
java.io.IOException- if unable to create new connection
-
addEventListener
public boolean addEventListener(java.util.EventListener listener)
Description copied from interface:ContainerAdd an event listener. EventListeners added by this method are also added as beans.- Specified by:
addEventListenerin interfaceContainer- Specified by:
addEventListenerin interfaceLifeCycle- Overrides:
addEventListenerin classContainerLifeCycle- Parameters:
listener- An EventListener- Returns:
- true if the listener was added
- See Also:
SelectorManager.AcceptListener,Container.addEventListener(EventListener)
-
removeEventListener
public boolean removeEventListener(java.util.EventListener listener)
Description copied from interface:ContainerRemove an event listener.- Specified by:
removeEventListenerin interfaceContainer- Specified by:
removeEventListenerin interfaceLifeCycle- Overrides:
removeEventListenerin classContainerLifeCycle- Parameters:
listener- the listener to remove- Returns:
- true if the listener was removed
- See Also:
Container.removeBean(Object)
-
onAccepting
protected void onAccepting(java.nio.channels.SelectableChannel channel)
-
onAcceptFailed
protected void onAcceptFailed(java.nio.channels.SelectableChannel channel, java.lang.Throwable cause)
-
onAccepted
protected void onAccepted(java.nio.channels.SelectableChannel channel)
-
-