Class ConnectionFactoryUtils
- java.lang.Object
-
- org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils
-
public final class ConnectionFactoryUtils extends Object
Helper class for managing a Spring based RabbitConnectionFactory, in particular for obtaining transactional Rabbit resources for a given ConnectionFactory.Mainly for internal use within the framework. Used by
RabbitTemplateas well asSimpleMessageListenerContainer.- Author:
- Mark Fisher, Dave Syer, Gary Russell, Artem Bilan
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceConnectionFactoryUtils.ResourceFactoryCallback interface for resource creation.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static RabbitResourceHolderbindResourceToTransaction(RabbitResourceHolder resourceHolder, ConnectionFactory connectionFactory, boolean synched)static voidcheckAfterCompletion()When using transaction synchronization, call this method after the transaction commits to verify that the RabbitMQ transaction committed.static ConnectioncreateConnection(ConnectionFactory connectionFactory, boolean publisherConnectionIfPossible)Create a connection with this connection factory and/or its publisher factory.static voidenableAfterCompletionFailureCapture(boolean enable)Call this method to enable capturingAfterCompletionFailedExceptions when using transaction synchronization.static RabbitResourceHoldergetTransactionalResourceHolder(ConnectionFactory connectionFactory, boolean synchedLocalTransactionAllowed)Obtain a RabbitMQ Channel that is synchronized with the current transaction, if any.static RabbitResourceHoldergetTransactionalResourceHolder(ConnectionFactory connectionFactory, boolean synchedLocalTransactionAllowed, boolean publisherConnectionIfPossible)Obtain a RabbitMQ Channel that is synchronized with the current transaction, if any.static booleanisChannelTransactional(com.rabbitmq.client.Channel channel, ConnectionFactory connectionFactory)Determine whether the given RabbitMQ Channel is transactional, that is, bound to the current thread by Spring's transaction facilities.static voidregisterDeliveryTag(ConnectionFactory connectionFactory, com.rabbitmq.client.Channel channel, Long tag)static voidreleaseResources(RabbitResourceHolder resourceHolder)
-
-
-
Method Detail
-
isChannelTransactional
public static boolean isChannelTransactional(com.rabbitmq.client.Channel channel, ConnectionFactory connectionFactory)Determine whether the given RabbitMQ Channel is transactional, that is, bound to the current thread by Spring's transaction facilities.- Parameters:
channel- the RabbitMQ Channel to checkconnectionFactory- the RabbitMQ ConnectionFactory that the Channel originated from- Returns:
- whether the Channel is transactional
-
getTransactionalResourceHolder
public static RabbitResourceHolder getTransactionalResourceHolder(ConnectionFactory connectionFactory, boolean synchedLocalTransactionAllowed)
Obtain a RabbitMQ Channel that is synchronized with the current transaction, if any.- Parameters:
connectionFactory- the ConnectionFactory to obtain a Channel forsynchedLocalTransactionAllowed- whether to allow for a local RabbitMQ 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 RabbitMQ transaction committing right after the main transaction. If not allowed, the given ConnectionFactory needs to handle transaction enlistment underneath the covers.- Returns:
- the transactional Channel, or
nullif none found
-
getTransactionalResourceHolder
public static RabbitResourceHolder getTransactionalResourceHolder(ConnectionFactory connectionFactory, boolean synchedLocalTransactionAllowed, boolean publisherConnectionIfPossible)
Obtain a RabbitMQ Channel that is synchronized with the current transaction, if any.- Parameters:
connectionFactory- the ConnectionFactory to obtain a Channel forsynchedLocalTransactionAllowed- whether to allow for a local RabbitMQ 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 RabbitMQ transaction committing right after the main transaction. If not allowed, the given ConnectionFactory needs to handle transaction enlistment underneath the covers.publisherConnectionIfPossible- obtain a connection from a separate publisher connection if possible.- Returns:
- the transactional Channel, or
nullif none found
-
releaseResources
public static void releaseResources(@Nullable RabbitResourceHolder resourceHolder)
-
bindResourceToTransaction
public static RabbitResourceHolder bindResourceToTransaction(RabbitResourceHolder resourceHolder, ConnectionFactory connectionFactory, boolean synched)
-
enableAfterCompletionFailureCapture
public static void enableAfterCompletionFailureCapture(boolean enable)
Call this method to enable capturingAfterCompletionFailedExceptions when using transaction synchronization. Exceptions are stored in aThreadLocalwhich must be cleared by callingcheckAfterCompletion()after the transaction has completed.- Parameters:
enable- true to enable capture.
-
checkAfterCompletion
public static void checkAfterCompletion()
When using transaction synchronization, call this method after the transaction commits to verify that the RabbitMQ transaction committed.- Throws:
AfterCompletionFailedException- if synchronization failed.- Since:
- 2.3.10
-
registerDeliveryTag
public static void registerDeliveryTag(ConnectionFactory connectionFactory, com.rabbitmq.client.Channel channel, Long tag)
-
createConnection
public static Connection createConnection(ConnectionFactory connectionFactory, boolean publisherConnectionIfPossible)
Create a connection with this connection factory and/or its publisher factory.- Parameters:
connectionFactory- the connection factory.publisherConnectionIfPossible- true to use the publisher factory, if present.- Returns:
- the connection.
- Since:
- 2.0.2
-
-