Class NioParams
- java.lang.Object
-
- com.rabbitmq.client.impl.nio.NioParams
-
public class NioParams extends java.lang.Object
Parameters used to configure the NIO mode of aConnectionFactory
.- Since:
- 4.0.0
-
-
Field Summary
Fields Modifier and Type Field Description private ByteBufferFactory
byteBufferFactory
The factory to createByteBuffer
s.private java.util.concurrent.ExecutorService
connectionShutdownExecutor
the executor service used for connection shutdown(package private) static java.util.function.Function<NioContext,NioQueue>
DEFAULT_WRITE_QUEUE_FACTORY
private int
nbIoThreads
the max number of IO threadsprivate java.util.concurrent.ExecutorService
nioExecutor
the executor service used for IO threads and connections shutdownprivate int
readByteBufferSize
size of the byte buffer used for inbound dataprivate SocketChannelConfigurator
socketChannelConfigurator
the hook to configure the socket channel before it's openprivate SslEngineConfigurator
sslEngineConfigurator
the hook to configure the SSL engine before the connection is openprivate java.util.concurrent.ThreadFactory
threadFactory
the thread factory used for IO threads and connections shutdownprivate int
writeByteBufferSize
size of the byte buffer used for outbound dataprivate int
writeEnqueuingTimeoutInMs
the timeout to enqueue outbound framesprivate int
writeQueueCapacity
the capacity of the queue used for outbound framesprivate java.util.function.Function<NioContext,NioQueue>
writeQueueFactory
Factory to create aNioQueue
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description NioParams
enableHostnameVerification()
Enable server hostname verification for TLS connections.ByteBufferFactory
getByteBufferFactory()
java.util.concurrent.ExecutorService
getConnectionShutdownExecutor()
int
getNbIoThreads()
java.util.concurrent.ExecutorService
getNioExecutor()
int
getReadByteBufferSize()
SocketChannelConfigurator
getSocketChannelConfigurator()
SslEngineConfigurator
getSslEngineConfigurator()
java.util.concurrent.ThreadFactory
getThreadFactory()
int
getWriteByteBufferSize()
int
getWriteEnqueuingTimeoutInMs()
int
getWriteQueueCapacity()
java.util.function.Function<NioContext,NioQueue>
getWriteQueueFactory()
NioParams
setByteBufferFactory(ByteBufferFactory byteBufferFactory)
Set the factory to createByteBuffer
s.NioParams
setConnectionShutdownExecutor(java.util.concurrent.ExecutorService connectionShutdownExecutor)
Set theExecutorService
used for connection shutdown.NioParams
setNbIoThreads(int nbIoThreads)
Sets the max number of threads/tasks used for NIO.NioParams
setNioExecutor(java.util.concurrent.ExecutorService nioExecutor)
Sets theExecutorService
to use for NIO threads/tasks.NioParams
setReadByteBufferSize(int readByteBufferSize)
Sets the size in byte of the readByteBuffer
used in the NIO loop.void
setSocketChannelConfigurator(SocketChannelConfigurator configurator)
Set theSocketChannel
configurator.void
setSslEngineConfigurator(SslEngineConfigurator configurator)
Set theSSLEngine
configurator.NioParams
setThreadFactory(java.util.concurrent.ThreadFactory threadFactory)
Sets theThreadFactory
to use for NIO threads/tasks.NioParams
setWriteByteBufferSize(int writeByteBufferSize)
Sets the size in byte of the writeByteBuffer
used in the NIO loop.NioParams
setWriteEnqueuingTimeoutInMs(int writeEnqueuingTimeoutInMs)
Sets the timeout for queuing outbound frames.NioParams
setWriteQueueCapacity(int writeQueueCapacity)
Set the capacity of the queue used for outbound frames.NioParams
setWriteQueueFactory(java.util.function.Function<NioContext,NioQueue> writeQueueFactory)
Set the factory to createNioQueue
s.
-
-
-
Field Detail
-
DEFAULT_WRITE_QUEUE_FACTORY
static java.util.function.Function<NioContext,NioQueue> DEFAULT_WRITE_QUEUE_FACTORY
-
readByteBufferSize
private int readByteBufferSize
size of the byte buffer used for inbound data
-
writeByteBufferSize
private int writeByteBufferSize
size of the byte buffer used for outbound data
-
nbIoThreads
private int nbIoThreads
the max number of IO threads
-
writeEnqueuingTimeoutInMs
private int writeEnqueuingTimeoutInMs
the timeout to enqueue outbound frames
-
writeQueueCapacity
private int writeQueueCapacity
the capacity of the queue used for outbound frames
-
nioExecutor
private java.util.concurrent.ExecutorService nioExecutor
the executor service used for IO threads and connections shutdown
-
threadFactory
private java.util.concurrent.ThreadFactory threadFactory
the thread factory used for IO threads and connections shutdown
-
socketChannelConfigurator
private SocketChannelConfigurator socketChannelConfigurator
the hook to configure the socket channel before it's open
-
sslEngineConfigurator
private SslEngineConfigurator sslEngineConfigurator
the hook to configure the SSL engine before the connection is open
-
connectionShutdownExecutor
private java.util.concurrent.ExecutorService connectionShutdownExecutor
the executor service used for connection shutdown- Since:
- 5.4.0
-
byteBufferFactory
private ByteBufferFactory byteBufferFactory
The factory to createByteBuffer
s. The default is to create heap-basedByteBuffer
s.- Since:
- 5.5.0
-
writeQueueFactory
private java.util.function.Function<NioContext,NioQueue> writeQueueFactory
Factory to create aNioQueue
.- Since:
- 5.5.0
-
-
Constructor Detail
-
NioParams
public NioParams()
-
NioParams
public NioParams(NioParams nioParams)
-
-
Method Detail
-
enableHostnameVerification
public NioParams enableHostnameVerification()
Enable server hostname verification for TLS connections.- Returns:
- this
NioParams
instance - See Also:
setSslEngineConfigurator(SslEngineConfigurator)
,SslEngineConfigurators.ENABLE_HOSTNAME_VERIFICATION
-
getReadByteBufferSize
public int getReadByteBufferSize()
-
setReadByteBufferSize
public NioParams setReadByteBufferSize(int readByteBufferSize)
Sets the size in byte of the readByteBuffer
used in the NIO loop. Default is 32768.This parameter isn't used when using SSL/TLS, where
ByteBuffer
size is set up according to theSSLSession
packet size.- Parameters:
readByteBufferSize
- size of theByteBuffer
for inbound data- Returns:
- this
NioParams
instance
-
getWriteByteBufferSize
public int getWriteByteBufferSize()
-
setWriteByteBufferSize
public NioParams setWriteByteBufferSize(int writeByteBufferSize)
Sets the size in byte of the writeByteBuffer
used in the NIO loop. Default is 32768.This parameter isn't used when using SSL/TLS, where
ByteBuffer
size is set up according to theSSLSession
packet size.- Parameters:
writeByteBufferSize
- size of theByteBuffer
used for outbound data- Returns:
- this
NioParams
instance
-
getNbIoThreads
public int getNbIoThreads()
-
setNbIoThreads
public NioParams setNbIoThreads(int nbIoThreads)
Sets the max number of threads/tasks used for NIO. Default is 1. Set this number according to the number of simultaneous connections and their activity. Threads/tasks are created as necessary (e.g. with 10 threads, when 10 connections have been created). Once a connection is created, it's assigned to a thread/task and all its IO activity is handled by this thread/task.When idle for a few seconds (i.e. without any connection to perform IO for), a thread/task stops and is recreated if necessary.
- Parameters:
nbIoThreads
-- Returns:
- this
NioParams
instance
-
getWriteEnqueuingTimeoutInMs
public int getWriteEnqueuingTimeoutInMs()
-
setWriteEnqueuingTimeoutInMs
public NioParams setWriteEnqueuingTimeoutInMs(int writeEnqueuingTimeoutInMs)
Sets the timeout for queuing outbound frames. Default is 10,000 ms. Every requests to the server is divided into frames that are then queued in aBlockingQueue
before being sent on the network by a IO thread.If the IO thread cannot cope with the frames dispatch, the
BlockingQueue
gets filled up and blocks (blocking the calling thread by the same occasion). This timeout is the time theBlockingQueue
will wait before rejecting the outbound frame. The calling thread will then received an exception.The appropriate value depends on the application scenarios: rate of outbound data (published messages, acknowledgment, etc), network speed...
- Parameters:
writeEnqueuingTimeoutInMs
-- Returns:
- this
NioParams
instance - See Also:
setWriteQueueCapacity(int)
-
getNioExecutor
public java.util.concurrent.ExecutorService getNioExecutor()
-
setNioExecutor
public NioParams setNioExecutor(java.util.concurrent.ExecutorService nioExecutor)
Sets theExecutorService
to use for NIO threads/tasks. Default is to use the thread factory.The
ExecutorService
should be able to run the number of requested IO threads, plus a few more, as it's also used to dispatch the shutdown of connections.Connection shutdown can also be handled by a dedicated
ExecutorService
, seesetConnectionShutdownExecutor(ExecutorService)
.It's developer's responsibility to shut down the executor when it is no longer needed.
The thread factory isn't used if an executor service is set up.
- Parameters:
nioExecutor
-ExecutorService
used for IO threads and connection shutdown- Returns:
- this
NioParams
instance - See Also:
setNbIoThreads(int)
,setThreadFactory(ThreadFactory)
,setConnectionShutdownExecutor(ExecutorService)
-
getThreadFactory
public java.util.concurrent.ThreadFactory getThreadFactory()
-
setThreadFactory
public NioParams setThreadFactory(java.util.concurrent.ThreadFactory threadFactory)
Sets theThreadFactory
to use for NIO threads/tasks. Default is to use theConnectionFactory
'sThreadFactory
.The
ThreadFactory
is used to spawn the IO threads and dispatch the shutdown of connections.- Parameters:
threadFactory
-ThreadFactory
used for IO threads and connection shutdown- Returns:
- this
NioParams
instance - See Also:
setNbIoThreads(int)
,setNioExecutor(ExecutorService)
-
getWriteQueueCapacity
public int getWriteQueueCapacity()
-
setWriteQueueCapacity
public NioParams setWriteQueueCapacity(int writeQueueCapacity)
Set the capacity of the queue used for outbound frames. Default capacity is 10,000.- Parameters:
writeQueueCapacity
-- Returns:
- this
NioParams
instance - See Also:
setWriteEnqueuingTimeoutInMs(int)
-
getSocketChannelConfigurator
public SocketChannelConfigurator getSocketChannelConfigurator()
-
setSocketChannelConfigurator
public void setSocketChannelConfigurator(SocketChannelConfigurator configurator)
Set theSocketChannel
configurator. This gets a chance to "configure" a socket channel before it has been opened. The default implementation disables Nagle's algorithm.- Parameters:
configurator
- the configurator to use
-
getSslEngineConfigurator
public SslEngineConfigurator getSslEngineConfigurator()
-
setSslEngineConfigurator
public void setSslEngineConfigurator(SslEngineConfigurator configurator)
Set theSSLEngine
configurator. This gets a change to "configure" the SSL engine before the connection has been opened. This can be used e.g. to setSSLParameters
. The default implementation doesn't do anything.- Parameters:
configurator
- the configurator to use
-
getConnectionShutdownExecutor
public java.util.concurrent.ExecutorService getConnectionShutdownExecutor()
-
setConnectionShutdownExecutor
public NioParams setConnectionShutdownExecutor(java.util.concurrent.ExecutorService connectionShutdownExecutor)
Set theExecutorService
used for connection shutdown. If not set, falls back to the NIO executor and then the thread factory. This executor service is useful when strict control of the number of threads is necessary, the application can experience the closing of several connections at once, and automatic recovery is enabled. In such cases, the connection recovery can take place in the same pool of threads as the NIO operations, which can create deadlocks (all the threads of the pool are busy recovering, and there's no thread left for NIO, so connections never recover).Note it's developer's responsibility to shut down the executor when it is no longer needed.
Using the thread factory for such scenarios avoid the deadlocks, at the price of potentially creating many short-lived threads in case of massive connection lost.
With both the NIO and connection shutdown executor services set and configured accordingly, the application can control reliably the number of threads used.
- Parameters:
connectionShutdownExecutor
- the executor service to use- Returns:
- this
NioParams
instance - Since:
- 5.4.0
- See Also:
setNioExecutor(ExecutorService)
-
setByteBufferFactory
public NioParams setByteBufferFactory(ByteBufferFactory byteBufferFactory)
Set the factory to createByteBuffer
s.The default implementation creates heap-based
ByteBuffer
s.- Parameters:
byteBufferFactory
- the factory to use- Returns:
- this
NioParams
instance - Since:
- 5.5.0
- See Also:
ByteBufferFactory
,DefaultByteBufferFactory
-
getByteBufferFactory
public ByteBufferFactory getByteBufferFactory()
-
setWriteQueueFactory
public NioParams setWriteQueueFactory(java.util.function.Function<NioContext,NioQueue> writeQueueFactory)
Set the factory to createNioQueue
s.The default uses a
ArrayBlockingQueue
.
-
getWriteQueueFactory
public java.util.function.Function<NioContext,NioQueue> getWriteQueueFactory()
-
-