/// <summary>Obtain a RabbitMQ Channel that is synchronized with the current transaction, if any.</summary>
        /// <param name="connectionFactory">The connection factory.</param>
        /// <param name="resourceFactory">The resource factory.</param>
        /// <returns>The transactional Channel, or null if none found.</returns>
        private static RabbitResourceHolder DoGetTransactionalResourceHolder(IConnectionFactory connectionFactory, IResourceFactory resourceFactory)
        {
            AssertUtils.ArgumentNotNull(connectionFactory, "ConnectionFactory must not be null");
            AssertUtils.ArgumentNotNull(resourceFactory, "ResourceFactory must not be null");

            var resourceHolder = (RabbitResourceHolder)TransactionSynchronizationManager.GetResource(connectionFactory);

            if (resourceHolder != null)
            {
                var tempchannel = resourceFactory.GetChannel(resourceHolder);
                if (tempchannel != null)
                {
                    return(resourceHolder);
                }
            }

            var resourceHolderToUse = resourceHolder;

            if (resourceHolderToUse == null)
            {
                resourceHolderToUse = new RabbitResourceHolder();
            }

            var    connection = resourceFactory.GetConnection(resourceHolderToUse);
            IModel channel    = null;

            try
            {
                var isExistingCon = connection != null;
                if (!isExistingCon)
                {
                    connection = resourceFactory.CreateConnection();
                    resourceHolderToUse.AddConnection(connection);
                }

                channel = consumerChannel.Value;

                if (channel == null)
                {
                    channel = resourceFactory.CreateChannel(connection);
                }

                resourceHolderToUse.AddChannel(channel, connection);

                if (resourceHolderToUse != resourceHolder)
                {
                    BindResourceToTransaction(resourceHolderToUse, connectionFactory, resourceFactory.IsSynchedLocalTransactionAllowed);
                }

                return(resourceHolderToUse);
            }
            catch (Exception ex)
            {
                RabbitUtils.CloseChannel(channel);
                RabbitUtils.CloseConnection(connection);
                throw new AmqpIOException(ex);
            }
        }
        /// <summary>
        /// Obtain a RabbitMQ Channel that is synchronized with the current transaction, if any.
        /// </summary>
        /// <param name="connectionFactory">
        /// The connection factory.
        /// </param>
        /// <param name="resourceFactory">
        /// The resource factory.
        /// </param>
        /// <returns>
        /// The transactional Channel, or null if none found.
        /// </returns>
        /// <exception cref="AmqpException">
        /// </exception>
        private static RabbitResourceHolder DoGetTransactionalResourceHolder(IConnectionFactory connectionFactory, IResourceFactory resourceFactory)
        {
            AssertUtils.ArgumentNotNull(connectionFactory, "ConnectionFactory must not be null");
            AssertUtils.ArgumentNotNull(resourceFactory, "ResourceFactory must not be null");

            var resourceHolder = (RabbitResourceHolder)TransactionSynchronizationManager.GetResource(connectionFactory);
            if (resourceHolder != null)
            {
                var tempchannel = resourceFactory.GetChannel(resourceHolder);
                if (tempchannel != null)
                {
                    return resourceHolder;
                }
            }

            var resourceHolderToUse = resourceHolder;
            if (resourceHolderToUse == null)
            {
                resourceHolderToUse = new RabbitResourceHolder();
            }

            var connection = resourceFactory.GetConnection(resourceHolderToUse);
            IModel channel = null;
            try
            {
                bool isExistingCon = connection != null;
                if (!isExistingCon)
                {
                    connection = resourceFactory.CreateConnection();
                    resourceHolderToUse.AddConnection(connection);
                }
                channel = resourceFactory.CreateChannel(connection);
                resourceHolderToUse.AddChannel(channel, connection);

                if (resourceHolderToUse != resourceHolder)
                {
                    BindResourceToTransaction(resourceHolderToUse, connectionFactory, resourceFactory.IsSynchedLocalTransactionAllowed);
                }

                return resourceHolderToUse;

            }
            catch (Exception ex)
            {
                RabbitUtils.CloseChannel(channel);
                RabbitUtils.CloseConnection(connection);
                throw;
            }
        }
Пример #3
0
        private static RabbitResourceHolder DoGetTransactionalResourceHolder(IConnectionFactory connectionFactory, IResourceFactory resourceFactory)
        {
            if (connectionFactory == null)
            {
                throw new ArgumentNullException(nameof(connectionFactory));
            }

            if (resourceFactory == null)
            {
                throw new ArgumentNullException(nameof(resourceFactory));
            }

            var resourceHolder = (RabbitResourceHolder)TransactionSynchronizationManager.GetResource(connectionFactory);

            if (resourceHolder != null)
            {
                var model = resourceFactory.GetChannel(resourceHolder);
                if (model != null)
                {
                    return(resourceHolder);
                }
            }

            var resourceHolderToUse = resourceHolder;

            if (resourceHolderToUse == null)
            {
                resourceHolderToUse = new RabbitResourceHolder();
            }

            var    connection = resourceFactory.GetConnection(resourceHolderToUse);
            IModel channel;

            try
            {
                /*
                 * If we are in a listener container, first see if there's a channel registered
                 * for this consumer and the consumer is using the same connection factory.
                 */
                channel = ConsumerChannelRegistry.GetConsumerChannel(connectionFactory);
                if (channel == null && connection == null)
                {
                    connection = resourceFactory.CreateConnection2();
                    if (resourceHolder == null)
                    {
                        /*
                         * While creating a connection, a connection listener might have created a
                         * transactional channel and bound it to the transaction.
                         */
                        resourceHolder = (RabbitResourceHolder)TransactionSynchronizationManager.GetResource(connectionFactory);
                        if (resourceHolder != null)
                        {
                            channel             = resourceHolder.GetChannel();
                            resourceHolderToUse = resourceHolder;
                        }
                    }

                    resourceHolderToUse.AddConnection(connection);
                }

                if (channel == null)
                {
                    channel = resourceFactory.CreateChannel(connection);
                }

                resourceHolderToUse.AddChannel(channel, connection);

                if (!resourceHolderToUse.Equals(resourceHolder))
                {
                    BindResourceToTransaction(resourceHolderToUse, connectionFactory, resourceFactory.IsSynchedLocalTransactionAllowed);
                }

                return(resourceHolderToUse);
            }
            catch (IOException ex)
            {
                RabbitUtils.CloseConnection(connection);
                throw new AmqpIOException(ex);
            }
        }