コード例 #1
0
        /// <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);
            }
        }
コード例 #2
0
        /// <summary>Release the resources.</summary>
        /// <param name="resourceHolder">The resource holder.</param>
        public static void ReleaseResources(RabbitResourceHolder resourceHolder)
        {
            if (resourceHolder == null || resourceHolder.SynchronizedWithTransaction)
            {
                return;
            }

            RabbitUtils.CloseChannel(resourceHolder.Channel);
            RabbitUtils.CloseConnection(resourceHolder.Connection);
        }