Class ConnectionFactoryUtils
java.lang.Object
org.springframework.jms.connection.ConnectionFactoryUtils
Helper class for managing a JMS
ConnectionFactory, in particular
for obtaining transactional JMS resources for a given ConnectionFactory.
Mainly for internal use within the framework. Used by
JmsTemplate as well as
DefaultMessageListenerContainer.
- Since:
- 2.0
- Author:
- Juergen Hoeller
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceCallback interface for resource creation. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic @Nullable jakarta.jms.SessiondoGetTransactionalSession(jakarta.jms.ConnectionFactory connectionFactory, ConnectionFactoryUtils.ResourceFactory resourceFactory) Obtain a JMS Session that is synchronized with the current transaction, if any.static @Nullable jakarta.jms.SessiondoGetTransactionalSession(jakarta.jms.ConnectionFactory connectionFactory, ConnectionFactoryUtils.ResourceFactory resourceFactory, boolean startConnection) Obtain a JMS Session that is synchronized with the current transaction, if any.static jakarta.jms.SessiongetTargetSession(jakarta.jms.Session session) Return the innermost target Session of the given Session.static @Nullable jakarta.jms.QueueSessiongetTransactionalQueueSession(jakarta.jms.QueueConnectionFactory cf, @Nullable jakarta.jms.QueueConnection existingCon, boolean synchedLocalTransactionAllowed) Obtain a JMS QueueSession that is synchronized with the current transaction, if any.static @Nullable jakarta.jms.SessiongetTransactionalSession(jakarta.jms.ConnectionFactory cf, @Nullable jakarta.jms.Connection existingCon, boolean synchedLocalTransactionAllowed) Obtain a JMS Session that is synchronized with the current transaction, if any.static @Nullable jakarta.jms.TopicSessiongetTransactionalTopicSession(jakarta.jms.TopicConnectionFactory cf, @Nullable jakarta.jms.TopicConnection existingCon, boolean synchedLocalTransactionAllowed) Obtain a JMS TopicSession that is synchronized with the current transaction, if any.static booleanisSessionTransactional(@Nullable jakarta.jms.Session session, @Nullable jakarta.jms.ConnectionFactory cf) Determine whether the given JMS Session is transactional, that is, bound to the current thread by Spring's transaction facilities.static voidreleaseConnection(@Nullable jakarta.jms.Connection con, @Nullable jakarta.jms.ConnectionFactory cf, boolean started) Release the given Connection, stopping it (if necessary) and eventually closing it.
-
Constructor Details
-
ConnectionFactoryUtils
public ConnectionFactoryUtils()
-
-
Method Details
-
releaseConnection
public static void releaseConnection(@Nullable jakarta.jms.Connection con, @Nullable jakarta.jms.ConnectionFactory cf, boolean started) Release the given Connection, stopping it (if necessary) and eventually closing it.Checks
SmartConnectionFactory.shouldStop(Connection), if available. This is essentially a more sophisticated version ofJmsUtils.closeConnection(Connection).- Parameters:
con- the Connection to release (if this isnull, the call will be ignored)cf- the ConnectionFactory that the Connection was obtained from (may benull)started- whether the Connection might have been started by the application- See Also:
-
getTargetSession
public static jakarta.jms.Session getTargetSession(jakarta.jms.Session session) Return the innermost target Session of the given Session. If the given Session is a proxy, it will be unwrapped until a non-proxy Session is found. Otherwise, the passed-in Session will be returned as-is.- Parameters:
session- the Session proxy to unwrap- Returns:
- the innermost target Session, or the passed-in one if no proxy
- See Also:
-
isSessionTransactional
public static boolean isSessionTransactional(@Nullable jakarta.jms.Session session, @Nullable jakarta.jms.ConnectionFactory cf) Determine whether the given JMS Session is transactional, that is, bound to the current thread by Spring's transaction facilities.- Parameters:
session- the JMS Session to checkcf- the JMS ConnectionFactory that the Session originated from- Returns:
- whether the Session is transactional
-
getTransactionalSession
public static @Nullable jakarta.jms.Session getTransactionalSession(jakarta.jms.ConnectionFactory cf, @Nullable jakarta.jms.Connection existingCon, boolean synchedLocalTransactionAllowed) throws jakarta.jms.JMSException Obtain a JMS Session that is synchronized with the current transaction, if any.- Parameters:
cf- the ConnectionFactory to obtain a Session forexistingCon- the existing JMS Connection to obtain a Session for (may benull)synchedLocalTransactionAllowed- whether to allow for a local JMS transaction that is synchronized with a Spring-managed transaction (where the main transaction might be a JDBC-based one for a specific DataSource, for example), with the JMS transaction committing right after the main transaction. If not allowed, the given ConnectionFactory needs to handle transaction enlistment underneath the covers.- Returns:
- the transactional Session, or
nullif none found - Throws:
jakarta.jms.JMSException- in case of JMS failure
-
getTransactionalQueueSession
public static @Nullable jakarta.jms.QueueSession getTransactionalQueueSession(jakarta.jms.QueueConnectionFactory cf, @Nullable jakarta.jms.QueueConnection existingCon, boolean synchedLocalTransactionAllowed) throws jakarta.jms.JMSException Obtain a JMS QueueSession that is synchronized with the current transaction, if any.Mainly intended for use with the JMS 1.0.2 API.
- Parameters:
cf- the ConnectionFactory to obtain a Session forexistingCon- the existing JMS Connection to obtain a Session for (may benull)synchedLocalTransactionAllowed- whether to allow for a local JMS transaction that is synchronized with a Spring-managed transaction (where the main transaction might be a JDBC-based one for a specific DataSource, for example), with the JMS transaction committing right after the main transaction. If not allowed, the given ConnectionFactory needs to handle transaction enlistment underneath the covers.- Returns:
- the transactional Session, or
nullif none found - Throws:
jakarta.jms.JMSException- in case of JMS failure
-
getTransactionalTopicSession
public static @Nullable jakarta.jms.TopicSession getTransactionalTopicSession(jakarta.jms.TopicConnectionFactory cf, @Nullable jakarta.jms.TopicConnection existingCon, boolean synchedLocalTransactionAllowed) throws jakarta.jms.JMSException Obtain a JMS TopicSession that is synchronized with the current transaction, if any.Mainly intended for use with the JMS 1.0.2 API.
- Parameters:
cf- the ConnectionFactory to obtain a Session forexistingCon- the existing JMS Connection to obtain a Session for (may benull)synchedLocalTransactionAllowed- whether to allow for a local JMS transaction that is synchronized with a Spring-managed transaction (where the main transaction might be a JDBC-based one for a specific DataSource, for example), with the JMS transaction committing right after the main transaction. If not allowed, the given ConnectionFactory needs to handle transaction enlistment underneath the covers.- Returns:
- the transactional Session, or
nullif none found - Throws:
jakarta.jms.JMSException- in case of JMS failure
-
doGetTransactionalSession
public static @Nullable jakarta.jms.Session doGetTransactionalSession(jakarta.jms.ConnectionFactory connectionFactory, ConnectionFactoryUtils.ResourceFactory resourceFactory) throws jakarta.jms.JMSException Obtain a JMS Session that is synchronized with the current transaction, if any.This
doGetTransactionalSessionvariant always starts the underlying JMS Connection, assuming that the Session will be used for receiving messages.- Parameters:
connectionFactory- the JMS ConnectionFactory to bind for (used as TransactionSynchronizationManager key)resourceFactory- the ResourceFactory to use for extracting or creating JMS resources- Returns:
- the transactional Session, or
nullif none found - Throws:
jakarta.jms.JMSException- in case of JMS failure- See Also:
-
doGetTransactionalSession
public static @Nullable jakarta.jms.Session doGetTransactionalSession(jakarta.jms.ConnectionFactory connectionFactory, ConnectionFactoryUtils.ResourceFactory resourceFactory, boolean startConnection) throws jakarta.jms.JMSException Obtain a JMS Session that is synchronized with the current transaction, if any.- Parameters:
connectionFactory- the JMS ConnectionFactory to bind for (used as TransactionSynchronizationManager key)resourceFactory- the ResourceFactory to use for extracting or creating JMS resourcesstartConnection- whether the underlying JMS Connection approach should be started in order to allow for receiving messages. Note that a reused Connection may already have been started before, even if this flag isfalse.- Returns:
- the transactional Session, or
nullif none found - Throws:
jakarta.jms.JMSException- in case of JMS failure
-