SingleConnectionFactory subclass that adds Session caching as well as
MessageProducer and MessageConsumer caching. This ConnectionFactory
also switches the "reconnectOnException" property
to "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
high-concurrency environment.
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:
createQueueConnection and createTopicConnection will
lead to queue/topic mode, respectively; generic createConnection
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 JMSContext
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
logical 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.
Last but not least, MessageProducers and MessageConsumers for temporary queues and topics (TemporaryQueue/TemporaryTopic) will never be cached. Unfortunately, WebLogic JMS happens to implement the temporary queue/topic interfaces on its regular destination implementation, mis-indicating that none of its destinations can be cached. Please use a different connection pool/cache on WebLogic, or customize this class for WebLogic purposes.
Connection, Session, MessageProducer, MessageConsumer
| Modifier and Type | Class and Description |
|---|---|
| private class | CachingConnectionFactory.
Invocation handler for a cached JMS Session proxy. |
| private static class | CachingConnectionFactory.
Simple wrapper class around a Destination and other consumer attributes. |
| private static class | CachingConnectionFactory.
Simple wrapper class around a Destination reference. |
| Modifier and Type | Field and Description |
|---|---|
| private volatile boolean | |
| private boolean | |
| private final ConcurrentMap | |
| private boolean | |
| private int |
| Access | Constructor and Description |
|---|---|
| public | |
| public | CachingConnectionFactory(ConnectionFactory
the target ConnectionFactory targetConnectionFactory)Create a new CachingConnectionFactory for the given target ConnectionFactory. |
| Modifier and Type | Method and Description |
|---|---|
| public int | getCachedSessionCount()
Return a current session count, indicating the number of sessions currently cached by this connection factory. |
| protected Session | Returns: the wrapped Sessionthe original Session to wrap target, Deque<Session> the List of cached Sessions that the given Session belongs to sessionList)Wrap the given Session with a proxy that delegates every method call to it but adapts close calls. |
| protected Session | getSession(Connection
the JMS Connection to operate on con, Integer the Session acknowledgement mode
( mode)Session.TRANSACTED or one of the common modes)Overrides org. Checks for a cached Session for the given mode. |
| public int | |
| public boolean | |
| public boolean | |
| public void | resetConnection()
Overrides org. Resets the Session cache as well. |
| public void | setCacheConsumers(boolean cacheConsumers)
Specify whether to cache JMS MessageConsumers per JMS Session instance (more specifically: one MessageConsumer per Destination, selector String and Session). |
| public void | setCacheProducers(boolean cacheProducers)
Specify whether to cache JMS MessageProducers per JMS Session instance (more specifically: one MessageProducer per Destination and Session). |
| public void | setSessionCacheSize(int sessionCacheSize)
Specify the desired size for the JMS Session cache (per JMS Session type). |
| active | back to summary |
|---|---|
| private volatile boolean active | |
| cacheConsumers | back to summary |
|---|---|
| private boolean cacheConsumers | |
| cachedSessions | back to summary |
|---|---|
| private final ConcurrentMap<Integer, Deque<Session>> cachedSessions | |
| cacheProducers | back to summary |
|---|---|
| private boolean cacheProducers | |
| sessionCacheSize | back to summary |
|---|---|
| private int sessionCacheSize | |
| CachingConnectionFactory | back to summary |
|---|---|
| public CachingConnectionFactory() Create a new CachingConnectionFactory for bean-style usage.
| |
| CachingConnectionFactory | back to summary |
|---|---|
| public CachingConnectionFactory(ConnectionFactory targetConnectionFactory) Create a new CachingConnectionFactory for the given target ConnectionFactory.
| |
| getCachedSessionCount | back to summary |
|---|---|
| public int getCachedSessionCount() Return a current session count, indicating the number of sessions currently cached by this connection factory.
| |
| getCachedSessionProxy | back to summary |
|---|---|
| protected Session getCachedSessionProxy(Session target, Deque<Session> sessionList) Wrap the given Session with a proxy that delegates every method call to it but adapts close calls. This is useful for allowing application code to handle a special framework Session just like an ordinary Session. | |
| getSession | back to summary |
|---|---|
| protected Session getSession(Connection con, Integer mode) throws JMSException Overrides org. Checks for a cached Session for the given mode.
| |
| getSessionCacheSize | back to summary |
|---|---|
| public int getSessionCacheSize() Return the desired size for the JMS Session cache (per JMS Session type). | |
| isCacheConsumers | back to summary |
|---|---|
| public boolean isCacheConsumers() Return whether to cache JMS MessageConsumers per JMS Session instance. | |
| isCacheProducers | back to summary |
|---|---|
| public boolean isCacheProducers() Return whether to cache JMS MessageProducers per JMS Session instance. | |
| resetConnection | back to summary |
|---|---|
| public void resetConnection() Overrides org. Resets the Session cache as well.
| |
| setCacheConsumers | back to summary |
|---|---|
| public void setCacheConsumers(boolean cacheConsumers) Specify whether to cache JMS MessageConsumers per JMS Session instance (more specifically: one MessageConsumer per Destination, selector String and Session). Note that durable subscribers will only be cached until logical closing of the Session handle. Default is "true". Switch this to "false" in order to always recreate MessageConsumers on demand. | |
| setCacheProducers | back to summary |
|---|---|
| public void setCacheProducers(boolean cacheProducers) Specify whether to cache JMS MessageProducers per JMS Session instance (more specifically: one MessageProducer per Destination and Session). Default is "true". Switch this to "false" in order to always recreate MessageProducers on demand. | |
| setSessionCacheSize | back to summary |
|---|---|
| public void setSessionCacheSize(int sessionCacheSize) Specify the desired size for the JMS Session cache (per JMS Session type). 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.
| |
| Modifier and Type | Field and Description |
|---|---|
| private final Map | |
| private final Map | |
| private final Deque | |
| private final Session | |
| private boolean |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| private MessageConsumer | getCachedConsumer(Destination dest, String selector, Boolean noLocal, String subscription, boolean durable)
|
| private MessageProducer | |
| public Object | invoke(Object
the proxy instance that the method was invoked on proxy, Method the method, Object[] Method instance corresponding to
the interface method invoked on the proxy instance. The declaring
class of the Method object will be the interface that
the method was declared in, which may be a superinterface of the
proxy interface that the proxy class inherits the method through.an array of objects containing the values of the
arguments passed in the method invocation on the proxy instance,
or args)null if interface method takes no arguments.
Arguments of primitive types are wrapped in instances of the
appropriate primitive wrapper class, such as
java.lang.Integer or java.lang.Boolean.Implements java. Processes a method invocation on a proxy instance and returns the result. |
| private void | |
| private void |
| cachedConsumers | back to summary |
|---|---|
| private final Map<CachingConnectionFactory. | |
| cachedProducers | back to summary |
|---|---|
| private final Map<CachingConnectionFactory. | |
| sessionList | back to summary |
|---|---|
| private final Deque<Session> sessionList | |
| target | back to summary |
|---|---|
| private final Session target | |
| transactionOpen | back to summary |
|---|---|
| private boolean transactionOpen | |
| CachedSessionInvocationHandler | back to summary |
|---|---|
| public CachedSessionInvocationHandler(Session target, Deque<Session> sessionList) | |
| getCachedConsumer | back to summary |
|---|---|
private MessageConsumer getCachedConsumer(Destination dest, String selector, Boolean noLocal, String subscription, boolean durable) throws JMSException
| |
| getCachedProducer | back to summary |
|---|---|
| private MessageProducer getCachedProducer(Destination dest) throws JMSException | |
| invoke | back to summary |
|---|---|
| public Object invoke(Object proxy, Method method, Object[] args) throws Throwable Implements java. Doc from java. Processes a method invocation on a proxy instance and returns the result. This method will be invoked on an invocation handler when a method is invoked on a proxy instance that it is associated with.
| |
| logicalClose | back to summary |
|---|---|
| private void logicalClose(Session proxy) throws JMSException | |
| physicalClose | back to summary |
|---|---|
| private void physicalClose() throws JMSException | |
| Modifier and Type | Field and Description |
|---|---|
| private final boolean | |
| private final Boolean | |
| private final String | |
| private final String |
| Access | Constructor and Description |
|---|---|
| public | ConsumerCacheKey(Destination destination, String selector, Boolean noLocal, String subscription, boolean durable)
|
| Modifier and Type | Method and Description |
|---|---|
| public boolean | equals(Object
the reference object with which to compare. other)Overrides org. Indicates whether some other object is "equal to" this one. |
| public int | hashCode()
Overrides org. Returns a hash code value for this object. |
| public String | toString()
Overrides org. Returns a string representation of the object. |
| durable | back to summary |
|---|---|
| private final boolean durable | |
| noLocal | back to summary |
|---|---|
| private final Boolean noLocal
| |
| selector | back to summary |
|---|---|
| private final String selector
| |
| subscription | back to summary |
|---|---|
| private final String subscription
| |
| ConsumerCacheKey | back to summary |
|---|---|
| public ConsumerCacheKey(Destination destination, String selector, Boolean noLocal, String subscription, boolean durable) | |
| equals | back to summary |
|---|---|
| public boolean equals(Object other) Overrides org. Doc from java. Indicates whether some other object is "equal to" this one.
The
An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes. | |
| hashCode | back to summary |
|---|---|
| public int hashCode() Overrides org. Doc from java. Returns a hash code value for this object. This method is
supported for the benefit of hash tables such as those provided by
The general contract of
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides org. Doc from java. Returns a string representation of the object.
Satisfying this method's contract implies a non- | |
| Modifier and Type | Field and Description |
|---|---|
| private final Destination | |
| private String |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public int | compareTo(CachingConnectionFactory.
the object to be compared. otherImplements java. Compares this object with the specified object for order. |
| protected boolean | |
| public boolean | equals(Object
the reference object with which to compare. other)Overrides java. Indicates whether some other object is "equal to" this one. |
| private String | |
| public int | |
| public String |
| destination | back to summary |
|---|---|
| private final Destination destination | |
| destinationString | back to summary |
|---|---|
| private String destinationString
| |
| DestinationCacheKey | back to summary |
|---|---|
| public DestinationCacheKey(Destination destination) | |
| compareTo | back to summary |
|---|---|
| public int compareTo(CachingConnectionFactory. Implements java. Doc from java. Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. The implementor must ensure The implementor must also ensure that the relation is transitive:
Finally, the implementor must ensure that
| |
| destinationEquals | back to summary |
|---|---|
| protected boolean destinationEquals(CachingConnectionFactory. | |
| equals | back to summary |
|---|---|
| public boolean equals(Object other) Overrides java. Doc from java. Indicates whether some other object is "equal to" this one.
The
An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes. | |
| getDestinationString | back to summary |
|---|---|
| private String getDestinationString() | |
| hashCode | back to summary |
|---|---|
| public int hashCode() Overrides java. Doc from java. Returns a hash code value for this object. This method is
supported for the benefit of hash tables such as those provided by
The general contract of
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object.
Satisfying this method's contract implies a non- | |