createConnection() calls, and ignores calls to
jakarta.jms.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 org.
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.
org.springframework.jms.core.JmsTemplate , org.springframework.jms.listener.SimpleMessageListenerContainer , org.springframework.jms.listener.DefaultMessageListenerContainer#setCacheLevel
| Modifier and Type | Class and Description |
|---|---|
| private class | SingleConnectionFactory.
Internal aggregated ExceptionListener for handling the internal recovery listener in combination with user-specified listeners. |
| private class | SingleConnectionFactory.
Invocation handler for a cached JMS Connection proxy. |
| Modifier and Type | Field and Description |
|---|---|
| private SingleConnectionFactory. | aggregatedExceptionListener
An internal aggregator allowing for per-connection ExceptionListeners. |
| private String | |
| private Connection | connection
The target Connection. |
| private final Object | connectionMonitor
Synchronization monitor for the shared Connection. |
| private ExceptionListener | |
| protected final Log | |
| private Boolean | pubSubMode
A hint whether to create a queue or topic connection. |
| private boolean | |
| private int | startedCount
Whether the shared Connection has been started. |
| private ConnectionFactory |
| Access | Constructor and Description |
|---|---|
| public | |
| public | SingleConnectionFactory(Connection
the single Connection targetConnection)Create a new SingleConnectionFactory that always returns the given Connection. |
| public | SingleConnectionFactory(ConnectionFactory
the target ConnectionFactory targetConnectionFactory)Create a new SingleConnectionFactory that always returns a single Connection that it will lazily create via the given target ConnectionFactory. |
| Modifier and Type | Method and Description |
|---|---|
| public void | afterPropertiesSet()
Implements org. Make sure a Connection or ConnectionFactory has been set. |
| protected void | |
| public Connection | createConnection()
Implements jakarta. Creates a connection with the default user identity. |
| public Connection | createConnection(String
the caller's user name username, String the caller's password password)Implements jakarta. Creates a connection with the specified user identity. |
| public JMSContext | createContext()
Implements jakarta. Creates a JMSContext with the default user identity and an unspecified sessionMode. |
| public JMSContext | createContext(String
the caller's user name userName, String the caller's password password)Implements jakarta. Creates a JMSContext with the specified user identity and an unspecified sessionMode. |
| public JMSContext | createContext(String
the caller's user name userName, String the caller's password password, int indicates which of four possible session modes will be used.
sessionMode)
Implements jakarta. Creates a JMSContext with the specified user identity and the specified session mode. |
| public JMSContext | createContext(int
indicates which of four possible session modes will be used.
sessionMode)
Implements jakarta. Creates a JMSContext with the default user identity and the specified session mode. |
| public QueueConnection | createQueueConnection()
Implements jakarta. Creates a queue connection with the default user identity. |
| public QueueConnection | createQueueConnection(String
the caller's user name username, String the caller's password password)Implements jakarta. Creates a queue connection with the specified user identity. |
| protected Session | Returns: the newly created Sessionthe JMS Connection to operate on con, Integer the Session acknowledgement mode
( mode)Session.TRANSACTED or one of the common modes)Create a default Session for this ConnectionFactory, adapting to JMS 1.0.2 style queue/topic mode if necessary. |
| public TopicConnection | createTopicConnection()
Implements jakarta. Creates a topic connection with the default user identity. |
| public TopicConnection | createTopicConnection(String
the caller's user name username, String the caller's password password)Implements jakarta. Creates a topic connection with the specified user identity. |
| public void | destroy()
Implements org. Close the underlying shared connection. |
| protected Connection | Returns: the new JMS ConnectionCreate a JMS Connection via this template's ConnectionFactory. |
| protected String | getClientId()
Return a JMS client ID for the single Connection created and exposed by this ConnectionFactory, if any. |
| protected Connection | |
| protected ExceptionListener | getExceptionListener()
Return the JMS ExceptionListener implementation that should be registered with the single Connection created by this factory, if any. |
| protected Session | Returns: the Session to use, ornull to indicate
creation of a raw standard Sessionthe JMS Connection to operate on con, Integer the Session acknowledgement mode
( mode)Session.TRANSACTED or one of the common modes)Template method for obtaining a (potentially cached) Session. |
| protected Connection | Returns: the wrapped Connectionthe original Connection to wrap target)Wrap the given Connection with a proxy that delegates every method call to it but suppresses close calls. |
| public ConnectionFactory | getTargetConnectionFactory()
Return the target ConnectionFactory which will be used to lazily create a single Connection, if any. |
| public void | |
| protected boolean | isReconnectOnException()
Return whether the single Connection should be renewed when a JMSException is reported by the underlying Connection. |
| public boolean | isRunning()
Implements org. Check whether there is currently an underlying connection. |
| private ConnectionFactory | |
| public void | onException(JMSException
the Jakarta Messaging exception ex)Implements jakarta. Exception listener callback that renews the underlying single Connection. |
| protected void | prepareConnection(Connection
the Connection to prepare con)Prepare the given Connection before it is exposed. |
| public void | |
| public void | setClientId(String clientId)
Specify a JMS client ID for the single Connection created and exposed by this ConnectionFactory. |
| public void | setExceptionListener(ExceptionListener exceptionListener)
Specify an JMS ExceptionListener implementation that should be registered with the single Connection created by this factory. |
| public 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. |
| public void | setTargetConnectionFactory(ConnectionFactory targetConnectionFactory)
Set the target ConnectionFactory which will be used to lazily create a single Connection. |
| public void | start()
Implements org. Initialize the underlying shared connection on start. |
| public void | stop()
Implements org. Reset the underlying shared connection on stop. |