Top Description Inners Fields Constructors Methods
org.springframework.jms.connection

public abstract Class ConnectionFactoryUtils

extends Object
Class Inheritance
Imports
jakarta.jms.Connection, .ConnectionFactory, .JMSException, .QueueConnection, .QueueConnectionFactory, .QueueSession, .Session, .TopicConnection, .TopicConnectionFactory, .TopicSession, org.apache.commons.logging.Log, .LogFactory, org.springframework.lang.Nullable, org.springframework.transaction.support.ResourceHolderSynchronization, .TransactionSynchronizationManager, org.springframework.util.Assert

Helper class for managing a JMS jakarta.jms.ConnectionFactory, in particular for obtaining transactional JMS resources for a given ConnectionFactory.

Mainly for internal use within the framework. Used by org.springframework.jms.core.JmsTemplate as well as org.springframework.jms.listener.DefaultMessageListenerContainer.

Author
Juergen Hoeller
Since
2.0
See Also
SmartConnectionFactory

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
ConnectionFactoryUtils.JmsResourceSynchronization

Callback for resource cleanup at the end of a non-native JMS transaction (e.g. when participating in a JtaTransactionManager transaction).

public static interface
ConnectionFactoryUtils.ResourceFactory

Callback interface for resource creation.

Field Summary

Modifier and TypeField and Description
private static final Log

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static Session

Returns:

the transactional Session, or null if none found
doGetTransactionalSession
(ConnectionFactory
the JMS ConnectionFactory to bind for (used as TransactionSynchronizationManager key)
connectionFactory
,
ConnectionFactoryUtils.ResourceFactory
the ResourceFactory to use for extracting or creating JMS resources
resourceFactory
)

Obtain a JMS Session that is synchronized with the current transaction, if any.

public static Session

Returns:

the transactional Session, or null if none found
doGetTransactionalSession
(ConnectionFactory
the JMS ConnectionFactory to bind for (used as TransactionSynchronizationManager key)
connectionFactory
,
ConnectionFactoryUtils.ResourceFactory
the ResourceFactory to use for extracting or creating JMS resources
resourceFactory
,
boolean
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 is false.
startConnection
)

Obtain a JMS Session that is synchronized with the current transaction, if any.

public static Session

Returns:

the innermost target Session, or the passed-in one if no proxy
getTargetSession
(Session
the Session proxy to unwrap
session
)

Return the innermost target Session of the given Session.

public static QueueSession

Returns:

the transactional Session, or null if none found
getTransactionalQueueSession
(final QueueConnectionFactory
the ConnectionFactory to obtain a Session for
cf
,
final QueueConnection
the existing JMS Connection to obtain a Session for (may be null)
existingCon
,
final boolean
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.
synchedLocalTransactionAllowed
)

Obtain a JMS QueueSession that is synchronized with the current transaction, if any.

public static Session

Returns:

the transactional Session, or null if none found
getTransactionalSession
(final ConnectionFactory
the ConnectionFactory to obtain a Session for
cf
,
final Connection
the existing JMS Connection to obtain a Session for (may be null)
existingCon
,
final boolean
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.
synchedLocalTransactionAllowed
)

Obtain a JMS Session that is synchronized with the current transaction, if any.

public static TopicSession

Returns:

the transactional Session, or null if none found
getTransactionalTopicSession
(final TopicConnectionFactory
the ConnectionFactory to obtain a Session for
cf
,
final TopicConnection
the existing JMS Connection to obtain a Session for (may be null)
existingCon
,
final boolean
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.
synchedLocalTransactionAllowed
)

Obtain a JMS TopicSession that is synchronized with the current transaction, if any.

public static boolean

Returns:

whether the Session is transactional
isSessionTransactional
(Session
the JMS Session to check
session
,
ConnectionFactory
the JMS ConnectionFactory that the Session originated from
cf
)

Determine whether the given JMS Session is transactional, that is, bound to the current thread by Spring's transaction facilities.

public static void
releaseConnection(Connection
the Connection to release (if this is null, the call will be ignored)
con
,
ConnectionFactory
the ConnectionFactory that the Connection was obtained from (may be null)
cf
,
boolean
whether the Connection might have been started by the application
started
)

Release the given Connection, stopping it (if necessary) and eventually closing it.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

loggerback to summary
private static final Log logger

Constructor Detail

ConnectionFactoryUtilsback to summary
public ConnectionFactoryUtils()

Method Detail

doGetTransactionalSessionback to summary
public static Session doGetTransactionalSession(ConnectionFactory connectionFactory, ConnectionFactoryUtils.ResourceFactory resourceFactory) throws JMSException

Obtain a JMS Session that is synchronized with the current transaction, if any.

This doGetTransactionalSession variant always starts the underlying JMS Connection, assuming that the Session will be used for receiving messages.

Parameters
connectionFactory:ConnectionFactory

the JMS ConnectionFactory to bind for (used as TransactionSynchronizationManager key)

resourceFactory:ConnectionFactoryUtils.ResourceFactory

the ResourceFactory to use for extracting or creating JMS resources

Returns:Session

the transactional Session, or null if none found

Annotations
@Nullable
Exceptions
JMSException:
in case of JMS failure
See Also
doGetTransactionalSession(jakarta.jms.ConnectionFactory, ResourceFactory, boolean)
doGetTransactionalSessionback to summary
public static Session doGetTransactionalSession(ConnectionFactory connectionFactory, ConnectionFactoryUtils.ResourceFactory resourceFactory, boolean startConnection) throws JMSException

Obtain a JMS Session that is synchronized with the current transaction, if any.

Parameters
connectionFactory:ConnectionFactory

the JMS ConnectionFactory to bind for (used as TransactionSynchronizationManager key)

resourceFactory:ConnectionFactoryUtils.ResourceFactory

the ResourceFactory to use for extracting or creating JMS resources

startConnection:boolean

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 is false.

Returns:Session

the transactional Session, or null if none found

Annotations
@Nullable
Exceptions
JMSException:
in case of JMS failure
getTargetSessionback to summary
public static Session getTargetSession(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:Session

the Session proxy to unwrap

Returns:Session

the innermost target Session, or the passed-in one if no proxy

See Also
SessionProxy#getTargetSession()
getTransactionalQueueSessionback to summary
public static QueueSession getTransactionalQueueSession(final QueueConnectionFactory cf, final QueueConnection existingCon, final boolean synchedLocalTransactionAllowed) throws 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:QueueConnectionFactory

the ConnectionFactory to obtain a Session for

existingCon:QueueConnection

the existing JMS Connection to obtain a Session for (may be null)

synchedLocalTransactionAllowed:boolean

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:QueueSession

the transactional Session, or null if none found

Annotations
@Nullable
Exceptions
JMSException:
in case of JMS failure
getTransactionalSessionback to summary
public static Session getTransactionalSession(final ConnectionFactory cf, final Connection existingCon, final boolean synchedLocalTransactionAllowed) throws JMSException

Obtain a JMS Session that is synchronized with the current transaction, if any.

Parameters
cf:ConnectionFactory

the ConnectionFactory to obtain a Session for

existingCon:Connection

the existing JMS Connection to obtain a Session for (may be null)

synchedLocalTransactionAllowed:boolean

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:Session

the transactional Session, or null if none found

Annotations
@Nullable
Exceptions
JMSException:
in case of JMS failure
getTransactionalTopicSessionback to summary
public static TopicSession getTransactionalTopicSession(final TopicConnectionFactory cf, final TopicConnection existingCon, final boolean synchedLocalTransactionAllowed) throws 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:TopicConnectionFactory

the ConnectionFactory to obtain a Session for

existingCon:TopicConnection

the existing JMS Connection to obtain a Session for (may be null)

synchedLocalTransactionAllowed:boolean

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:TopicSession

the transactional Session, or null if none found

Annotations
@Nullable
Exceptions
JMSException:
in case of JMS failure
isSessionTransactionalback to summary
public static boolean isSessionTransactional(Session session, ConnectionFactory cf)

Determine whether the given JMS Session is transactional, that is, bound to the current thread by Spring's transaction facilities.

Parameters
session:Session

the JMS Session to check

cf:ConnectionFactory

the JMS ConnectionFactory that the Session originated from

Returns:boolean

whether the Session is transactional

releaseConnectionback to summary
public static void releaseConnection(Connection con, ConnectionFactory cf, boolean started)

Release the given Connection, stopping it (if necessary) and eventually closing it.

Checks SmartConnectionFactory#shouldStop, if available. This is essentially a more sophisticated version of org.springframework.jms.support.JmsUtils#closeConnection.

Parameters
con:Connection

the Connection to release (if this is null, the call will be ignored)

cf:ConnectionFactory

the ConnectionFactory that the Connection was obtained from (may be null)

started:boolean

whether the Connection might have been started by the application

See Also
SmartConnectionFactory#shouldStop, org.springframework.jms.support.JmsUtils#closeConnection
org.springframework.jms.connection back to summary

private Class ConnectionFactoryUtils.JmsResourceSynchronization

extends ResourceHolderSynchronization<JmsResourceHolder, Object>
Class Inheritance

Callback for resource cleanup at the end of a non-native JMS transaction (e.g. when participating in a JtaTransactionManager transaction).
See Also
org.springframework.transaction.jta.JtaTransactionManager

Field Summary

Modifier and TypeField and Description
private boolean
private final boolean

Constructor Summary

AccessConstructor and Description
public
JmsResourceSynchronization(JmsResourceHolder resourceHolder, Object resourceKey, boolean transacted)

Method Summary

Modifier and TypeMethod and Description
public void
afterCompletion(int
completion status according to the STATUS_* constants
status
)

Overrides org.springframework.transaction.support.ResourceHolderSynchronization.afterCompletion.

Overrides default org.springframework.transaction.support.TransactionSynchronization.afterCompletion.

Invoked after transaction commit/rollback.

protected void
processResourceAfterCommit(JmsResourceHolder
the resource holder to process
resourceHolder
)

Overrides org.springframework.transaction.support.ResourceHolderSynchronization.processResourceAfterCommit.

After-commit callback for the given resource holder.

protected void
releaseResource(JmsResourceHolder
the resource holder to process
resourceHolder
,
Object
the key that the ResourceHolder was bound for
resourceKey
)

Overrides org.springframework.transaction.support.ResourceHolderSynchronization.releaseResource.

Release the given resource (after it has been unbound from the thread).

protected boolean
shouldReleaseBeforeCompletion()

Overrides org.springframework.transaction.support.ResourceHolderSynchronization.shouldReleaseBeforeCompletion.

Return whether this holder's resource should be released before transaction completion (true) or rather after transaction completion (false).

Inherited from org.springframework.transaction.support.ResourceHolderSynchronization:
afterCommitbeforeCommitbeforeCompletioncleanupResourceflushflushResourceresumeshouldReleaseAfterCompletionshouldUnbindAtCompletionsuspend

Field Detail

commitProcessedback to summary
private boolean commitProcessed
transactedback to summary
private final boolean transacted

Constructor Detail

JmsResourceSynchronizationback to summary
public JmsResourceSynchronization(JmsResourceHolder resourceHolder, Object resourceKey, boolean transacted)

Method Detail

afterCompletionback to summary
public void afterCompletion(int status)

Overrides org.springframework.transaction.support.ResourceHolderSynchronization.afterCompletion.

Overrides default org.springframework.transaction.support.TransactionSynchronization.afterCompletion.

Doc from org.springframework.transaction.support.TransactionSynchronization.afterCompletion.

Invoked after transaction commit/rollback. Can perform resource cleanup after transaction completion.

Note

The transaction will have been committed or rolled back already, but the transactional resources might still be active and accessible. As a consequence, any data access code triggered at this point will still "participate" in the original transaction, allowing to perform some cleanup (with no commit following anymore!), unless it explicitly declares that it needs to run in a separate transaction. Hence: Use PROPAGATION_REQUIRES_NEW for any transactional operation that is called from here.

Parameters
status:int

completion status according to the STATUS_* constants

Annotations
@Override
processResourceAfterCommitback to summary
protected void processResourceAfterCommit(JmsResourceHolder resourceHolder)

Overrides org.springframework.transaction.support.ResourceHolderSynchronization.processResourceAfterCommit.

Doc from org.springframework.transaction.support.ResourceHolderSynchronization.processResourceAfterCommit.

After-commit callback for the given resource holder. Only called when the resource hasn't been released yet (shouldReleaseBeforeCompletion()).

Parameters
resourceHolder:JmsResourceHolder

the resource holder to process

Annotations
@Override
releaseResourceback to summary
protected void releaseResource(JmsResourceHolder resourceHolder, Object resourceKey)

Overrides org.springframework.transaction.support.ResourceHolderSynchronization.releaseResource.

Doc from org.springframework.transaction.support.ResourceHolderSynchronization.releaseResource.

Release the given resource (after it has been unbound from the thread).

Parameters
resourceHolder:JmsResourceHolder

the resource holder to process

resourceKey:Object

the key that the ResourceHolder was bound for

Annotations
@Override
shouldReleaseBeforeCompletionback to summary
protected boolean shouldReleaseBeforeCompletion()

Overrides org.springframework.transaction.support.ResourceHolderSynchronization.shouldReleaseBeforeCompletion.

Doc from org.springframework.transaction.support.ResourceHolderSynchronization.shouldReleaseBeforeCompletion.

Return whether this holder's resource should be released before transaction completion (true) or rather after transaction completion (false).

Note that resources will only be released when they are unbound from the thread (shouldUnbindAtCompletion()).

The default implementation returns true.

Annotations
@Override
org.springframework.jms.connection back to summary

public Interface ConnectionFactoryUtils.ResourceFactory

Known Direct Implementers
org.springframework.jms.core.JmsTemplate.JmsTemplateResourceFactory, org.springframework.jms.listener.AbstractPollingMessageListenerContainer.MessageListenerContainerResourceFactory

Callback interface for resource creation. Serving as argument for the doGetTransactionalSession method.

Method Summary

Modifier and TypeMethod and Description
public Connection

Returns:

the new JMS Connection
createConnection
()

Create a new JMS Connection for registration with a JmsResourceHolder.

public Session

Returns:

the new JMS Session
createSession
(Connection
the JMS Connection to create a Session for
con
)

Create a new JMS Session for registration with a JmsResourceHolder.

public Connection

Returns:

an appropriate Connection fetched from the holder, or null if none found
getConnection
(JmsResourceHolder
the JmsResourceHolder
holder
)

Fetch an appropriate Connection from the given JmsResourceHolder.

public Session

Returns:

an appropriate Session fetched from the holder, or null if none found
getSession
(JmsResourceHolder
the JmsResourceHolder
holder
)

Fetch an appropriate Session from the given JmsResourceHolder.

public boolean

Returns:

whether to allow for synchronizing a local JMS transaction
isSynchedLocalTransactionAllowed
()

Return 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.

Method Detail

createConnectionback to summary
public Connection createConnection() throws JMSException

Create a new JMS Connection for registration with a JmsResourceHolder.

Returns:Connection

the new JMS Connection

Exceptions
JMSException:
if thrown by JMS API methods
createSessionback to summary
public Session createSession(Connection con) throws JMSException

Create a new JMS Session for registration with a JmsResourceHolder.

Parameters
con:Connection

the JMS Connection to create a Session for

Returns:Session

the new JMS Session

Exceptions
JMSException:
if thrown by JMS API methods
getConnectionback to summary
public Connection getConnection(JmsResourceHolder holder)

Fetch an appropriate Connection from the given JmsResourceHolder.

Parameters
holder:JmsResourceHolder

the JmsResourceHolder

Returns:Connection

an appropriate Connection fetched from the holder, or null if none found

Annotations
@Nullable
getSessionback to summary
public Session getSession(JmsResourceHolder holder)

Fetch an appropriate Session from the given JmsResourceHolder.

Parameters
holder:JmsResourceHolder

the JmsResourceHolder

Returns:Session

an appropriate Session fetched from the holder, or null if none found

Annotations
@Nullable
isSynchedLocalTransactionAllowedback to summary
public boolean isSynchedLocalTransactionAllowed()

Return 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.

Returns:boolean

whether to allow for synchronizing a local JMS transaction