Package org.glassfish.grizzly
Class NIOTransportBuilder<T extends NIOTransportBuilder>
- java.lang.Object
-
- org.glassfish.grizzly.NIOTransportBuilder<T>
-
- Direct Known Subclasses:
TCPNIOTransportBuilder,UDPNIOTransportBuilder
public abstract class NIOTransportBuilder<T extends NIOTransportBuilder> extends java.lang.ObjectThis builder is responsible for creatingNIOTransportimplementations as well as providing basic configuration forIOStrategiesand thread pools.- Since:
- 2.0
- See Also:
NIOTransport,IOStrategy,ThreadPoolConfig
-
-
Field Summary
Fields Modifier and Type Field Description protected AttributeBuilderattributeBuilderprotected intclientSocketSoTimeoutprotected intconnectionTimeoutprotected IOStrategyioStrategyprotected ThreadPoolConfigkernelConfigprotected intmaxPendingBytesPerConnectionprotected MemoryManagermemoryManagerprotected java.lang.Stringnameprotected NIOChannelDistributornioChannelDistributorprotected booleanoptimizedForMultiplexingprotected Processorprocessorprotected ProcessorSelectorprocessorSelectorprotected intreadBufferSizeprotected longreadTimeoutprotected booleanreuseAddressprotected SelectionKeyHandlerselectionKeyHandlerprotected SelectorHandlerselectorHandlerprotected java.nio.channels.spi.SelectorProviderselectorProviderprotected intselectorRunnerCountprotected java.lang.Class<? extends NIOTransport>transportClassprotected ThreadPoolConfigworkerConfigprotected intwriteBufferSizeprotected longwriteTimeout
-
Constructor Summary
Constructors Modifier Constructor Description protectedNIOTransportBuilder(java.lang.Class<? extends NIOTransport> transportClass)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description NIOTransportbuild()protected abstract NIOTransportcreate(java.lang.String name)AttributeBuildergetAttributeBuilder()intgetClientSocketSoTimeout()intgetConnectionTimeout()IOStrategygetIOStrategy()intgetMaxAsyncWriteQueueSizeInBytes()Max asynchronous write queue size in bytesMemoryManagergetMemoryManager()java.lang.StringgetName()NIOChannelDistributorgetNIOChannelDistributor()ProcessorgetProcessor()ProcessorSelectorgetProcessorSelector()intgetReadBufferSize()longgetReadTimeout(java.util.concurrent.TimeUnit timeUnit)SelectionKeyHandlergetSelectionKeyHandler()SelectorHandlergetSelectorHandler()java.nio.channels.spi.SelectorProvidergetSelectorProvider()intgetSelectorRunnersCount()ThreadPoolConfiggetSelectorThreadPoolConfig()protected abstract TgetThis()ThreadPoolConfiggetWorkerThreadPoolConfig()intgetWriteBufferSize()longgetWriteTimeout(java.util.concurrent.TimeUnit timeUnit)booleanisOptimizedForMultiplexing()booleanisReuseAddress()Whether address may be reused for multiple socketsTsetAttributeBuilder(AttributeBuilder attributeBuilder)Set theAttributeBuilderto be used by the createdNIOTransport.TsetClientSocketSoTimeout(int clientSocketSoTimeout)Sets the timeout on socket blocking operations for the clientTsetConnectionTimeout(int connectionTimeout)TsetIOStrategy(IOStrategy ioStrategy)Changes theIOStrategythat will be used.TsetMaxAsyncWriteQueueSizeInBytes(int maxAsyncWriteQueueSizeInBytes)TsetMemoryManager(MemoryManager memoryManager)Set theMemoryManagerto be used by the createdNIOTransport.TsetName(java.lang.String name)TsetNIOChannelDistributor(NIOChannelDistributor nioChannelDistributor)Set theNIOChannelDistributorto be used by the createdNIOTransport.TsetOptimizedForMultiplexing(boolean optimizedForMultiplexing)TsetProcessor(Processor processor)TsetProcessorSelector(ProcessorSelector processorSelector)TsetReadBufferSize(int readBufferSize)TsetReadTimeout(long timeout, java.util.concurrent.TimeUnit timeUnit)Sets the value of the blocking read timeoutTsetReuseAddress(boolean reuseAddress)Sets whether address may be reused for multiple socketsTsetSelectionKeyHandler(SelectionKeyHandler selectionKeyHandler)Set theSelectionKeyHandlerto be used by the createdNIOTransport.TsetSelectorHandler(SelectorHandler selectorHandler)Set theSelectorHandlerto be used by the createdNIOTransport.TsetSelectorProvider(java.nio.channels.spi.SelectorProvider selectorProvider)Set theSelectorProviderto be used by the createdNIOTransport.TsetSelectorRunnersCount(int selectorRunnersCount)Sets the number ofSelectors to be created to serve Transport connections.TsetSelectorThreadPoolConfig(ThreadPoolConfig kernelConfig)Sets theThreadPoolConfigthat will be used to construct theExecutorServicewhich will run theNIOTransport'sSelectorRunners.TsetWorkerThreadPoolConfig(ThreadPoolConfig workerConfig)Sets theThreadPoolConfigthat will be used to construct theExecutorServiceforIOStrategiesthat require worker threadsTsetWriteBufferSize(int writeBufferSize)TsetWriteTimeout(long timeout, java.util.concurrent.TimeUnit timeUnit)
-
-
-
Field Detail
-
transportClass
protected final java.lang.Class<? extends NIOTransport> transportClass
-
workerConfig
protected ThreadPoolConfig workerConfig
-
kernelConfig
protected ThreadPoolConfig kernelConfig
-
selectorProvider
protected java.nio.channels.spi.SelectorProvider selectorProvider
-
selectorHandler
protected SelectorHandler selectorHandler
-
selectionKeyHandler
protected SelectionKeyHandler selectionKeyHandler
-
memoryManager
protected MemoryManager memoryManager
-
attributeBuilder
protected AttributeBuilder attributeBuilder
-
ioStrategy
protected IOStrategy ioStrategy
-
selectorRunnerCount
protected int selectorRunnerCount
-
nioChannelDistributor
protected NIOChannelDistributor nioChannelDistributor
-
name
protected java.lang.String name
-
processor
protected Processor processor
-
processorSelector
protected ProcessorSelector processorSelector
-
readBufferSize
protected int readBufferSize
-
writeBufferSize
protected int writeBufferSize
-
clientSocketSoTimeout
protected int clientSocketSoTimeout
-
connectionTimeout
protected int connectionTimeout
-
reuseAddress
protected boolean reuseAddress
-
maxPendingBytesPerConnection
protected int maxPendingBytesPerConnection
-
optimizedForMultiplexing
protected boolean optimizedForMultiplexing
-
readTimeout
protected long readTimeout
-
writeTimeout
protected long writeTimeout
-
-
Constructor Detail
-
NIOTransportBuilder
protected NIOTransportBuilder(java.lang.Class<? extends NIOTransport> transportClass)
Constructs a new
NIOTransportusing the giventransportClassandIOStrategy.The builder's worker thread pool configuration will be based on the return value of
WorkerThreadPoolConfigProducer.createDefaultWorkerPoolConfig(Transport). If worker thread configuration is non-null, the initial selector thread pool configuration will be cloned from it, otherwise a default configuration will be chosen.- Parameters:
transportClass- the class of theNIOTransportimplementation to be used.
-
-
Method Detail
-
getSelectorRunnersCount
public int getSelectorRunnersCount()
- Returns:
- the number of
Selectors to be created to serve Transport connections. -1 is the default value, which lets the Transport to pick the value, usually it's equal to the number of CPU coresRuntime.availableProcessors()
-
setSelectorRunnersCount
public T setSelectorRunnersCount(int selectorRunnersCount)
Sets the number ofSelectors to be created to serve Transport connections. -1 is the default value, which lets the Transport to pick the value, usually it's equal to the number of CPU coresRuntime.availableProcessors().- Parameters:
selectorRunnersCount- number of channels- Returns:
- the builder
-
getWorkerThreadPoolConfig
public ThreadPoolConfig getWorkerThreadPoolConfig()
- Returns:
- the
ThreadPoolConfigthat will be used to construct theExecutorServiceforIOStrategiesthat require worker threads. This method will returnnullif aThreadPoolConfighad not been previously set.
-
setWorkerThreadPoolConfig
public T setWorkerThreadPoolConfig(ThreadPoolConfig workerConfig)
Sets theThreadPoolConfigthat will be used to construct theExecutorServiceforIOStrategiesthat require worker threads- Parameters:
workerConfig- the config- Returns:
- this builder
-
getSelectorThreadPoolConfig
public ThreadPoolConfig getSelectorThreadPoolConfig()
- Returns:
- the
ThreadPoolConfigthat will be used to construct theExecutorServicewhich will run theNIOTransport'sSelectorRunners.
-
setSelectorThreadPoolConfig
public T setSelectorThreadPoolConfig(ThreadPoolConfig kernelConfig)
Sets theThreadPoolConfigthat will be used to construct theExecutorServicewhich will run theNIOTransport'sSelectorRunners.- Parameters:
kernelConfig- the config- Returns:
- this builder
-
getIOStrategy
public IOStrategy getIOStrategy()
- Returns:
- the
IOStrategythat will be used by the createdNIOTransport.
-
setIOStrategy
public T setIOStrategy(IOStrategy ioStrategy)
Changes the
IOStrategythat will be used. Invoking this method may change the return value ofgetWorkerThreadPoolConfig()- Parameters:
ioStrategy- theIOStrategyto use.- Returns:
- this
NIOTransportBuilder
-
getMemoryManager
public MemoryManager getMemoryManager()
- Returns:
- the
MemoryManagerthat will be used by the createdNIOTransport. If not explicitly set, thenMemoryManager.DEFAULT_MEMORY_MANAGERwill be used.
-
setMemoryManager
public T setMemoryManager(MemoryManager memoryManager)
Set theMemoryManagerto be used by the createdNIOTransport.- Parameters:
memoryManager- theMemoryManager.- Returns:
- this
NIOTransportBuilder
-
getSelectorHandler
public SelectorHandler getSelectorHandler()
- Returns:
- the
SelectorHandlerthat will be used by the createdNIOTransport. If not explicitly set, thenSelectorHandler.DEFAULT_SELECTOR_HANDLERwill be used.
-
setSelectorHandler
public T setSelectorHandler(SelectorHandler selectorHandler)
Set theSelectorHandlerto be used by the createdNIOTransport.- Parameters:
selectorHandler- theSelectorHandler.- Returns:
- this
NIOTransportBuilder
-
getSelectionKeyHandler
public SelectionKeyHandler getSelectionKeyHandler()
- Returns:
- the
SelectionKeyHandlerthat will be used by the createdNIOTransport. If not explicitly set, thenSelectionKeyHandler.DEFAULT_SELECTION_KEY_HANDLERwill be used.
-
setSelectionKeyHandler
public T setSelectionKeyHandler(SelectionKeyHandler selectionKeyHandler)
Set theSelectionKeyHandlerto be used by the createdNIOTransport.- Parameters:
selectionKeyHandler- theSelectionKeyHandler.- Returns:
- this
NIOTransportBuilder
-
getAttributeBuilder
public AttributeBuilder getAttributeBuilder()
- Returns:
- the
AttributeBuilderthat will be used by the createdNIOTransport. If not explicitly set, thenAttributeBuilder.DEFAULT_ATTRIBUTE_BUILDERwill be used.
-
setAttributeBuilder
public T setAttributeBuilder(AttributeBuilder attributeBuilder)
Set theAttributeBuilderto be used by the createdNIOTransport.- Parameters:
attributeBuilder- theAttributeBuilder.- Returns:
- this
NIOTransportBuilder
-
getNIOChannelDistributor
public NIOChannelDistributor getNIOChannelDistributor()
- Returns:
- the
NIOChannelDistributorthat will be used by the createdNIOTransport. If not explicitly set, thenAttributeBuilder.DEFAULT_ATTRIBUTE_BUILDERwill be used.
-
setNIOChannelDistributor
public T setNIOChannelDistributor(NIOChannelDistributor nioChannelDistributor)
Set theNIOChannelDistributorto be used by the createdNIOTransport.- Parameters:
nioChannelDistributor- theNIOChannelDistributor.- Returns:
- this
NIOTransportBuilder
-
getSelectorProvider
public java.nio.channels.spi.SelectorProvider getSelectorProvider()
- Returns:
- the
SelectorProviderthat will be used by the createdNIOTransport. If not explicitly set, thenSelectorProvider.provider()will be used.
-
setSelectorProvider
public T setSelectorProvider(java.nio.channels.spi.SelectorProvider selectorProvider)
Set theSelectorProviderto be used by the createdNIOTransport.- Parameters:
selectorProvider- theSelectorProvider.- Returns:
- this
NIOTransportBuilder
-
getName
public java.lang.String getName()
- Returns:
- the Transport name
- See Also:
Transport.getName()
-
setName
public T setName(java.lang.String name)
- Parameters:
name- theTransportname- Returns:
- this
NIOTransportBuilder - See Also:
Transport.setName(String)
-
getProcessor
public Processor getProcessor()
- Returns:
- the default
Processorif aConnectiondoes not specify a preference - See Also:
Transport.getProcessor()
-
setProcessor
public T setProcessor(Processor processor)
- Parameters:
processor- the defaultProcessorif aConnectiondoes not specify a preference- Returns:
- this
NIOTransportBuilder - See Also:
Transport.setProcessor(Processor)
-
getProcessorSelector
public ProcessorSelector getProcessorSelector()
- Returns:
- the default
ProcessorSelector - See Also:
Transport.getProcessorSelector()
-
setProcessorSelector
public T setProcessorSelector(ProcessorSelector processorSelector)
- Parameters:
processorSelector- the defaultProcessorSelector- Returns:
- this
NIOTransportBuilder - See Also:
Transport.setProcessorSelector(ProcessorSelector)
-
getReadBufferSize
public int getReadBufferSize()
- Returns:
- the default buffer size
- See Also:
Transport.getReadBufferSize()
-
setReadBufferSize
public T setReadBufferSize(int readBufferSize)
- Parameters:
readBufferSize- the new buffer size- Returns:
- this
NIOTransportBuilder - See Also:
Transport.setReadBufferSize(int)
-
getWriteBufferSize
public int getWriteBufferSize()
- Returns:
- the default buffer size
- See Also:
Transport.getWriteBufferSize()
-
setWriteBufferSize
public T setWriteBufferSize(int writeBufferSize)
- Parameters:
writeBufferSize- the new write buffer size- Returns:
- this
NIOTransportBuilder - See Also:
Transport.setWriteBufferSize(int)
-
getClientSocketSoTimeout
public int getClientSocketSoTimeout()
- Returns:
- gets the timeout on socket blocking operations on the client
- See Also:
Socket.getSoTimeout()
-
setClientSocketSoTimeout
public T setClientSocketSoTimeout(int clientSocketSoTimeout)
Sets the timeout on socket blocking operations for the client- Parameters:
clientSocketSoTimeout- the specified timeout in milliseconds- Returns:
- this
NIOTransportBuilder - See Also:
Socket.setSoTimeout(int)
-
getConnectionTimeout
public int getConnectionTimeout()
- Returns:
- value of the connectio timeout in milliseconds
- See Also:
URLConnection.getConnectTimeout()
-
setConnectionTimeout
public T setConnectionTimeout(int connectionTimeout)
- Parameters:
connectionTimeout- the value of the connection timeout in milliseconds- Returns:
- this
NIOTransportBuilder - See Also:
NIOTransport.setConnectionTimeout(int)
-
getReadTimeout
public long getReadTimeout(java.util.concurrent.TimeUnit timeUnit)
- Parameters:
timeUnit- theTimeUnitto convert the result to- Returns:
- the blocking read timeout in the specified
TimeUnit - See Also:
Transport.getReadTimeout(java.util.concurrent.TimeUnit)
-
setReadTimeout
public T setReadTimeout(long timeout, java.util.concurrent.TimeUnit timeUnit)
Sets the value of the blocking read timeout- Parameters:
timeout- the new timeout valuetimeUnit- the unit of the new timeout value- Returns:
- this NioTransportBuilder
- See Also:
Transport.setReadTimeout(long, java.util.concurrent.TimeUnit)
-
getWriteTimeout
public long getWriteTimeout(java.util.concurrent.TimeUnit timeUnit)
- Parameters:
timeUnit- theTimeUnitto convert the result to- Returns:
- the value of the write timeout
- See Also:
Transport.getWriteTimeout(java.util.concurrent.TimeUnit)
-
setWriteTimeout
public T setWriteTimeout(long timeout, java.util.concurrent.TimeUnit timeUnit)
- Parameters:
timeout- the new write timeout valuetimeUnit- theTimeUnitof the timeout value- Returns:
- this NIOTransportBuilder
- See Also:
Transport.setWriteTimeout(long, java.util.concurrent.TimeUnit)
-
isReuseAddress
public boolean isReuseAddress()
Whether address may be reused for multiple sockets- Returns:
- SO_REUSEADDR
- See Also:
- Socket man page
-
setReuseAddress
public T setReuseAddress(boolean reuseAddress)
Sets whether address may be reused for multiple sockets- Parameters:
reuseAddress- SO_REUSEADDR- Returns:
- this
TCPNIOTransportBuilder - See Also:
- Socket man page
-
getMaxAsyncWriteQueueSizeInBytes
public int getMaxAsyncWriteQueueSizeInBytes()
Max asynchronous write queue size in bytes- Returns:
- the value is per connection, not transport total.
- See Also:
AsyncQueueWriter.getMaxPendingBytesPerConnection()
-
setMaxAsyncWriteQueueSizeInBytes
public T setMaxAsyncWriteQueueSizeInBytes(int maxAsyncWriteQueueSizeInBytes)
- Parameters:
maxAsyncWriteQueueSizeInBytes- the value is per connection, not transport total.- Returns:
- this
TCPNIOTransportBuilder - See Also:
AsyncQueueWriter.setMaxPendingBytesPerConnection(int)
-
isOptimizedForMultiplexing
public boolean isOptimizedForMultiplexing()
- Returns:
- true, if NIOTransport is configured to use AsyncQueueWriter, optimized to be used in connection multiplexing mode, or false otherwise.
- See Also:
NIOTransport.isOptimizedForMultiplexing()
-
setOptimizedForMultiplexing
public T setOptimizedForMultiplexing(boolean optimizedForMultiplexing)
- Parameters:
optimizedForMultiplexing- Configure NIOTransport to be optimized for connection multiplexing- Returns:
- this
TCPNIOTransportBuilder - See Also:
NIOTransport.setOptimizedForMultiplexing(boolean)
-
build
public NIOTransport build()
- Returns:
- an
NIOTransportbased on the builder's configuration.
-
getThis
protected abstract T getThis()
- Returns:
- this NIOTransportBuilder
- See Also:
- http://www.angelikalanger.com/GenericsFAQ/FAQSections/ProgrammingIdioms.html#FAQ205
-
create
protected abstract NIOTransport create(java.lang.String name)
-
-