Example usage for org.springframework.amqp.rabbit.connection RabbitResourceHolder RabbitResourceHolder

List of usage examples for org.springframework.amqp.rabbit.connection RabbitResourceHolder RabbitResourceHolder

Introduction

In this page you can find the example usage for org.springframework.amqp.rabbit.connection RabbitResourceHolder RabbitResourceHolder.

Prototype

public RabbitResourceHolder() 

Source Link

Document

Create a new RabbitResourceHolder that is open for resources to be added.

Usage

From source file:org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils.java

/**
 * Obtain a RabbitMQ Channel that is synchronized with the current
 * transaction, if any./*  w  ww  .j  av  a2  s .co m*/
 * 
 * @param connectionFactory
 *            the RabbitMQ ConnectionFactory to bind for (used as
 *            TransactionSynchronizationManager key)
 * @param resourceFactory
 *            the ResourceFactory to use for extracting or creating RabbitMQ
 *            resources
 * @return the transactional Channel, or <code>null</code> if none found
 */
private static RabbitResourceHolder doGetTransactionalResourceHolder(ConnectionFactory connectionFactory,
        ResourceFactory resourceFactory) {

    Assert.notNull(connectionFactory, "ConnectionFactory must not be null");
    Assert.notNull(resourceFactory, "ResourceFactory must not be null");

    RabbitResourceHolder resourceHolder = (RabbitResourceHolder) TransactionSynchronizationManager
            .getResource(connectionFactory);
    if (resourceHolder != null) {
        Channel channel = resourceFactory.getChannel(resourceHolder);
        if (channel != null) {
            return resourceHolder;
        }
    }
    RabbitResourceHolder resourceHolderToUse = resourceHolder;
    if (resourceHolderToUse == null) {
        resourceHolderToUse = new RabbitResourceHolder();
    }
    Connection connection = resourceFactory.getConnection(resourceHolderToUse);
    Channel channel = null;
    try {
        boolean isExistingCon = (connection != null);
        if (!isExistingCon) {
            connection = resourceFactory.createConnection();
            resourceHolderToUse.addConnection(connection);
        }
        channel = consumerChannel.get();
        if (channel == null) {
            channel = resourceFactory.createChannel(connection);
        }
        if (null != channel && channel.isOpen()) {
            resourceHolderToUse.addChannel(channel, connection);
        }

        if (resourceHolderToUse != resourceHolder) {
            bindResourceToTransaction(resourceHolderToUse, connectionFactory,
                    resourceFactory.isSynchedLocalTransactionAllowed());
        }

        return resourceHolderToUse;

    } catch (IOException ex) {
        RabbitUtils.closeChannel(channel);
        RabbitUtils.closeConnection(connection);
        throw new AmqpIOException(ex);
    }
}