Class SelectorManager

java.lang.Object
org.eclipse.jetty.util.component.AbstractLifeCycle
org.eclipse.jetty.util.component.ContainerLifeCycle
org.eclipse.jetty.io.SelectorManager
All Implemented Interfaces:
org.eclipse.jetty.util.component.Container, org.eclipse.jetty.util.component.Destroyable, org.eclipse.jetty.util.component.Dumpable, org.eclipse.jetty.util.component.Dumpable.DumpableContainer, org.eclipse.jetty.util.component.LifeCycle
Direct Known Subclasses:
ClientConnector.ClientSelectorManager

@ManagedObject("Manager of the NIO Selectors") public abstract class SelectorManager extends org.eclipse.jetty.util.component.ContainerLifeCycle implements org.eclipse.jetty.util.component.Dumpable

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.

  • Field Details

    • DEFAULT_CONNECT_TIMEOUT

      public static final int DEFAULT_CONNECT_TIMEOUT
      See Also:
    • LOG

      protected static final org.slf4j.Logger LOG
  • Constructor Details

    • SelectorManager

      protected SelectorManager(Executor executor, org.eclipse.jetty.util.thread.Scheduler scheduler)
    • SelectorManager

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

    • getExecutor

      @ManagedAttribute("The Executor") public Executor getExecutor()
    • getScheduler

      @ManagedAttribute("The Scheduler") public org.eclipse.jetty.util.thread.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(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
    • chooseSelector

      protected ManagedSelector chooseSelector()
    • connect

      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 - the channel to register
      attachment - the attachment object
      See Also:
    • accept

      public void accept(SelectableChannel channel)
      Parameters:
      channel - the channel to accept
      See Also:
    • accept

      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 - the channel to register
      attachment - the attachment object
    • acceptor

      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 - the server channel to register
      Returns:
      A Closable that allows the acceptor to be cancelled
    • accepted

      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 - the
      Throws:
      IOException - if unable to accept channel
    • doStart

      protected void doStart() throws Exception
      Overrides:
      doStart in class org.eclipse.jetty.util.component.ContainerLifeCycle
      Throws:
      Exception
    • newSelector

      protected ManagedSelector newSelector(int id)

      Factory method for ManagedSelector.

      Parameters:
      id - an identifier for the to create
      Returns:
      a new ManagedSelector
    • newSelector

      protected Selector newSelector() throws IOException
      Throws:
      IOException
    • doStop

      protected void doStop() throws Exception
      Overrides:
      doStop in class org.eclipse.jetty.util.component.ContainerLifeCycle
      Throws:
      Exception
    • 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, Object context)

      Callback method invoked when a connection is opened.

      Parameters:
      connection - the connection just opened
      context - the attachment associated with the creation of the connection
      See Also:
    • connectionClosed

      public void connectionClosed(Connection connection, Throwable cause)

      Callback method invoked when a connection is closed.

      Parameters:
      connection - the connection just closed
      cause - the cause of the close or null for normal close
    • doFinishConnect

      protected boolean doFinishConnect(SelectableChannel channel) throws IOException
      Throws:
      IOException
    • isConnectionPending

      protected boolean isConnectionPending(SelectableChannel channel)
    • doAccept

      protected SelectableChannel doAccept(SelectableChannel server) throws IOException
      Throws:
      IOException
    • connectionFailed

      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 - the channel that attempted the connect
      ex - the exception that caused the connect to fail
      attachment - the attachment object associated at registration
    • newEndPoint

      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 - the channel associated to the endpoint
      selector - the selector the channel is registered to
      selectionKey - the selection key
      Returns:
      a new endpoint
      Throws:
      IOException - if the endPoint cannot be created
      See Also:
    • newConnection

      public abstract Connection newConnection(SelectableChannel channel, EndPoint endpoint, Object attachment) throws IOException

      Factory method to create Connection.

      Parameters:
      channel - the channel associated to the connection
      endpoint - the endpoint
      attachment - the attachment
      Returns:
      a new connection
      Throws:
      IOException - if unable to create new connection
    • addEventListener

      public boolean addEventListener(EventListener listener)
      Specified by:
      addEventListener in interface org.eclipse.jetty.util.component.Container
      Specified by:
      addEventListener in interface org.eclipse.jetty.util.component.LifeCycle
      Overrides:
      addEventListener in class org.eclipse.jetty.util.component.ContainerLifeCycle
      Parameters:
      listener - An EventListener
      See Also:
    • removeEventListener

      public boolean removeEventListener(EventListener listener)
      Specified by:
      removeEventListener in interface org.eclipse.jetty.util.component.Container
      Specified by:
      removeEventListener in interface org.eclipse.jetty.util.component.LifeCycle
      Overrides:
      removeEventListener in class org.eclipse.jetty.util.component.ContainerLifeCycle
    • onAccepting

      protected void onAccepting(SelectableChannel channel)
    • onAcceptFailed

      protected void onAcceptFailed(SelectableChannel channel, Throwable cause)
    • onAccepted

      protected void onAccepted(SelectableChannel channel)