Mix Publisher Confirms and non confirms via Transaction

1

I tried to use transaction covering both Publisher Confirms and non confirms for producers, like the code below, but I got the error below:

 RabbitMQ.Client.Exceptions.OperationInterruptedException
      HResult=0x80131500
      Message=The AMQP operation was interrupted: AMQP close-reason, initiated by Peer, code=406, text="PRECONDITION_FAILED - cannot switch from confirm to tx mode", classId=90, methodId=10, cause=
      Source=RabbitMQ.Client
      StackTrace:
       at RabbitMQ.Client.Impl.SimpleBlockingRpcContinuation.GetReply(TimeSpan timeout)
       at RabbitMQ.Client.Impl.ModelBase.ModelRpc(MethodBase method, ContentHeaderBase header, Byte[] body)
       at RabbitMQ.Client.Framing.Impl.Model.TxSelect()



 public class ProducerConfirmsTx
    {
        public void Run()
        {
            Produce();
        }
        public void Produce()
        {
            var factory = new ConnectionFactory() {
                HostName = _appSetting.MessagingServerSetting.ServerName,
                UserName = _appSetting.MessagingServerSetting.UserName,
                Password = _appSetting.MessagingServerSetting.Password,               
            };
            using (var connection = factory.CreateConnection())
            using (var channel = connection.CreateModel())
            {
                channel.QueueDeclare(
                    queue: QUEUE_NAME,
                    durable: true,
                    exclusive: false,
                    autoDelete: false,
                    arguments: null);         

                //3
                channel.BasicAcks += Channel_BasicAcks; 

                //1
                channel.ConfirmSelect(); 

                var properties = channel.CreateBasicProperties();
                properties.Persistent = true;

                    channel.TxSelect();  //tx starts

                    channel.BasicPublish(
                    exchange: "",
                    routingKey: QUEUE_NAME,
                    basicProperties: properties,
                    body: ConvertMessageToByte(message));

                    //2
                    channel.WaitForConfirmsOrDie();  //blocked and wait


                    //other works that need tx here

                    channel.TxCommit();   //tx ends
                    //channel.TxRollback();            
            }

        }

        private void Channel_BasicAcks(object sender, RabbitMQ.Client.Events.BasicAckEventArgs e)
        {
            DisplayResponseInfo(e.DeliveryTag);            
        }
}
c#
rabbitmq
asked on Stack Overflow Apr 29, 2018 by Pingpong

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0