This chapter describes how JBoss Messaging uses and pools threads and how you can manage them.
First we'll discuss how threads are managed and used on the server side, then we'll look at the client side.
Each JBoss Messaging Server maintains a single thread pool for general use, and a scheduled thread pool for scheduled use. A Java scheduled thread pool cannot be configured to use a standard thread pool, otherwise we could use a single thread pool for both scheduled and non scheduled activity.
There are also a small number of other places where threads are used directly, we'll discuss each in turn.
The server scheduled thread pool is used for most activities on the server side that require running periodically or with delays. It maps internally to a java.util.concurrent.ScheduledThreadPoolExecutor instance.
The maximum number of thread used by this pool is configure in jbm-configuration.xml with the scheduled-thread-pool-max-size parameter. The default value is 5 threads. A small number of threads is usually sufficient for this pool.
This general purpose thread pool is used for most asynchronous actions on the server side. It maps internally to a java.util.concurrent.ThreadPoolExecutor instance.
The maximum number of thread used by this pool is configure in jbm-configuration.xml with the thread-pool-max-size parameter.
If a value of -1 is used this signifies that the thread pool has no upper bound and new threads will be created on demand if there are enough threads available to satisfy a request. If activity later subsides then threads are timed-out and closed.
If a value of n where nis a positive integer greater than zero is used this signifies that the thread pool is bounded. If more requests come in and there are no free threads in the pool and the pool is full then requests will block until a thread becomes available. It is recommended that a bounded thread pool is used with caution since it can lead to dead-lock situations if the upper bound is chosen to be too low.
The default value for thread-pool-max-size is -1, i.e. the thread pool is unbounded.
See the J2SE javadoc for more information on unbounded (cached), and bounded (fixed) thread pools.
A single thread is also used on the server side to scan for expired messages in queues. We cannot use either of the thread pools for this since this thread needs to run at its own configurable priority.
For more information on configuring the reaper, please see Chapter 21, Message Expiry.
Asynchronous IO has a thread pool for receiving and dispatching events out of the native layer. You will find it on a thread dump with the prefix JBM-AIO-poller-pool. JBoss Messaging uses one thread per opened file on the journal (there is usually one).
There is also a single thread used to invoke writes on libaio. We do that to avoid context switching on libaio what would cause performance issues. You will find this thread on a thread dump with the prefix JBM-AIO-writer-pool.
On the client side, JBoss Messaging maintains a single static scheduled thread pool and a single static general thread pool for use by all clients using the same classloader in that JVM instance.
The static scheduled thread pool has a maximum size of 2 threads, and the general purpose thread pool has an unbounded maximum size.
If required JBoss Messaging can also be configured so that each ClientSessionFactory instance does not use these static pools but instead maintains its own scheduled and general purpose pool. Any sessions created from that ClientSessionFactory will use those pools instead.
To configure a ClientSessionFactory instance to use its own pools, simply use the appropriate setter methods immediately after creation, for example:
ClientSessionFactory myFactory = new ClientSessionFactory(...); myFactory.setUseGlobalPools(false); myFactory.setScheduledThreadPoolMaxSize(10); myFactory.setThreadPoolMaxSize(-1);
If you're using the JMS API, you can set the same parameters directly on the JBossConnectionFactory instance, for example:
JBossConnectionFactory myFactory = new JBossConnectionFactory(...); myFactory.setUseGlobalPools(false); myFactory.setScheduledThreadPoolMaxSize(10); myFactory.setThreadPoolMaxSize(-1);
If you're using JNDI to instantiate JBossConnectionFactory instances, you can also set these parameters in the jbm-jms.xml file where you describe your connection factory, for example:
<connection-factory name="ConnectionFactory"> <connector-ref connector-name="netty"/> <entries> <entry name="ConnectionFactory"/> <entry name="XAConnectionFactory"/> </entries> <use-global-pools>false</use-global-pools> <scheduled-thread-pool-max-size>10</scheduled-thread-pool-max-size> <thread-pool-max-size>-1</thread-pool-max-size> </connection-factory>