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

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

Introduction

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

Prototype

public final void addChannel(Channel channel, @Nullable Connection connection) 

Source Link

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 w w .j a  v  a 2 s  .  c o 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);
    }
}