混合发布者通过交易确认和不确认

时间:2018-04-29 21:59:32

标签: c# rabbitmq

我尝试使用覆盖发布商确认的交易和生产商的非确认,如下面的代码,但我收到以下错误:

 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);            
        }
}

0 个答案:

没有答案