Top Description Inners Fields Constructors Methods
org.eclipse.jetty.io

public abstract Class SelectorManager

extends ContainerLifeCycle
implements Dumpable
Class Inheritance
All Implemented Interfaces
org.eclipse.jetty.util.component.Dumpable
Known Direct Subclasses
org.eclipse.jetty.io.ClientConnector.ClientSelectorManager
Annotations
@ManagedObject:Manager of the NIO Selectors
Imports
java.io.Closeable, .IOException, java.net.Socket, .SocketAddress, java.nio.channels.SelectableChannel, .SelectionKey, .Selector, .ServerSocketChannel, .SocketChannel, java.util.Arrays, .EventListener, .List, java.util.concurrent.CopyOnWriteArrayList, .Executor, java.util.concurrent.atomic.AtomicInteger, java.util.function.IntUnaryOperator, org.eclipse.jetty.util.ProcessorUtils, org.eclipse.jetty.util.annotation.ManagedAttribute, .ManagedObject, org.eclipse.jetty.util.component.Container, .ContainerLifeCycle, .Dumpable, org.eclipse.jetty.util.thread.Scheduler, .ThreadPool, .ThreadPoolBudget, org.slf4j.Logger, .LoggerFactory

SelectorManager manages a number of 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.

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface
SelectorManager.AcceptListener

A listener for accept events.

public static interface

Field Summary

Modifier and TypeField and Description
private final List<SelectorManager.AcceptListener>
private long
private ThreadPoolBudget.Lease
private final AtomicInteger
private final IntUnaryOperator
private final ManagedSelector[]
public static final int
private final Executor
protected static final Logger
private final Scheduler

Constructor Summary

AccessConstructor and Description
protected
SelectorManager(Executor executor, Scheduler scheduler)

protected
SelectorManager(Executor
The executor to use for handling selected EndPoints
executor
,
Scheduler
The scheduler to use for timing events
scheduler
,
int
The number of selectors to use, or -1 for a default derived from a heuristic over available CPUs and thread pool size.
selectors
)

Method Summary

Modifier and TypeMethod and Description
public void
accept(SelectableChannel
the channel to accept
channel
)

public void
accept(SelectableChannel
the channel to register
channel
,
Object
the attachment object
attachment
)

Registers a channel to perform non-blocking read/write operations.

protected void
accepted(SelectableChannel
the
channel
)

Callback method when a channel is accepted from the ServerSocketChannel passed to acceptor(SelectableChannel).

public Closeable

Returns:

A Closable that allows the acceptor to be cancelled
acceptor
(SelectableChannel
the server channel to register
server
)

Registers a server channel for accept operations.

public boolean
protected ManagedSelector
public void
connect(SelectableChannel
the channel to register
channel
,
Object
the attachment object
attachment
)

Registers a channel to perform a non-blocking connect.

public void
connectionClosed(Connection
the connection just closed
connection
,
Throwable
the cause of the close or null for normal close
cause
)

Callback method invoked when a connection is closed.

protected void
connectionFailed(SelectableChannel
the channel that attempted the connect
channel
,
Throwable
the exception that caused the connect to fail
ex
,
Object
the attachment object associated at registration
attachment
)

Callback method invoked when a non-blocking connect cannot be completed.

public void
connectionOpened(Connection
the connection just opened
connection
,
Object
the attachment associated with the creation of the connection
context
)

Callback method invoked when a connection is opened.

private static int
protected SelectableChannel
protected boolean
protected void
doStart()

Overrides org.eclipse.jetty.util.component.ContainerLifeCycle.doStart.

Starts the managed lifecycle beans in the order they were added.
protected void
doStop()

Overrides org.eclipse.jetty.util.component.ContainerLifeCycle.doStop.

Stops the managed lifecycle beans in the reverse order they were added.
protected void
endPointClosed(EndPoint
the endpoint being closed
endpoint
)

Callback method invoked when an endpoint is closed.

protected void
endPointOpened(EndPoint
the endpoint being opened
endpoint
)

Callback method invoked when an endpoint is opened.

protected void
execute(Runnable
the task to execute
task
)

Executes the given task in a different thread.

public long

Returns:

the connect timeout (in milliseconds)
getConnectTimeout
()

Get the connect timeout

public Executor
public Scheduler
public int

Returns:

the number of selectors in use
getSelectorCount
()

public int

Returns:

total number of selector keys
getTotalKeys
()

Get total number of keys from each selector.

protected boolean
public abstract Connection

Returns:

a new connection
newConnection
(SelectableChannel
the channel associated to the connection
channel
,
EndPoint
the endpoint
endpoint
,
Object
the attachment
attachment
)

Factory method to create Connection.

protected abstract EndPoint

Returns:

a new endpoint
newEndPoint
(SelectableChannel
the channel associated to the endpoint
channel
,
ManagedSelector
the selector the channel is registered to
selector
,
SelectionKey
the selection key
selectionKey
)

Factory method to create EndPoint.

protected ManagedSelector

Returns:

a new ManagedSelector
newSelector
(int
an identifier for the to create
id
)

Factory method for ManagedSelector.

protected Selector
protected void
protected void
protected void
public boolean
public void
setConnectTimeout(long
the number of milliseconds for the timeout
milliseconds
)

Set the connect timeout (in milliseconds)

public String
toString()

Overrides org.eclipse.jetty.util.component.AbstractLifeCycle.toString.

Returns a string representation of the object.
Inherited from org.eclipse.jetty.util.component.ContainerLifeCycle:
addBeanaddBeanaddManagedcontainsdestroydumpdumpdumpdumpObjectsdumpStdErrgetBeangetBeansgetBeansgetContainedBeansgetContainedBeansisAutoisManagedisUnmanagedmanageremoveBeanremoveBeanssetBeansstartstopunmanageupdateBeanupdateBeanupdateBeansupdateBeans

Field Detail

_acceptListenersback to summary
private final List<SelectorManager.AcceptListener> _acceptListeners
_connectTimeoutback to summary
private long _connectTimeout
_leaseback to summary
private ThreadPoolBudget.Lease _lease
_selectorIndexback to summary
private final AtomicInteger _selectorIndex
_selectorIndexUpdateback to summary
private final IntUnaryOperator _selectorIndexUpdate
_selectorsback to summary
private final ManagedSelector[] _selectors
DEFAULT_CONNECT_TIMEOUTback to summary
public static final int DEFAULT_CONNECT_TIMEOUT
executorback to summary
private final Executor executor
LOGback to summary
protected static final Logger LOG

Hides org.eclipse.jetty.util.component.ContainerLifeCycle.LOG.

schedulerback to summary
private final Scheduler scheduler

Constructor Detail

SelectorManagerback to summary
protected SelectorManager(Executor executor, Scheduler scheduler)
SelectorManagerback to summary
protected SelectorManager(Executor executor, Scheduler scheduler, int selectors)
Parameters
executor:Executor

The executor to use for handling selected EndPoints

scheduler:Scheduler

The scheduler to use for timing events

selectors:int

The number of selectors to use, or -1 for a default derived from a heuristic over available CPUs and thread pool size.

Method Detail

acceptback to summary
public void accept(SelectableChannel channel)
Parameters
channel:SelectableChannel

the channel to accept

See Also
accept(SelectableChannel, Object)
acceptback to summary
public void accept(SelectableChannel 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.

Parameters
channel:SelectableChannel

the channel to register

attachment:Object

the attachment object

acceptedback to summary
protected void accepted(SelectableChannel channel) throws IOException

Callback method when a channel is accepted from the ServerSocketChannel passed to acceptor(SelectableChannel). The default impl throws an UnsupportedOperationException, so it must be overridden by subclasses if a server channel is provided.

Parameters
channel:SelectableChannel

the

Exceptions
IOException:
if unable to accept channel
acceptorback to summary
public Closeable acceptor(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

Parameters
server:SelectableChannel

the server channel to register

Returns:Closeable

A Closable that allows the acceptor to be cancelled

addEventListenerback to summary
public boolean addEventListener(EventListener listener)

Overrides org.eclipse.jetty.util.component.ContainerLifeCycle.addEventListener.

Implements org.eclipse.jetty.util.component.Container.addEventListener, org.eclipse.jetty.util.component.LifeCycle.addEventListener.

Doc from org.eclipse.jetty.util.component.Container.addEventListener.

Add an event listener. EventListeners added by this method are also added as beans.

Parameters
listener:EventListener

An EventListener

Returns:boolean

true if the listener was added

Annotations
@Override
See Also
AcceptListener, Container#addEventListener(EventListener)
chooseSelectorback to summary
protected ManagedSelector chooseSelector()
connectback to summary
public void connect(SelectableChannel 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).

Parameters
channel:SelectableChannel

the channel to register

attachment:Object

the attachment object

See Also
accept(SelectableChannel, Object)
connectionClosedback to summary
public void connectionClosed(Connection connection, Throwable cause)

Callback method invoked when a connection is closed.

Parameters
connection:Connection

the connection just closed

cause:Throwable

the cause of the close or null for normal close

connectionFailedback to summary
protected void connectionFailed(SelectableChannel channel, Throwable ex, Object attachment)

Callback method invoked when a non-blocking connect cannot be completed.

By default it just logs with level warning.

Parameters
channel:SelectableChannel

the channel that attempted the connect

ex:Throwable

the exception that caused the connect to fail

attachment:Object

the attachment object associated at registration

connectionOpenedback to summary
public void connectionOpened(Connection connection, Object context)

Callback method invoked when a connection is opened.

Parameters
connection:Connection

the connection just opened

context:Object

the attachment associated with the creation of the connection

See Also
newConnection(SelectableChannel, EndPoint, Object)
defaultSelectorsback to summary
private static int defaultSelectors(Executor executor)
doAcceptback to summary
protected SelectableChannel doAccept(SelectableChannel server) throws IOException
doFinishConnectback to summary
protected boolean doFinishConnect(SelectableChannel channel) throws IOException
doStartback to summary
protected void doStart() throws Exception

Overrides org.eclipse.jetty.util.component.ContainerLifeCycle.doStart.

Doc from org.eclipse.jetty.util.component.ContainerLifeCycle.doStart.

Starts the managed lifecycle beans in the order they were added.

Annotations
@Override
Exceptions
Exception:

Doc from org.eclipse.jetty.util.component.AbstractLifeCycle.doStart.

If there was a problem starting. Will cause a transition to FAILED state

doStopback to summary
protected void doStop() throws Exception

Overrides org.eclipse.jetty.util.component.ContainerLifeCycle.doStop.

Doc from org.eclipse.jetty.util.component.ContainerLifeCycle.doStop.

Stops the managed lifecycle beans in the reverse order they were added.

Annotations
@Override
Exceptions
Exception:

Doc from org.eclipse.jetty.util.component.AbstractLifeCycle.doStop.

If there was a problem stopping. Will cause a transition to FAILED state

endPointClosedback to summary
protected void endPointClosed(EndPoint endpoint)

Callback method invoked when an endpoint is closed.

Parameters
endpoint:EndPoint

the endpoint being closed

endPointOpenedback to summary
protected void endPointOpened(EndPoint endpoint)

Callback method invoked when an endpoint is opened.

Parameters
endpoint:EndPoint

the endpoint being opened

executeback to summary
protected void execute(Runnable task)

Executes the given task in a different thread.

Parameters
task:Runnable

the task to execute

getConnectTimeoutback to summary
public long getConnectTimeout()

Get the connect timeout

Returns:long

the connect timeout (in milliseconds)

Annotations
@ManagedAttribute:The Connection timeout (ms)
getExecutorback to summary
public Executor getExecutor()
Annotations
@ManagedAttribute:The Executor
getSchedulerback to summary
public Scheduler getScheduler()
Annotations
@ManagedAttribute:The Scheduler
getSelectorCountback to summary
public int getSelectorCount()
Returns:int

the number of selectors in use

Annotations
@ManagedAttribute:The number of NIO Selectors
getTotalKeysback to summary
public int getTotalKeys()

Get total number of keys from each selector.

Returns:int

total number of selector keys

Annotations
@ManagedAttribute:Total number of keys in all selectors
readonly:true
isConnectionPendingback to summary
protected boolean isConnectionPending(SelectableChannel channel)
newConnectionback to summary
public abstract Connection newConnection(SelectableChannel channel, EndPoint endpoint, Object attachment) throws IOException

Factory method to create Connection.

Parameters
channel:SelectableChannel

the channel associated to the connection

endpoint:EndPoint

the endpoint

attachment:Object

the attachment

Returns:Connection

a new connection

Exceptions
IOException:
if unable to create new connection
newEndPointback to summary
protected abstract EndPoint newEndPoint(SelectableChannel channel, 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(SelectableChannel, Object) or accept(SelectableChannel).

Parameters
channel:SelectableChannel

the channel associated to the endpoint

selector:ManagedSelector

the selector the channel is registered to

selectionKey:SelectionKey

the selection key

Returns:EndPoint

a new endpoint

Exceptions
IOException:
if the endPoint cannot be created
See Also
newConnection(SelectableChannel, EndPoint, Object)
newSelectorback to summary
protected ManagedSelector newSelector(int id)

Factory method for ManagedSelector.

Parameters
id:int

an identifier for the to create

Returns:ManagedSelector

a new ManagedSelector

newSelectorback to summary
protected Selector newSelector() throws IOException
onAcceptedback to summary
protected void onAccepted(SelectableChannel channel)
onAcceptFailedback to summary
protected void onAcceptFailed(SelectableChannel channel, Throwable cause)
onAcceptingback to summary
protected void onAccepting(SelectableChannel channel)
removeEventListenerback to summary
public boolean removeEventListener(EventListener listener)

Overrides org.eclipse.jetty.util.component.ContainerLifeCycle.removeEventListener.

Implements org.eclipse.jetty.util.component.Container.removeEventListener, org.eclipse.jetty.util.component.LifeCycle.removeEventListener.

Doc from org.eclipse.jetty.util.component.Container.removeEventListener.

Remove an event listener.

Parameters
listener:EventListener

the listener to remove

Returns:boolean

true if the listener was removed

Annotations
@Override
setConnectTimeoutback to summary
public void setConnectTimeout(long milliseconds)

Set the connect timeout (in milliseconds)

Parameters
milliseconds:long

the number of milliseconds for the timeout

toStringback to summary
public String toString()

Overrides org.eclipse.jetty.util.component.AbstractLifeCycle.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override
org.eclipse.jetty.io back to summary

public Interface SelectorManager.AcceptListener

extends SelectorManagerListener

A listener for accept events.

This listener is called from either the selector or acceptor thread and implementations must be non blocking and fast.

Method Summary

Modifier and TypeMethod and Description
public default void
onAccepted(SelectableChannel
the accepted channel
channel
)

Called after the accepted channel has been allocated an EndPoint and associated Connection, and after the onOpen notifications have been called on both endPoint and connection.

public default void
onAcceptFailed(SelectableChannel
the accepted channel
channel
,
Throwable
the cause of the failure
cause
)

Called if the processing of the accepted channel fails prior to calling onAccepted(SelectableChannel).

public default void
onAccepting(SelectableChannel
the accepted channel
channel
)

Called immediately after a new SelectableChannel is accepted, but before it has been submitted to the SelectorManager.

Method Detail

onAcceptedback to summary
public default void onAccepted(SelectableChannel channel)

Called after the accepted channel has been allocated an EndPoint and associated Connection, and after the onOpen notifications have been called on both endPoint and connection.

Parameters
channel:SelectableChannel

the accepted channel

onAcceptFailedback to summary
public default void onAcceptFailed(SelectableChannel channel, Throwable cause)

Called if the processing of the accepted channel fails prior to calling onAccepted(SelectableChannel).

Parameters
channel:SelectableChannel

the accepted channel

cause:Throwable

the cause of the failure

onAcceptingback to summary
public default void onAccepting(SelectableChannel channel)

Called immediately after a new SelectableChannel is accepted, but before it has been submitted to the SelectorManager.

Parameters
channel:SelectableChannel

the accepted channel

org.eclipse.jetty.io back to summary

public Interface SelectorManager.SelectorManagerListener

extends EventListener
Known Direct Subinterfaces
org.eclipse.jetty.io.SelectorManager.AcceptListener