RabbitMQ消息处于未确认状态

时间:2019-03-18 10:12:19

标签: c# rabbitmq

  

尝试监听从RMQ接收到的消息,虽然已接收到消息,但队列中的消息仍处于未确认状态。

           ConnectionFactory factory = new ConnectionFactory();// assign factory details

           IConnection _connection = factory.CreateConnection();
           IModel  _model = _connection.CreateModel();
           _model.ExchangeDeclare("RmqExchangeName", "topic", true);
           var consumer = new EventingBasicConsumer(_model);

            consumer.Received += (object ch, BasicDeliverEventArgs ea) =>
            {
                   var message = Encoding.Default.GetString(ea.Body);

                    MemoryStream payloadstream = new MemoryStream(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new UriDto { Url = message })));
                    Helper.Log("Start  Message");
                    // Do something with payloadstream
                    Helper.Log("Sent  Message");
                    _model.BasicAck(ea.DeliveryTag, false);
             };
           _model.BasicConsume(RmqQueueName, false, consumer);

1 个答案:

答案 0 :(得分:0)

您需要将交换绑定到队列以使用消息。

       ConnectionFactory factory = new ConnectionFactory();// assign factory details
       IConnection _connection = factory.CreateConnection();
       IModel  _model = _connection.CreateModel();
       _model.ExchangeDeclare("RmqExchangeName", "topic", true);
       _model.QueueBind(queue: queueName,exchange: "RmqExchangeName",routingKey: "");
       var consumer = new EventingBasicConsumer(_model);

        consumer.Received += (object ch, BasicDeliverEventArgs ea) =>
        {
               var message = Encoding.Default.GetString(ea.Body);

                MemoryStream payloadstream = new MemoryStream(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new UriDto { Url = message })));
                Helper.Log("Start  Message");
                // Do something with payloadstream
                Helper.Log("Sent  Message");
                _model.BasicAck(ea.DeliveryTag, false);
         };
       _model.BasicConsume(RmqQueueName, true, consumer);