Class SingleConnectionFactory
- All Implemented Interfaces:
- ConnectionFactory,- ExceptionListener,- QueueConnectionFactory,- TopicConnectionFactory,- DisposableBean,- InitializingBean,- Lifecycle
- Direct Known Subclasses:
- CachingConnectionFactory
createConnection() calls, and ignores calls to
 Connection.close(). According to the JMS Connection
 model, this is perfectly thread-safe (in contrast to e.g. JDBC). The
 shared Connection can be automatically recovered in case of an Exception.
 You can either pass in a specific JMS Connection directly or let this factory lazily create a Connection via a given target ConnectionFactory. This factory generally works with JMS 1.1 as well as the JMS 1.0.2 API.
Note that 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.
 
Useful for testing and standalone environments in order to keep using the
 same Connection for multiple JmsTemplate
 calls, without having a pooling ConnectionFactory underneath. This may span
 any number of transactions, even concurrently executing transactions.
 
Note that Spring's message listener containers support the use of a shared Connection within each listener container instance. Using SingleConnectionFactory in combination only really makes sense for sharing a single JMS Connection across multiple listener containers.
- Since:
- 1.1
- Author:
- Juergen Hoeller, Mark Pollack
- See Also:
- 
Field SummaryFields
- 
Constructor SummaryConstructorsConstructorDescriptionCreate a new SingleConnectionFactory for bean-style usage.SingleConnectionFactory(Connection targetConnection) Create a new SingleConnectionFactory that always returns the given Connection.SingleConnectionFactory(ConnectionFactory targetConnectionFactory) Create a new SingleConnectionFactory that always returns a single Connection that it will lazily create via the given target ConnectionFactory.
- 
Method SummaryModifier and TypeMethodDescriptionvoidMake sure a Connection or ConnectionFactory has been set.protected voidClose the given Connection.createConnection(String username, String password) createContext(int sessionMode) createContext(String userName, String password) createContext(String userName, String password, int sessionMode) createQueueConnection(String username, String password) protected SessioncreateSession(Connection con, Integer mode) Create a default Session for this ConnectionFactory, adapting to JMS 1.0.2 style queue/topic mode if necessary.createTopicConnection(String username, String password) voiddestroy()Close the underlying shared connection.protected ConnectionCreate a JMS Connection via this template's ConnectionFactory.protected StringReturn a JMS client ID for the single Connection created and exposed by this ConnectionFactory, if any.protected ConnectionObtain an initialized shared Connection.protected ExceptionListenerReturn the JMS ExceptionListener implementation that should be registered with the single Connection created by this factory, if any.protected SessiongetSession(Connection con, Integer mode) Template method for obtaining a (potentially cached) Session.protected ConnectiongetSharedConnectionProxy(Connection target) Wrap the given Connection with a proxy that delegates every method call to it but suppresses close calls.Return the target ConnectionFactory which will be used to lazily create a single Connection, if any.voidInitialize the underlying shared Connection.protected booleanReturn whether the single Connection should be renewed when a JMSException is reported by the underlying Connection.booleanCheck whether there is currently an underlying connection.voidException listener callback that renews the underlying single Connection.protected voidPrepare the given Connection before it is exposed.voidReset the underlying shared Connection, to be reinitialized on next access.voidsetClientId(String clientId) Specify a JMS client ID for the single Connection created and exposed by this ConnectionFactory.voidsetExceptionListener(ExceptionListener exceptionListener) Specify an JMS ExceptionListener implementation that should be registered with the single Connection created by this factory.voidsetReconnectOnException(boolean reconnectOnException) Specify whether the single Connection should be reset (to be subsequently renewed) when a JMSException is reported by the underlying Connection.voidsetTargetConnectionFactory(ConnectionFactory targetConnectionFactory) Set the target ConnectionFactory which will be used to lazily create a single Connection.voidstart()Initialize the underlying shared connection on start.voidstop()Reset the underlying shared connection on stop.
- 
Field Details- 
logger
 
- 
- 
Constructor Details- 
SingleConnectionFactorypublic SingleConnectionFactory()Create a new SingleConnectionFactory for bean-style usage.
- 
SingleConnectionFactoryCreate a new SingleConnectionFactory that always returns the given Connection.- Parameters:
- targetConnection- the single Connection
 
- 
SingleConnectionFactoryCreate a new SingleConnectionFactory that always returns a single Connection that it will lazily create via the given target ConnectionFactory.- Parameters:
- targetConnectionFactory- the target ConnectionFactory
 
 
- 
- 
Method Details- 
setTargetConnectionFactorySet the target ConnectionFactory which will be used to lazily create a single Connection.
- 
getTargetConnectionFactoryReturn the target ConnectionFactory which will be used to lazily create a single Connection, if any.
- 
setClientIdSpecify a JMS client ID for the single Connection created and exposed by this ConnectionFactory.Note that client IDs need to be unique among all active Connections of the underlying JMS provider. Furthermore, a client ID can only be assigned if the original ConnectionFactory hasn't already assigned one. 
- 
getClientIdReturn a JMS client ID for the single Connection created and exposed by this ConnectionFactory, if any.
- 
setExceptionListenerSpecify an JMS ExceptionListener implementation that should be registered with the single Connection created by this factory.- See Also:
 
- 
getExceptionListenerReturn the JMS ExceptionListener implementation that should be registered with the single Connection created by this factory, if any.
- 
setReconnectOnExceptionpublic void setReconnectOnException(boolean reconnectOnException) Specify whether the single Connection should be reset (to be subsequently renewed) when a JMSException is reported by the underlying Connection.Default is "false". Switch this to "true" to automatically trigger recovery based on your JMS provider's exception notifications. Internally, this will lead to a special JMS ExceptionListener (this SingleConnectionFactory itself) being registered with the underlying Connection. This can also be combined with a user-specified ExceptionListener, if desired. 
- 
isReconnectOnExceptionprotected boolean isReconnectOnException()Return whether the single Connection should be renewed when a JMSException is reported by the underlying Connection.
- 
afterPropertiesSetpublic void afterPropertiesSet()Make sure a Connection or ConnectionFactory has been set.- Specified by:
- afterPropertiesSetin interface- InitializingBean
 
- 
createConnection- Specified by:
- createConnectionin interface- ConnectionFactory
- Throws:
- JMSException
 
- 
createConnection- Specified by:
- createConnectionin interface- ConnectionFactory
- Throws:
- JMSException
 
- 
createQueueConnection- Specified by:
- createQueueConnectionin interface- QueueConnectionFactory
- Throws:
- JMSException
 
- 
createQueueConnection- Specified by:
- createQueueConnectionin interface- QueueConnectionFactory
- Throws:
- JMSException
 
- 
createTopicConnection- Specified by:
- createTopicConnectionin interface- TopicConnectionFactory
- Throws:
- JMSException
 
- 
createTopicConnection- Specified by:
- createTopicConnectionin interface- TopicConnectionFactory
- Throws:
- JMSException
 
- 
createContext- Specified by:
- createContextin interface- ConnectionFactory
 
- 
createContext- Specified by:
- createContextin interface- ConnectionFactory
 
- 
createContext- Specified by:
- createContextin interface- ConnectionFactory
 
- 
createContext- Specified by:
- createContextin interface- ConnectionFactory
 
- 
getConnectionObtain an initialized shared Connection.- Returns:
- the Connection (never null)
- Throws:
- JMSException- if thrown by JMS API methods
- See Also:
 
- 
onExceptionException listener callback that renews the underlying single Connection.- Specified by:
- onExceptionin interface- ExceptionListener
- See Also:
 
- 
destroypublic void destroy()Close the underlying shared connection. The provider of this ConnectionFactory needs to care for proper shutdown.As this bean implements DisposableBean, a bean factory will automatically invoke this on destruction of its cached singletons. - Specified by:
- destroyin interface- DisposableBean
- See Also:
 
- 
startpublic void start()Initialize the underlying shared connection on start.
- 
stoppublic void stop()Reset the underlying shared connection on stop.
- 
isRunningpublic boolean isRunning()Check whether there is currently an underlying connection.
- 
initConnectionInitialize the underlying shared Connection.Closes and reinitializes the Connection if an underlying Connection is present already. - Throws:
- JMSException- if thrown by JMS API methods
- See Also:
 
- 
doCreateConnectionCreate a JMS Connection via this template's ConnectionFactory.- Returns:
- the new JMS Connection
- Throws:
- JMSException- if thrown by JMS API methods
 
- 
prepareConnectionPrepare the given Connection before it is exposed.The default implementation applies ExceptionListener and client id. Can be overridden in subclasses. - Parameters:
- con- the Connection to prepare
- Throws:
- JMSException- if thrown by JMS API methods
- See Also:
 
- 
getSessionTemplate method for obtaining a (potentially cached) Session.The default implementation always returns null. Subclasses may override this for exposing specific Session handles, possibly delegating tocreateSession(jakarta.jms.Connection, java.lang.Integer)for the creation of raw Session objects that will then get wrapped and returned from here.- Parameters:
- con- the JMS Connection to operate on
- mode- the Session acknowledgement mode (- Session.TRANSACTEDor one of the common modes)
- Returns:
- the Session to use, or nullto indicate creation of a raw standard Session
- Throws:
- JMSException- if thrown by the JMS API
 
- 
createSessionCreate a default Session for this ConnectionFactory, adapting to JMS 1.0.2 style queue/topic mode if necessary.- Parameters:
- con- the JMS Connection to operate on
- mode- the Session acknowledgement mode (- Session.TRANSACTEDor one of the common modes)
- Returns:
- the newly created Session
- Throws:
- JMSException- if thrown by the JMS API
 
- 
resetConnectionpublic void resetConnection()Reset the underlying shared Connection, to be reinitialized on next access.- See Also:
 
- 
closeConnectionClose the given Connection.- Parameters:
- con- the Connection to close
 
 
-