Class 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.LifeCycle

    @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.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static interface  SelectorManager.AcceptListener
      A listener for accept events.
      • Nested classes/interfaces inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle

        org.eclipse.jetty.util.component.AbstractLifeCycle.AbstractLifeCycleListener
      • Nested classes/interfaces inherited from interface org.eclipse.jetty.util.component.Container

        org.eclipse.jetty.util.component.Container.InheritedListener, org.eclipse.jetty.util.component.Container.Listener
      • Nested classes/interfaces inherited from interface org.eclipse.jetty.util.component.LifeCycle

        org.eclipse.jetty.util.component.LifeCycle.Listener
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int DEFAULT_CONNECT_TIMEOUT  
      protected static org.eclipse.jetty.util.log.Logger LOG  
      • Fields inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle

        FAILED, RUNNING, STARTED, STARTING, STOPPED, STOPPING
      • Fields inherited from interface org.eclipse.jetty.util.component.Dumpable

        KEY
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected SelectorManager​(java.util.concurrent.Executor executor, org.eclipse.jetty.util.thread.Scheduler scheduler)  
      protected SelectorManager​(java.util.concurrent.Executor executor, org.eclipse.jetty.util.thread.Scheduler scheduler, int selectors)  
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method 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.
      void addAcceptListener​(SelectorManager.AcceptListener listener)  
      void addEventListener​(java.util.EventListener listener)  
      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.
      protected java.nio.channels.SelectableChannel doAccept​(java.nio.channels.SelectableChannel server)  
      protected boolean doFinishConnect​(java.nio.channels.SelectableChannel channel)  
      protected void doStart()  
      protected void doStop()  
      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. 
      org.eclipse.jetty.util.thread.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.
      protected void onAccepted​(java.nio.channels.SelectableChannel channel)  
      protected void onAcceptFailed​(java.nio.channels.SelectableChannel channel, java.lang.Throwable cause)  
      protected void onAccepting​(java.nio.channels.SelectableChannel channel)  
      void removeAcceptListener​(SelectorManager.AcceptListener listener)  
      void removeEventListener​(java.util.EventListener listener)  
      void setConnectTimeout​(long milliseconds)
      Set the connect timeout (in milliseconds)
      void setReservedThreads​(int threads)
      Deprecated. 
      • Methods inherited from class org.eclipse.jetty.util.component.ContainerLifeCycle

        addBean, addBean, addBean, addEventListener, addManaged, contains, destroy, dump, dump, dump, dump, dump, dumpBeans, dumpObject, dumpObjects, dumpStdErr, dumpThis, getBean, getBeans, getBeans, getContainedBeans, getContainedBeans, isAuto, isManaged, isUnmanaged, manage, removeBean, removeBeans, removeEventListener, setBeans, setStopTimeout, start, stop, unmanage, updateBean, updateBean, updateBeans
      • Methods inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle

        addLifeCycleListener, getState, getState, getStopTimeout, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, removeLifeCycleListener, 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.Dumpable

        dump, dump, dumpSelf
    • Field Detail

      • DEFAULT_CONNECT_TIMEOUT

        public static final int DEFAULT_CONNECT_TIMEOUT
        See Also:
        Constant Field Values
      • LOG

        protected static final org.eclipse.jetty.util.log.Logger LOG
    • Constructor Detail

      • SelectorManager

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

        protected SelectorManager​(java.util.concurrent.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 Detail

      • getExecutor

        @ManagedAttribute("The Executor")
        public java.util.concurrent.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
      • getReservedThreads

        @Deprecated
        public int getReservedThreads()
        Deprecated.
        Returns:
        -1
      • setReservedThreads

        @Deprecated
        public void setReservedThreads​(int threads)
        Deprecated.
        Parameters:
        threads - ignored
      • 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 of SocketChannel.connect(SocketAddress) must be false).

        Parameters:
        channel - the channel to register
        attachment - 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 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 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

        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.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:
        java.io.IOException - if unable to accept channel
      • doStart

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

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

        Callback method invoked when a connection is opened.

        Parameters:
        connection - the connection just opened
      • connectionClosed

        public void connectionClosed​(Connection connection)

        Callback method invoked when a connection is closed.

        Parameters:
        connection - the connection just closed
      • 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 connect
        ex - the exception that caused the connect to fail
        attachment - the attachment object associated at registration
      • newSelector

        protected java.nio.channels.Selector newSelector()
                                                  throws java.io.IOException
        Throws:
        java.io.IOException
      • 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 connection
        endpoint - the endpoint
        attachment - the attachment
        Returns:
        a new connection
        Throws:
        java.io.IOException - if unable to create new connection
      • addEventListener

        public void addEventListener​(java.util.EventListener listener)
      • removeEventListener

        public void removeEventListener​(java.util.EventListener listener)
      • 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)