Exemple #1
0
        public void RollbackAll()
        {
            foreach (var channel in _channels)
            {
                _logger?.LogDebug("Rolling back messages to channel: {channel}", channel);

                RabbitUtils.RollbackIfNecessary(channel);
                if (_deliveryTags.TryGetValue(channel, out var tags))
                {
                    foreach (var deliveryTag in tags)
                    {
                        try
                        {
                            channel.BasicReject(deliveryTag, RequeueOnRollback);
                        }
                        catch (Exception ex)
                        {
                            _logger?.LogError(ex, "Error Rolling back messages to {channel} ", channel);
                            throw RabbitExceptionTranslator.ConvertRabbitAccessException(ex);
                        }
                    }

                    // Need to commit the reject (=nack)
                    RabbitUtils.CommitIfNecessary(channel);
                }
            }
        }
Exemple #2
0
        public void CloseAll()
        {
            foreach (var channel in _channels)
            {
                try
                {
                    if (channel != ConsumerChannelRegistry.GetConsumerChannel())
                    {
                        channel.Close();
                    }
                    else
                    {
                        _logger?.LogDebug("Skipping close of consumer channel: {channel} ", channel);
                    }
                }
                catch (Exception ex)
                {
                    _logger?.LogDebug(ex, "Could not close synchronized Rabbit Channel after transaction");
                }
            }

            foreach (var con in _connections)
            {
                RabbitUtils.CloseConnection(con);
            }

            _connections.Clear();
            _channels.Clear();
            _channelsPerConnection.Clear();
        }
Exemple #3
0
        public static void ReleaseResources(RabbitResourceHolder resourceHolder)
        {
            if (resourceHolder == null || resourceHolder.SynchronizedWithTransaction)
            {
                return;
            }

            RabbitUtils.CloseChannel(resourceHolder.GetChannel());
            RabbitUtils.CloseConnection(resourceHolder.GetConnection());
        }
Exemple #4
0
            public void Destroy()
            {
                if (Target != null)
                {
                    _factory.ConnectionListener.OnClose(Target);
                    RabbitUtils.CloseConnection(Target);
                }

                Target = null;
            }
Exemple #5
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);

            RC.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 (Exception ex)
            {
                RabbitUtils.CloseConnection(connection);
                throw RabbitExceptionTranslator.ConvertRabbitAccessException(ex);
            }
        }