Class ClientConnector

  • 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

    @ManagedObject
    public class ClientConnector
    extends org.eclipse.jetty.util.component.ContainerLifeCycle

    The client-side component that connects to server sockets.

    ClientConnector delegates the handling of SocketChannels to a SelectorManager, and centralizes the configuration of necessary components such as the executor, the scheduler, etc.

    ClientConnector offers a low-level API that can be used to connect SocketChannels to listening servers via the connect(SocketAddress, Map) method.

    However, a ClientConnector instance is typically just configured and then passed to an HttpClient transport, so that applications can use high-level APIs to make HTTP requests to servers:

     // Create a ClientConnector instance.
     ClientConnector connector = new ClientConnector();
    
     // Configure the ClientConnector.
     connector.setSelectors(1);
     connector.setSslContextFactory(new SslContextFactory.Client());
    
     // Pass it to the HttpClient transport.
     HttpClientTransport transport = new HttpClientTransportDynamic(connector);
     HttpClient httpClient = new HttpClient(transport);
     httpClient.start();
     
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      protected class  ClientConnector.ClientSelectorManager  
      static class  ClientConnector.Configurator
      Configures a ClientConnector.
      • Nested classes/interfaces inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle

        org.eclipse.jetty.util.component.AbstractLifeCycle.AbstractLifeCycleListener, org.eclipse.jetty.util.component.AbstractLifeCycle.StopException
      • 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.Dumpable

        org.eclipse.jetty.util.component.Dumpable.DumpableContainer
      • Nested classes/interfaces inherited from interface org.eclipse.jetty.util.component.LifeCycle

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

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void accept​(java.nio.channels.SelectableChannel selectable, java.util.Map<java.lang.String,​java.lang.Object> context)  
      protected void configure​(java.nio.channels.SelectableChannel selectable)  
      void connect​(java.net.SocketAddress address, java.util.Map<java.lang.String,​java.lang.Object> context)  
      protected void connectFailed​(java.lang.Throwable failure, java.util.Map<java.lang.String,​java.lang.Object> context)  
      protected void doStart()  
      protected void doStop()  
      static ClientConnector forUnixDomain​(java.nio.file.Path path)
      Creates a ClientConnector configured to connect via Unix-Domain sockets to the given Unix-Domain path
      java.net.SocketAddress getBindAddress()  
      ByteBufferPool getByteBufferPool()  
      java.time.Duration getConnectTimeout()  
      java.util.concurrent.Executor getExecutor()  
      java.time.Duration getIdleTimeout()  
      int getReceiveBufferSize()  
      boolean getReuseAddress()  
      org.eclipse.jetty.util.thread.Scheduler getScheduler()  
      int getSelectors()  
      int getSendBufferSize()  
      org.eclipse.jetty.util.ssl.SslContextFactory.Client getSslContextFactory()  
      boolean isConnectBlocking()  
      boolean isIntrinsicallySecure​(java.net.SocketAddress address)  
      boolean isReusePort()  
      boolean isTCPNoDelay()  
      protected Connection newConnection​(EndPoint endPoint, java.util.Map<java.lang.String,​java.lang.Object> context)  
      protected EndPoint newEndPoint​(java.nio.channels.SelectableChannel selectable, ManagedSelector selector, java.nio.channels.SelectionKey selectionKey)  
      protected SelectorManager newSelectorManager()  
      protected org.eclipse.jetty.util.ssl.SslContextFactory.Client newSslContextFactory()  
      void setBindAddress​(java.net.SocketAddress bindAddress)
      Sets the bind address of sockets before the connect operation.
      void setByteBufferPool​(ByteBufferPool byteBufferPool)  
      void setConnectBlocking​(boolean connectBlocking)  
      void setConnectTimeout​(java.time.Duration connectTimeout)  
      void setExecutor​(java.util.concurrent.Executor executor)  
      void setIdleTimeout​(java.time.Duration idleTimeout)  
      void setReceiveBufferSize​(int receiveBufferSize)  
      void setReuseAddress​(boolean reuseAddress)
      Sets whether it is allowed to bind a socket to a socket address that may be in use by another socket in tear-down state, for example in TIME_WAIT state.
      void setReusePort​(boolean reusePort)
      Sets whether it is allowed to bind multiple sockets to the same socket address (same host and same port).
      void setScheduler​(org.eclipse.jetty.util.thread.Scheduler scheduler)  
      void setSelectors​(int selectors)  
      void setSendBufferSize​(int sendBufferSize)  
      void setSslContextFactory​(org.eclipse.jetty.util.ssl.SslContextFactory.Client sslContextFactory)  
      void setTCPNoDelay​(boolean tcpNoDelay)  
      • Methods inherited from class org.eclipse.jetty.util.component.ContainerLifeCycle

        addBean, addBean, addEventListener, addManaged, contains, destroy, dump, dump, dump, dumpObjects, dumpStdErr, getBean, getBeans, getBeans, getContainedBeans, getContainedBeans, isAuto, isManaged, isUnmanaged, manage, removeBean, removeBeans, removeEventListener, setBeans, start, stop, unmanage, updateBean, updateBean, updateBeans, 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
      • Methods inherited from interface org.eclipse.jetty.util.component.Dumpable

        dumpSelf
      • Methods inherited from interface org.eclipse.jetty.util.component.Dumpable.DumpableContainer

        isDumpable
    • Field Detail

      • CLIENT_CONNECTOR_CONTEXT_KEY

        public static final java.lang.String CLIENT_CONNECTOR_CONTEXT_KEY
        See Also:
        Constant Field Values
      • REMOTE_SOCKET_ADDRESS_CONTEXT_KEY

        public static final java.lang.String REMOTE_SOCKET_ADDRESS_CONTEXT_KEY
        See Also:
        Constant Field Values
      • CLIENT_CONNECTION_FACTORY_CONTEXT_KEY

        public static final java.lang.String CLIENT_CONNECTION_FACTORY_CONTEXT_KEY
        See Also:
        Constant Field Values
      • CONNECTION_PROMISE_CONTEXT_KEY

        public static final java.lang.String CONNECTION_PROMISE_CONTEXT_KEY
        See Also:
        Constant Field Values
      • APPLICATION_PROTOCOLS_CONTEXT_KEY

        public static final java.lang.String APPLICATION_PROTOCOLS_CONTEXT_KEY
        See Also:
        Constant Field Values
    • Method Detail

      • forUnixDomain

        public static ClientConnector forUnixDomain​(java.nio.file.Path path)

        Creates a ClientConnector configured to connect via Unix-Domain sockets to the given Unix-Domain path

        Parameters:
        path - the Unix-Domain path to connect to
        Returns:
        a ClientConnector that connects to the given Unix-Domain path
      • getExecutor

        public java.util.concurrent.Executor getExecutor()
      • setExecutor

        public void setExecutor​(java.util.concurrent.Executor executor)
      • getScheduler

        public org.eclipse.jetty.util.thread.Scheduler getScheduler()
      • setScheduler

        public void setScheduler​(org.eclipse.jetty.util.thread.Scheduler scheduler)
      • setByteBufferPool

        public void setByteBufferPool​(ByteBufferPool byteBufferPool)
      • getSslContextFactory

        public org.eclipse.jetty.util.ssl.SslContextFactory.Client getSslContextFactory()
      • setSslContextFactory

        public void setSslContextFactory​(org.eclipse.jetty.util.ssl.SslContextFactory.Client sslContextFactory)
      • getSelectors

        @ManagedAttribute("The number of NIO selectors")
        public int getSelectors()
        Returns:
        the number of NIO selectors
      • setSelectors

        public void setSelectors​(int selectors)
      • isConnectBlocking

        @ManagedAttribute("Whether connect operations are performed in blocking mode")
        public boolean isConnectBlocking()
        Returns:
        whether connect(SocketAddress, Map) operations are performed in blocking mode
      • setConnectBlocking

        public void setConnectBlocking​(boolean connectBlocking)
      • getConnectTimeout

        @ManagedAttribute("The timeout of connect operations")
        public java.time.Duration getConnectTimeout()
        Returns:
        the timeout of connect(SocketAddress, Map) operations
      • setConnectTimeout

        public void setConnectTimeout​(java.time.Duration connectTimeout)
      • getIdleTimeout

        @ManagedAttribute("The duration for which a connection can be idle")
        public java.time.Duration getIdleTimeout()
        Returns:
        the max duration for which a connection can be idle (that is, without traffic of bytes in either direction)
      • setIdleTimeout

        public void setIdleTimeout​(java.time.Duration idleTimeout)
      • getBindAddress

        @ManagedAttribute("The socket address to bind sockets to before the connect operation")
        public java.net.SocketAddress getBindAddress()
        Returns:
        the address to bind a socket to before the connect operation
      • setBindAddress

        public void setBindAddress​(java.net.SocketAddress bindAddress)

        Sets the bind address of sockets before the connect operation.

        In multi-homed hosts, you may want to connect from a specific address:

         clientConnector.setBindAddress(new InetSocketAddress("127.0.0.2", 0));
         

        Note the use of the port 0 to indicate that a different ephemeral port should be used for each different connection.

        In the rare cases where you want to use the same port for all connections, you must also call setReusePort(true).

        Parameters:
        bindAddress - the socket address to bind to before the connect operation
      • isTCPNoDelay

        @ManagedAttribute("Whether small TCP packets are sent without delay")
        public boolean isTCPNoDelay()
        Returns:
        whether small TCP packets are sent without delay
      • setTCPNoDelay

        public void setTCPNoDelay​(boolean tcpNoDelay)
      • getReuseAddress

        @ManagedAttribute("Whether rebinding is allowed with sockets in tear-down states")
        public boolean getReuseAddress()
        Returns:
        whether rebinding is allowed with sockets in tear-down states
      • setReuseAddress

        public void setReuseAddress​(boolean reuseAddress)

        Sets whether it is allowed to bind a socket to a socket address that may be in use by another socket in tear-down state, for example in TIME_WAIT state.

        This is useful when ClientConnector is restarted: an existing connection may still be using a network address (same host and same port) that is also chosen for a new connection.

        Parameters:
        reuseAddress - whether rebinding is allowed with sockets in tear-down states
        See Also:
        setReusePort(boolean)
      • isReusePort

        @ManagedAttribute("Whether binding to same host and port is allowed")
        public boolean isReusePort()
        Returns:
        whether binding to same host and port is allowed
      • setReusePort

        public void setReusePort​(boolean reusePort)

        Sets whether it is allowed to bind multiple sockets to the same socket address (same host and same port).

        Parameters:
        reusePort - whether binding to same host and port is allowed
      • getReceiveBufferSize

        @ManagedAttribute("The receive buffer size in bytes")
        public int getReceiveBufferSize()
        Returns:
        the receive buffer size in bytes, or -1 for the default value
      • setReceiveBufferSize

        public void setReceiveBufferSize​(int receiveBufferSize)
      • getSendBufferSize

        @ManagedAttribute("The send buffer size in bytes")
        public int getSendBufferSize()
        Returns:
        the send buffer size in bytes, or -1 for the default value
      • setSendBufferSize

        public void setSendBufferSize​(int sendBufferSize)
      • 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
      • newSslContextFactory

        protected org.eclipse.jetty.util.ssl.SslContextFactory.Client newSslContextFactory()
      • connect

        public void connect​(java.net.SocketAddress address,
                            java.util.Map<java.lang.String,​java.lang.Object> context)
      • accept

        public void accept​(java.nio.channels.SelectableChannel selectable,
                           java.util.Map<java.lang.String,​java.lang.Object> context)
      • configure

        protected void configure​(java.nio.channels.SelectableChannel selectable)
                          throws java.io.IOException
        Throws:
        java.io.IOException
      • newEndPoint

        protected EndPoint newEndPoint​(java.nio.channels.SelectableChannel selectable,
                                       ManagedSelector selector,
                                       java.nio.channels.SelectionKey selectionKey)
      • newConnection

        protected Connection newConnection​(EndPoint endPoint,
                                           java.util.Map<java.lang.String,​java.lang.Object> context)
                                    throws java.io.IOException
        Throws:
        java.io.IOException
      • connectFailed

        protected void connectFailed​(java.lang.Throwable failure,
                                     java.util.Map<java.lang.String,​java.lang.Object> context)