public class CachingConnectionFactory extends SingleConnectionFactory
SingleConnectionFactorysubclass that adds
Sessioncaching as well
MessageProducercaching. This ConnectionFactory also switches the
"reconnectOnException" propertyto "true" by default, allowing for automatic recovery of the underlying Connection.
By default, only one single Session will be cached, with further requested
Sessions being created and disposed on demand. Consider raising the
"sessionCacheSize" value in case of a
When using the JMS 1.0.2 API, this ConnectionFactory will switch
into queue/topic mode according to the JMS API methods used at runtime:
lead to queue/topic mode, respectively; generic
calls will lead to a JMS 1.1 connection which is able to serve both modes.
As of Spring Framework 5, this class supports JMS 2.0
calls and therefore requires the JMS 2.0 API to be present at runtime.
It may nevertheless run against a JMS 1.1 driver (bound to the JMS 2.0 API)
as long as no actual JMS 2.0 calls are triggered by the application's setup.
NOTE: This ConnectionFactory requires explicit closing of all Sessions obtained from its shared Connection. This is the usual recommendation for native JMS access code anyway. However, with this ConnectionFactory, its use is mandatory in order to actually allow for Session reuse.
Note also that MessageConsumers obtained from a cached Session won't get
closed until the Session will eventually be removed from the pool. This may
lead to semantic side effects in some cases. For a durable subscriber, the
Session.close() call will also close the subscription.
Re-registering a durable consumer for the same subscription on the same
Session handle is not supported; close and reobtain a cached Session first.
|Constructor and Description|
Create a new CachingConnectionFactory for bean-style usage.
Create a new CachingConnectionFactory for the given target ConnectionFactory.
|Modifier and Type||Method and Description|
Wrap the given Session with a proxy that delegates every method call to it but adapts close calls.
Checks for a cached Session for the given mode.
Return the desired size for the JMS Session cache (per JMS Session type).
Return whether to cache JMS MessageConsumers per JMS Session instance.
Return whether to cache JMS MessageProducers per JMS Session instance.
Resets the Session cache as well.
Specify whether to cache JMS MessageConsumers per JMS Session instance (more specifically: one MessageConsumer per Destination, selector String and Session).
Specify whether to cache JMS MessageProducers per JMS Session instance (more specifically: one MessageProducer per Destination and Session).
Specify the desired size for the JMS Session cache (per JMS Session type).
afterPropertiesSet, closeConnection, createConnection, createConnection, createContext, createContext, createContext, createContext, createQueueConnection, createQueueConnection, createSession, createTopicConnection, createTopicConnection, destroy, doCreateConnection, getClientId, getConnection, getExceptionListener, getSharedConnectionProxy, getTargetConnectionFactory, initConnection, isReconnectOnException, onException, prepareConnection, setClientId, setExceptionListener, setReconnectOnException, setTargetConnectionFactory
public CachingConnectionFactory(ConnectionFactory targetConnectionFactory)
targetConnectionFactory- the target ConnectionFactory
public void setSessionCacheSize(int sessionCacheSize)
This cache size is the maximum limit for the number of cached Sessions per session acknowledgement type (auto, client, dups_ok, transacted). As a consequence, the actual number of cached Sessions may be up to four times as high as the specified value - in the unlikely case of mixing and matching different acknowledgement types.
Default is 1: caching a single Session, (re-)creating further ones on demand. Specify a number like 10 if you'd like to raise the number of cached Sessions; that said, 1 may be sufficient for low-concurrency scenarios.
public int getSessionCacheSize()
public void setCacheProducers(boolean cacheProducers)
Default is "true". Switch this to "false" in order to always recreate MessageProducers on demand.
public boolean isCacheProducers()
public void setCacheConsumers(boolean cacheConsumers)
Default is "true". Switch this to "false" in order to always recreate MessageConsumers on demand.
public boolean isCacheConsumers()
public void resetConnection()
protected Session getSession(Connection con, java.lang.Integer mode) throws JMSException
con- the JMS Connection to operate on
mode- the Session acknowledgement mode (
Session.TRANSACTEDor one of the common modes)
nullto indicate creation of a raw standard Session
JMSException- if thrown by the JMS API
target- the original Session to wrap
sessionList- the List of cached Sessions that the given Session belongs to