Class ServerConnector
- All Implemented Interfaces:
Closeable, AutoCloseable, Connector, NetworkConnector, Container, Destroyable, Dumpable, Dumpable.DumpableContainer, Graceful, LifeCycle
- Direct Known Subclasses:
NetworkTrafficServerConnector
Connector implementation is the primary connector for the
Jetty server over TCP/IP. By the use of various ConnectionFactory instances it is able
to accept connections for HTTP, HTTP/2 and WebSocket, either directly or over SSL.
The connector is a fully asynchronous NIO based implementation that by default will
use all the commons services (eg Executor, Scheduler) of the
passed Server instance, but all services may also be constructor injected
into the connector so that it may operate with dedicated or otherwise shared services.
Connection Factories
Various convenience constructors are provided to assist with common configurations of
ConnectionFactories, whose generic use is described in AbstractConnector.
If no connection factories are passed, then the connector will
default to use a HttpConnectionFactory. If an non null SslContextFactory
instance is passed, then this used to instantiate a SslConnectionFactory which is
prepended to the other passed or default factories.
Selectors
The default number of selectors is equal to half of the number of processors available to the JVM, which should allow optimal performance even if all the connections used are performing significant non-blocking work in the callback tasks.
-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from class AbstractLifeCycle
AbstractLifeCycle.AbstractLifeCycleListener, AbstractLifeCycle.StopExceptionNested classes/interfaces inherited from interface Container
Container.InheritedListener, Container.ListenerNested classes/interfaces inherited from interface Dumpable
Dumpable.DumpableContainerNested classes/interfaces inherited from interface Graceful
Graceful.Shutdown, Graceful.ThrowingRunnableNested classes/interfaces inherited from interface LifeCycle
LifeCycle.ListenerNested classes/interfaces inherited from interface NetworkConnector
NetworkConnector.Listener -
Field Summary
Fields inherited from class AbstractConnector
LOG -
Constructor Summary
ConstructorsConstructorDescriptionServerConnector(Server server) Construct a ServerConnector with a private instance ofHttpConnectionFactoryas the only factory.ServerConnector(Server server, int acceptors, int selectors) Construct a ServerConnector with a private instance ofHttpConnectionFactoryas the only factory.ServerConnector(Server server, int acceptors, int selectors, ConnectionFactory... factories) Construct a ServerConnector with a private instance ofHttpConnectionFactoryas the only factory.ServerConnector(Server server, int acceptors, int selectors, SslContextFactory.Server sslContextFactory) Construct a ServerConnector with a private instance ofHttpConnectionFactoryas the primary protocolServerConnector(Server server, Executor executor, Scheduler scheduler, ByteBufferPool bufferPool, int acceptors, int selectors, ConnectionFactory... factories) ServerConnector(Server server, ConnectionFactory... factories) Construct a Server Connector with the passed Connection factories.ServerConnector(Server server, SslContextFactory.Server sslContextFactory) Construct a ServerConnector with a private instance ofHttpConnectionFactoryas the primary protocolServerConnector(Server server, SslContextFactory.Server sslContextFactory, ConnectionFactory... factories) -
Method Summary
Modifier and TypeMethodDescriptionvoidaccept(int acceptorID) voidclose()Performs the activities needed to close the network communication (for example, to stop accepting network connections).protected voidprotected 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.intintbooleanintintbooleanGet the underlying socket, channel, buffer etc. for the connector..booleanbooleanisOpen()A Connector may be opened and not started (to reserve a port) or closed and running (to allow graceful shutdown of existing connections)booleanprotected SocketChannelEndPointnewEndPoint(SocketChannel channel, ManagedSelector selectSet, SelectionKey key) protected SelectorManagernewSelectorManager(Executor executor, Scheduler scheduler, int selectors) voidopen()Performs the activities needed to open the network communication (for example, to start accepting incoming network connections).voidopen(ServerSocketChannel acceptChannel) Open the connector using the passed ServerSocketChannel.protected ServerSocketChannelCalled byopen()to obtain the accepting channel.voidsetAcceptedReceiveBufferSize(int receiveBufferSize) voidsetAcceptedSendBufferSize(int sendBufferSize) voidsetAcceptedTcpNoDelay(boolean tcpNoDelay) voidsetAccepting(boolean accepting) voidsetAcceptQueueSize(int acceptQueueSize) Set the accept queue size (also known as accept backlog).voidsetInheritChannel(boolean inheritChannel) Sets whether this connector uses a channel inherited from the JVM.voidsetReuseAddress(boolean reuseAddress) voidsetReusePort(boolean reusePort) Set whether it is allowed to bind multiple server sockets to the same host and port.Methods inherited from class AbstractNetworkConnector
getHost, getPort, handleAcceptFailure, setHost, setPort, shutdown, toStringMethods inherited from class AbstractConnector
addConnectionFactory, addFirstConnectionFactory, addIfAbsentConnectionFactory, clearConnectionFactories, getAcceptorPriorityDelta, getAcceptors, getByteBufferPool, getConnectedEndPoints, getConnectionFactories, getConnectionFactory, getConnectionFactory, getDefaultConnectionFactory, getDefaultProtocol, getExecutor, getIdleTimeout, getName, getProtocols, getScheduler, getServer, getShutdownIdleTimeout, interruptAcceptors, isAccepting, isShutdown, join, join, onEndPointClosed, onEndPointOpened, removeConnectionFactory, setAcceptorPriorityDelta, setConnectionFactories, setDefaultProtocol, setIdleTimeout, setName, setShutdownIdleTimeoutMethods inherited from class ContainerLifeCycle
addBean, addBean, addEventListener, addManaged, contains, destroy, dump, dump, dump, dumpObjects, dumpStdErr, getBean, getBeans, getBeans, getContainedBeans, getContainedBeans, installBean, installBean, isAuto, isManaged, isUnmanaged, manage, removeBean, removeBeans, removeEventListener, setBeans, start, stop, unmanage, updateBean, updateBean, updateBeans, updateBeansMethods inherited from class AbstractLifeCycle
getEventListeners, getState, getState, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, setEventListeners, start, stopMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface Connector
getByteBufferPool, getConnectedEndPoints, getConnectionFactories, getConnectionFactory, getConnectionFactory, getDefaultConnectionFactory, getExecutor, getIdleTimeout, getName, getProtocols, getScheduler, getServerMethods inherited from interface Container
addBean, addBean, addEventListener, getBean, getBeans, getBeans, getCachedBeans, getContainedBeans, getEventListeners, isManaged, manage, removeBean, removeEventListener, unmanageMethods inherited from interface Dumpable.DumpableContainer
isDumpableMethods inherited from interface Graceful
isShutdownMethods inherited from interface LifeCycle
addEventListener, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, removeEventListener, start, stop
-
Constructor Details
-
ServerConnector
Construct a ServerConnector with a private instance of
HttpConnectionFactoryas the only factory.- Parameters:
server- TheServerthis connector will accept connection for.
-
ServerConnector
public ServerConnector(@Name("server") Server server, @Name("acceptors") int acceptors, @Name("selectors") int selectors) Construct a ServerConnector with a private instance of
HttpConnectionFactoryas the only factory.- Parameters:
server- TheServerthis connector will accept connection for.acceptors- the number of acceptor threads to use, or -1 for a default value. Acceptors accept new TCP/IP connections. If 0, then the selector threads are used to accept connections.selectors- the number of selector threads, or <=0 for a default value. Selectors notice and schedule established connection that can make IO progress.
-
ServerConnector
public ServerConnector(@Name("server") Server server, @Name("acceptors") int acceptors, @Name("selectors") int selectors, @Name("factories") ConnectionFactory... factories) Construct a ServerConnector with a private instance of
HttpConnectionFactoryas the only factory.- Parameters:
server- TheServerthis connector will accept connection for.acceptors- the number of acceptor threads to use, or -1 for a default value. Acceptors accept new TCP/IP connections. If 0, then the selector threads are used to accept connections.selectors- the number of selector threads, or <=0 for a default value. Selectors notice and schedule established connection that can make IO progress.factories- Zero or moreConnectionFactoryinstances used to create and configure connections.
-
ServerConnector
public ServerConnector(@Name("server") Server server, @Name("factories") ConnectionFactory... factories) Construct a Server Connector with the passed Connection factories.
- Parameters:
server- TheServerthis connector will accept connection for.factories- Zero or moreConnectionFactoryinstances used to create and configure connections.
-
ServerConnector
public ServerConnector(@Name("server") Server server, @Name("sslContextFactory") SslContextFactory.Server sslContextFactory) Construct a ServerConnector with a private instance of
.HttpConnectionFactoryas the primary protocol- Parameters:
server- TheServerthis connector will accept connection for.sslContextFactory- If non null, then aSslConnectionFactoryis instantiated and prepended to the list of HTTP Connection Factory.
-
ServerConnector
public ServerConnector(@Name("server") Server server, @Name("acceptors") int acceptors, @Name("selectors") int selectors, @Name("sslContextFactory") SslContextFactory.Server sslContextFactory) Construct a ServerConnector with a private instance of
.HttpConnectionFactoryas the primary protocol- Parameters:
server- TheServerthis connector will accept connection for.acceptors- the number of acceptor threads to use, or -1 for a default value. Acceptors accept new TCP/IP connections. If 0, then the selector threads are used to accept connections.selectors- the number of selector threads, or <=0 for a default value. Selectors notice and schedule established connection that can make IO progress.sslContextFactory- If non null, then aSslConnectionFactoryis instantiated and prepended to the list of HTTP Connection Factory.
-
ServerConnector
public ServerConnector(@Name("server") Server server, @Name("sslContextFactory") SslContextFactory.Server sslContextFactory, @Name("factories") ConnectionFactory... factories) - Parameters:
server- TheServerthis connector will accept connection for.sslContextFactory- If non null, then aSslConnectionFactoryis instantiated and prepended to the list of ConnectionFactories, with the first factory being the default protocol for the SslConnectionFactory.factories- Zero or moreConnectionFactoryinstances used to create and configure connections.
-
ServerConnector
public ServerConnector(@Name("server") Server server, @Name("executor") Executor executor, @Name("scheduler") Scheduler scheduler, @Name("bufferPool") ByteBufferPool bufferPool, @Name("acceptors") int acceptors, @Name("selectors") int selectors, @Name("factories") ConnectionFactory... factories) - Parameters:
server- The server this connector will be accept connection for.executor- An executor used to run tasks for handling requests, acceptors and selectors. If null then use the servers executorscheduler- A scheduler used to schedule timeouts. If null then use the servers schedulerbufferPool- A ByteBuffer pool used to allocate buffers. If null then create a private pool with default configuration.acceptors- the number of acceptor threads to use, or -1 for a default value. Acceptors accept new TCP/IP connections. If 0, then the selector threads are used to accept connections.selectors- the number of selector threads, or <=0 for a default value. Selectors notice and schedule established connection that can make IO progress.factories- Zero or moreConnectionFactoryinstances used to create and configure connections.
-
-
Method Details
-
newSelectorManager
-
doStart
Description copied from class:ContainerLifeCycleStarts the managed lifecycle beans in the order they were added.- Overrides:
doStartin classAbstractNetworkConnector- Throws:
Exception- If there was a problem starting. Will cause a transition to FAILED state
-
doStop
Description copied from class:ContainerLifeCycleStops the managed lifecycle beans in the reverse order they were added.- Overrides:
doStopin classAbstractNetworkConnector- Throws:
Exception- If there was a problem stopping. Will cause a transition to FAILED state
-
isOpen
public boolean isOpen()Description copied from interface:NetworkConnectorA Connector may be opened and not started (to reserve a port) or closed and running (to allow graceful shutdown of existing connections)- Returns:
- True if the connector is Open.
-
isInheritChannel
public boolean isInheritChannel()- Returns:
- whether this connector uses a channel inherited from the JVM.
- See Also:
-
setInheritChannel
public void setInheritChannel(boolean inheritChannel) Sets whether this connector uses a channel inherited from the JVM.
If true, the connector first tries to inherit from a channel provided by the system. If there is no inherited channel available, or if the inherited channel is not usable, then it will fall back using
ServerSocketChannel.Use it with xinetd/inetd, to launch an instance of Jetty on demand. The port used to access pages on the Jetty instance is the same as the port used to launch Jetty.
- Parameters:
inheritChannel- whether this connector uses a channel inherited from the JVM.- See Also:
-
open
Open the connector using the passed ServerSocketChannel. This open method can be called before starting the connector to pass it a ServerSocketChannel that will be used instead of one returned fromopenAcceptChannel()- Parameters:
acceptChannel- the channel to use- Throws:
IOException- if the server channel is not bound
-
open
Description copied from interface:NetworkConnectorPerforms the activities needed to open the network communication (for example, to start accepting incoming network connections).
Implementation must be idempotent.
- Specified by:
openin interfaceNetworkConnector- Overrides:
openin classAbstractNetworkConnector- Throws:
IOException- if this connector cannot be opened- See Also:
-
openAcceptChannel
Called byopen()to obtain the accepting channel.- Returns:
- ServerSocketChannel used to accept connections.
- Throws:
IOException- if unable to obtain or configure the server channel
-
close
public void close()Description copied from interface:NetworkConnectorPerforms the activities needed to close the network communication (for example, to stop accepting network connections).
Once a connector has been closed, it cannot be opened again without first calling
LifeCycle.stop()and it will not be active again until a subsequent call toLifeCycle.start().Implementation must be idempotent.
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceNetworkConnector- Overrides:
closein classAbstractNetworkConnector
-
accept
- Specified by:
acceptin classAbstractConnector- Throws:
IOException
-
configure
-
getSelectorManager
-
getTransport
-
getLocalPort
- Specified by:
getLocalPortin interfaceNetworkConnector- Overrides:
getLocalPortin classAbstractNetworkConnector- Returns:
- The actual port the connector is listening on, or -1 if it has not been opened, or -2 if it has been closed.
-
newEndPoint
protected SocketChannelEndPoint newEndPoint(SocketChannel channel, ManagedSelector selectSet, SelectionKey key) throws IOException - Throws:
IOException
-
getAcceptQueueSize
- Returns:
- the accept queue size
-
setAcceptQueueSize
public void setAcceptQueueSize(int acceptQueueSize) Set the accept queue size (also known as accept backlog).- Parameters:
acceptQueueSize- the accept queue size (also known as accept backlog)
-
getReuseAddress
- Returns:
- whether rebinding the server socket is allowed with sockets in tear-down states
- See Also:
-
setReuseAddress
public void setReuseAddress(boolean reuseAddress) - Parameters:
reuseAddress- whether rebinding the server socket is allowed with sockets in tear-down states- See Also:
-
isReusePort
- Returns:
- whether it is allowed to bind multiple server sockets to the same host and port
-
setReusePort
public void setReusePort(boolean reusePort) Set whether it is allowed to bind multiple server sockets to the same host and port.- Parameters:
reusePort- whether it is allowed to bind multiple server sockets to the same host and port
-
getAcceptedTcpNoDelay
- Returns:
- whether the accepted socket gets
TCP_NODELAYenabled. - See Also:
-
setAcceptedTcpNoDelay
public void setAcceptedTcpNoDelay(boolean tcpNoDelay) - Parameters:
tcpNoDelay- whetherTCP_NODELAYgets enabled on the the accepted socket.- See Also:
-
getAcceptedReceiveBufferSize
- Returns:
- the
SO_RCVBUFsize to set onto the accepted socket. A value of -1 indicates that it is left to its default value. - See Also:
-
setAcceptedReceiveBufferSize
public void setAcceptedReceiveBufferSize(int receiveBufferSize) - Parameters:
receiveBufferSize- theSO_RCVBUFsize to set onto the accepted socket. A value of -1 indicates that it is left to its default value.- See Also:
-
getAcceptedSendBufferSize
- Returns:
- the
SO_SNDBUFsize to set onto the accepted socket. A value of -1 indicates that it is left to its default value. - See Also:
-
setAcceptedSendBufferSize
public void setAcceptedSendBufferSize(int sendBufferSize) - Parameters:
sendBufferSize- theSO_SNDBUFsize to set onto the accepted socket. A value of -1 indicates that it is left to its default value.- See Also:
-
setAccepting
public void setAccepting(boolean accepting) - Overrides:
setAcceptingin classAbstractConnector
-