Пример #1
0
            private void Rollback(ulong deliveryTag, Exception e)
            {
                if (_container.IsChannelTransacted)
                {
                    RabbitUtils.RollbackIfNecessary(Model);
                }

                if (AckRequired || ContainerUtils.IsRejectManual(e))
                {
                    try
                    {
                        if (MessagesPerAck > 1)
                        {
                            lock (_lock)
                            {
                                if (PendingAcks > 0)
                                {
                                    SendAck(DateTimeOffset.Now.ToUnixTimeMilliseconds());
                                }
                            }
                        }

                        Model.BasicNack(deliveryTag, true, ContainerUtils.ShouldRequeue(_container.DefaultRequeueRejected, e, _logger));
                    }
                    catch (IOException e1)
                    {
                        _logger?.LogError("Failed to nack message", e1);
                    }
                }

                if (_container.IsChannelTransacted)
                {
                    RabbitUtils.CommitIfNecessary(Model);
                }
            }
Пример #2
0
        public void RollbackOnExceptionIfNecessary(Exception ex)
        {
            bool ackRequired = !AcknowledgeMode.IsAutoAck() && (!AcknowledgeMode.IsManual() || ContainerUtils.IsRejectManual(ex));

            try
            {
                if (Transactional)
                {
                    Logger?.LogDebug(ex, "Initiating transaction rollback on application exception");
                    RabbitUtils.RollbackIfNecessary(Channel);
                }

                if (ackRequired)
                {
                    if (DeliveryTags.Count > 0)
                    {
                        ulong deliveryTag = DeliveryTags.Max();
                        Channel.BasicNack(deliveryTag, true, ContainerUtils.ShouldRequeue(DefaultRequeueRejected, ex, Logger));
                    }

                    if (Transactional)
                    {
                        // Need to commit the reject (=nack)
                        RabbitUtils.CommitIfNecessary(Channel);
                    }
                }
            }
            catch (Exception e)
            {
                Logger?.LogError(ex, "Application exception overridden by rollback exception");
                throw RabbitExceptionTranslator.ConvertRabbitAccessException(e); // NOSONAR stack trace loss
            }
            finally
            {
                DeliveryTags.Clear();
            }
        }