Package org.glassfish.grizzly.nio
Class NIOTransport
- java.lang.Object
-
- org.glassfish.grizzly.AbstractTransport
-
- org.glassfish.grizzly.nio.NIOTransport
-
- All Implemented Interfaces:
AsyncQueueEnabledTransport,ConnectorHandler<SocketAddress>,MonitoringAware<TransportProbe>,TemporarySelectorsEnabledTransport,SocketBinder,SocketConnectorHandler,Transport
- Direct Known Subclasses:
TCPNIOTransport,UDPNIOTransport
public abstract class NIOTransport extends AbstractTransport implements SocketBinder, SocketConnectorHandler, TemporarySelectorsEnabledTransport, AsyncQueueEnabledTransport
- Author:
- oleksiys
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.glassfish.grizzly.Transport
Transport.State
-
-
Field Summary
Fields Modifier and Type Field Description protected ChannelConfiguratorchannelConfiguratorstatic intDEFAULT_CLIENT_SOCKET_SO_TIMEOUTstatic intDEFAULT_CONNECTION_TIMEOUTstatic booleanDEFAULT_OPTIMIZED_FOR_MULTIPLEXINGstatic booleanDEFAULT_REUSE_ADDRESSstatic intDEFAULT_SELECTOR_RUNNER_COUNTstatic intDEFAULT_SERVER_SOCKET_SO_TIMEOUTprotected NIOChannelDistributornioChannelDistributorprotected SelectionKeyHandlerselectionKeyHandlerprotected SelectorHandlerselectorHandlerprotected SelectorProviderselectorProviderprotected SelectorRunner[]selectorRunnersprotected FutureImpl<Transport>shutdownFutureFuture to control graceful shutdown statusprotected Set<GracefulShutdownListener>shutdownListenersprotected ExecutorServiceshutdownServiceExecutorService hosting shutdown listener threads.protected TemporarySelectorIOtemporarySelectorIO-
Fields inherited from class org.glassfish.grizzly.AbstractTransport
attributeBuilder, connectionMonitoringConfig, isBlocking, isStandalone, kernelPool, kernelPoolConfig, managedWorkerPool, memoryManager, name, processor, processorSelector, readBufferSize, readTimeout, state, strategy, threadPoolMonitoringConfig, transportMonitoringConfig, workerPoolConfig, workerThreadPool, writeBufferSize, writeTimeout
-
Fields inherited from interface org.glassfish.grizzly.Transport
DEFAULT_READ_BUFFER_SIZE, DEFAULT_READ_TIMEOUT, DEFAULT_WRITE_BUFFER_SIZE, DEFAULT_WRITE_TIMEOUT
-
-
Constructor Summary
Constructors Constructor Description NIOTransport(String name)
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description booleanaddShutdownListener(GracefulShutdownListener shutdownListener)Adds aGracefulShutdownListenerwhich will be called whenTransport.shutdown()is called to enable graceful shutdown of transports.protected abstract voidcloseConnection(Connection connection)Close the connection, managed by Transportprotected voidconfigureNIOConnection(NIOConnection connection)protected ExecutorServicecreateShutdownExecutorService()protected abstract TemporarySelectorIOcreateTemporarySelectorIO()protected voidfinalizeShutdown()ChannelConfiguratorgetChannelConfigurator()intgetClientSocketSoTimeout()intgetConnectionTimeout()protected intgetDefaultSelectorRunnersCount()NIOChannelDistributorgetNIOChannelDistributor()SelectionKeyHandlergetSelectionKeyHandler()SelectorHandlergetSelectorHandler()SelectorProvidergetSelectorProvider()Get theSelectorProviderto be used by this transport.protected SelectorRunner[]getSelectorRunners()intgetSelectorRunnersCount()intgetServerSocketSoTimeout()TemporarySelectorIOgetTemporarySelectorIO()booleanisOptimizedForMultiplexing()Returns true, if NIOTransport is configured to useAsyncQueueWriter, optimized to be used in connection multiplexing mode, or false otherwise.booleanisReuseAddress()protected abstract voidlisten()protected static voidnotifyProbesError(NIOTransport transport, Throwable error)Notify registeredTransportProbes about the error.protected static voidnotifyProbesPause(NIOTransport transport)Notify registeredTransportProbes about the pause event.protected static voidnotifyProbesResume(NIOTransport transport)Notify registeredTransportProbes about the resume event.protected static voidnotifyProbesStart(NIOTransport transport)Notify registeredTransportProbes about the start event.protected static voidnotifyProbesStop(NIOTransport transport)Notify registeredTransportProbes about the stop event.voidnotifyTransportError(Throwable error)Method gets invoked, when error occur during the Transport lifecycle.voidpause()Pause UDPNIOTransport, so I/O events coming on itsUDPNIOConnections will not be processed.voidresume()Resume UDPNIOTransport, which has been paused before usingpause().voidsetChannelConfigurator(ChannelConfigurator channelConfigurator)Sets the configurator responsible for initialSelectableChannelconfiguration.voidsetClientSocketSoTimeout(int socketTimeout)voidsetConnectionTimeout(int connectionTimeout)voidsetNIOChannelDistributor(NIOChannelDistributor nioChannelDistributor)voidsetOptimizedForMultiplexing(boolean optimizedForMultiplexing)Configures NIOTransport to be optimized for specific for the connection multiplexing usecase, when different threads will try to write data simultaneously.voidsetReuseAddress(boolean reuseAddress)voidsetSelectionKeyHandler(SelectionKeyHandler selectionKeyHandler)voidsetSelectorHandler(SelectorHandler selectorHandler)voidsetSelectorProvider(SelectorProvider selectorProvider)Set theSelectorProviderto be used by this transport.voidsetSelectorRunnersCount(int selectorRunnersCount)Sets the number ofSelectorRunners used for handling NIO events.voidsetServerSocketSoTimeout(int serverSocketSoTimeout)GrizzlyFuture<Transport>shutdown()Gracefully stops the transport accepting new connections and allows existing work to complete before finalizing the shutdown.GrizzlyFuture<Transport>shutdown(long gracePeriod, TimeUnit timeUnit)Gracefully stops the transport accepting new connections and allows existing work to complete before finalizing the shutdown.voidshutdownNow()Forcibly stops the transport and closes all connections.voidstart()Start TCPNIOTransport.protected voidstartSelectorRunners()protected voidstopSelectorRunners()abstract voidunbindAll()Unbinds all boundTransportconnections.-
Methods inherited from class org.glassfish.grizzly.AbstractTransport
configureBlocking, createJmxManagementObject, getAttributeBuilder, getConnectionMonitoringConfig, getIOStrategy, getKernelThreadPool, getKernelThreadPoolConfig, getMemoryManager, getMonitoringConfig, getName, getProcessor, getProcessorSelector, getReadBufferSize, getReadTimeout, getState, getThreadPoolMonitoringConfig, getWorkerThreadPool, getWorkerThreadPoolConfig, getWriteBufferSize, getWriteTimeout, isBlocking, isPaused, isStandalone, isStopped, notifyProbesBeforePause, notifyProbesBeforeResume, notifyProbesBeforeStart, notifyProbesBeforeStop, notifyProbesConfigChanged, notifyProbesPause, notifyProbesStop, obtainProcessor, setAttributeBuilder, setIOStrategy, setKernelPool0, setKernelThreadPool, setKernelThreadPoolConfig, setMemoryManager, setName, setProcessor, setProcessorSelector, setReadBufferSize, setReadTimeout, setWorkerThreadPool, setWorkerThreadPool0, setWorkerThreadPoolConfig, setWriteBufferSize, setWriteTimeout, stop
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.glassfish.grizzly.asyncqueue.AsyncQueueEnabledTransport
getAsyncQueueIO
-
Methods inherited from interface org.glassfish.grizzly.ConnectorHandler
connect, connect, connect, connect
-
Methods inherited from interface org.glassfish.grizzly.SocketBinder
bind, bind, bind, bind, bind, bind, bind, bindToInherited, unbind
-
Methods inherited from interface org.glassfish.grizzly.SocketConnectorHandler
connect
-
Methods inherited from interface org.glassfish.grizzly.Transport
configureStandalone, fireIOEvent, getReader, getReader, getWriter, getWriter
-
-
-
-
Field Detail
-
DEFAULT_SERVER_SOCKET_SO_TIMEOUT
public static final int DEFAULT_SERVER_SOCKET_SO_TIMEOUT
- See Also:
- Constant Field Values
-
DEFAULT_REUSE_ADDRESS
public static final boolean DEFAULT_REUSE_ADDRESS
- See Also:
- Constant Field Values
-
DEFAULT_CLIENT_SOCKET_SO_TIMEOUT
public static final int DEFAULT_CLIENT_SOCKET_SO_TIMEOUT
- See Also:
- Constant Field Values
-
DEFAULT_CONNECTION_TIMEOUT
public static final int DEFAULT_CONNECTION_TIMEOUT
- See Also:
- Constant Field Values
-
DEFAULT_SELECTOR_RUNNER_COUNT
public static final int DEFAULT_SELECTOR_RUNNER_COUNT
- See Also:
- Constant Field Values
-
DEFAULT_OPTIMIZED_FOR_MULTIPLEXING
public static final boolean DEFAULT_OPTIMIZED_FOR_MULTIPLEXING
- See Also:
- Constant Field Values
-
selectorHandler
protected SelectorHandler selectorHandler
-
selectionKeyHandler
protected SelectionKeyHandler selectionKeyHandler
-
channelConfigurator
protected ChannelConfigurator channelConfigurator
-
selectorRunners
protected SelectorRunner[] selectorRunners
-
nioChannelDistributor
protected NIOChannelDistributor nioChannelDistributor
-
selectorProvider
protected SelectorProvider selectorProvider
-
temporarySelectorIO
protected final TemporarySelectorIO temporarySelectorIO
-
shutdownListeners
protected Set<GracefulShutdownListener> shutdownListeners
-
shutdownFuture
protected FutureImpl<Transport> shutdownFuture
Future to control graceful shutdown status
-
shutdownService
protected ExecutorService shutdownService
ExecutorService hosting shutdown listener threads.
-
-
Constructor Detail
-
NIOTransport
public NIOTransport(String name)
-
-
Method Detail
-
unbindAll
public abstract void unbindAll()
Description copied from interface:SocketBinderUnbinds all boundTransportconnections.- Specified by:
unbindAllin interfaceSocketBinder
-
addShutdownListener
public boolean addShutdownListener(GracefulShutdownListener shutdownListener)
Description copied from interface:TransportAdds aGracefulShutdownListenerwhich will be called whenTransport.shutdown()is called to enable graceful shutdown of transports. This allows the owner of the listener to signal that all shutdown tasks are complete and that it's safe to finalize the termination of the transport- Specified by:
addShutdownListenerin interfaceTransport- Parameters:
shutdownListener- theGracefulShutdownListener- Returns:
trueif the listener was successfully registered, otherwisefalse. When this method returnsfalseit means one of two things: the transport is stopping or is stopped, or the listener has already been registered.
-
getTemporarySelectorIO
public TemporarySelectorIO getTemporarySelectorIO()
- Specified by:
getTemporarySelectorIOin interfaceTemporarySelectorsEnabledTransport
-
getSelectionKeyHandler
public SelectionKeyHandler getSelectionKeyHandler()
-
setSelectionKeyHandler
public void setSelectionKeyHandler(SelectionKeyHandler selectionKeyHandler)
-
getSelectorHandler
public SelectorHandler getSelectorHandler()
-
setSelectorHandler
public void setSelectorHandler(SelectorHandler selectorHandler)
-
getChannelConfigurator
public ChannelConfigurator getChannelConfigurator()
- Returns:
- the configurator responsible for initial
SelectableChannelconfiguration
-
setChannelConfigurator
public void setChannelConfigurator(ChannelConfigurator channelConfigurator)
Sets the configurator responsible for initialSelectableChannelconfiguration.- Parameters:
channelConfigurator-ChannelConfigurator
-
getSelectorRunnersCount
public int getSelectorRunnersCount()
- Returns:
- the number of
SelectorRunners used for handling NIO events
-
setSelectorRunnersCount
public void setSelectorRunnersCount(int selectorRunnersCount)
Sets the number ofSelectorRunners used for handling NIO events.- Parameters:
selectorRunnersCount-
-
getSelectorProvider
public SelectorProvider getSelectorProvider()
Get theSelectorProviderto be used by this transport.- Returns:
- the
SelectorProviderto be used by this transport.
-
setSelectorProvider
public void setSelectorProvider(SelectorProvider selectorProvider)
Set theSelectorProviderto be used by this transport.- Parameters:
selectorProvider- theSelectorProvider.
-
isOptimizedForMultiplexing
public boolean isOptimizedForMultiplexing()
Returns true, if NIOTransport is configured to useAsyncQueueWriter, optimized to be used in connection multiplexing mode, or false otherwise.- Returns:
- true, if NIOTransport is configured to use
AsyncQueueWriter, optimized to be used in connection multiplexing mode, or false otherwise.
-
setOptimizedForMultiplexing
public void setOptimizedForMultiplexing(boolean optimizedForMultiplexing)
Configures NIOTransport to be optimized for specific for the connection multiplexing usecase, when different threads will try to write data simultaneously.
-
startSelectorRunners
protected void startSelectorRunners() throws IOException- Throws:
IOException
-
stopSelectorRunners
protected void stopSelectorRunners()
-
getNIOChannelDistributor
public NIOChannelDistributor getNIOChannelDistributor()
-
setNIOChannelDistributor
public void setNIOChannelDistributor(NIOChannelDistributor nioChannelDistributor)
-
notifyTransportError
public void notifyTransportError(Throwable error)
Method gets invoked, when error occur during the Transport lifecycle.- Specified by:
notifyTransportErrorin interfaceTransport- Parameters:
error-Throwable.
-
getSelectorRunners
protected SelectorRunner[] getSelectorRunners()
-
notifyProbesError
protected static void notifyProbesError(NIOTransport transport, Throwable error)
Notify registeredTransportProbes about the error.- Parameters:
transport- the Transport event occurred on.
-
notifyProbesStart
protected static void notifyProbesStart(NIOTransport transport)
Notify registeredTransportProbes about the start event.- Parameters:
transport- the Transport event occurred on.
-
notifyProbesStop
protected static void notifyProbesStop(NIOTransport transport)
Notify registeredTransportProbes about the stop event.- Parameters:
transport- the Transport event occurred on.
-
notifyProbesPause
protected static void notifyProbesPause(NIOTransport transport)
Notify registeredTransportProbes about the pause event.- Parameters:
transport- the Transport event occurred on.
-
notifyProbesResume
protected static void notifyProbesResume(NIOTransport transport)
Notify registeredTransportProbes about the resume event.- Parameters:
transport- the Transport event occurred on.
-
start
public void start() throws IOExceptionStart TCPNIOTransport. The transport will be started only if its current state isTransport.State.STOPPED, otherwise the call will be ignored without exception thrown and the transport state will remain the same as it was before the method call.- Specified by:
startin interfaceTransport- Throws:
IOException- if an attempt was made to actually start which failed
-
shutdown
public GrizzlyFuture<Transport> shutdown()
Description copied from interface:TransportGracefully stops the transport accepting new connections and allows existing work to complete before finalizing the shutdown. This method will wait indefinitely for all interested parties to signal it is safe to terminate the transport. InvokeTransport.shutdownNow()to terminate the transport if the graceful shutdown is taking too long.- Specified by:
shutdownin interfaceTransport- Returns:
- a
GrizzlyFuturewhich will return the stopped transport. - See Also:
GracefulShutdownListener
-
shutdown
public GrizzlyFuture<Transport> shutdown(long gracePeriod, TimeUnit timeUnit)
Gracefully stops the transport accepting new connections and allows existing work to complete before finalizing the shutdown. This method will wait for the specified time for all interested parties to signal it is safe to terminate the transport. If the timeout is exceeded, the transport will be terminated forcefully.- Specified by:
shutdownin interfaceTransport- Parameters:
gracePeriod- the grace period for a graceful shutdown before the transport is forcibly terminated. If gracePeriod is zero or less, then there is no time limit for the shutdown.timeUnit- theTimeUnitof the specified grace period.- Returns:
- a
GrizzlyFuturewhich will return the stopped transport.
-
shutdownNow
public void shutdownNow() throws IOExceptionForcibly stops the transport and closes all connections.- Specified by:
shutdownNowin interfaceTransport- Throws:
IOException- if there was an error shutting down- See Also:
to complete gracefully
-
closeConnection
protected abstract void closeConnection(Connection connection) throws IOException
Description copied from class:AbstractTransportClose the connection, managed by Transport- Specified by:
closeConnectionin classAbstractTransport- Parameters:
connection-NIOConnectionto close- Throws:
IOException- not used
-
createTemporarySelectorIO
protected abstract TemporarySelectorIO createTemporarySelectorIO()
-
listen
protected abstract void listen()
-
getDefaultSelectorRunnersCount
protected int getDefaultSelectorRunnersCount()
-
finalizeShutdown
protected void finalizeShutdown()
-
pause
public void pause()
Pause UDPNIOTransport, so I/O events coming on itsUDPNIOConnections will not be processed. Useresume()in order to resume UDPNIOTransport processing. The transport will be paused only if its current state isTransport.State.STARTED, otherwise the call will be ignored without exception thrown and the transport state will remain the same as it was before the method call.
-
resume
public void resume()
Resume UDPNIOTransport, which has been paused before usingpause(). The transport will be resumed only if its current state isTransport.State.PAUSED, otherwise the call will be ignored without exception thrown and the transport state will remain the same as it was before the method call.
-
configureNIOConnection
protected void configureNIOConnection(NIOConnection connection)
-
isReuseAddress
public boolean isReuseAddress()
-
setReuseAddress
public void setReuseAddress(boolean reuseAddress)
-
getClientSocketSoTimeout
public int getClientSocketSoTimeout()
-
setClientSocketSoTimeout
public void setClientSocketSoTimeout(int socketTimeout)
-
getConnectionTimeout
public int getConnectionTimeout()
-
setConnectionTimeout
public void setConnectionTimeout(int connectionTimeout)
-
getServerSocketSoTimeout
public int getServerSocketSoTimeout()
-
setServerSocketSoTimeout
public void setServerSocketSoTimeout(int serverSocketSoTimeout)
-
createShutdownExecutorService
protected ExecutorService createShutdownExecutorService()
-
-