Rabbitmq retrieve multiple messages using single synchronous call

后端 未结 4 1274
灰色年华
灰色年华 2021-02-13 19:04

Is there a way to receive multiple message using a single synchronous call ?

When I know that there are N messages( N could be a small value less than 10) in the queue,

4条回答
  •  滥情空心
    2021-02-13 19:35

    First declare instance of QueueingBasicConsumer() wich wraps the model.
    From the model execute model.BasicConsume(QueueName, false, consumer)
    Then implement a loop that will loop around messages from the queue which will then processing
    Next line - consumer.Queue.Dequeue() method - waiting for the message to be received from the queue.
    Then convert byte array to a string and display it.
    Model.BasicAck() - release message from the queue to receive next message
    And then on the server side can start waiting for the next message to come through:

      public string GetMessagesByQueue(string QueueName)
        {
            var consumer = new QueueingBasicConsumer(_model);
            _model.BasicConsume(QueueName, false, consumer);
    
            string message = string.Empty;
    
            while (Enabled)
            {
                //Get next message
                var deliveryArgs = (BasicDeliverEventArgs)consumer.Queue.Dequeue();
    
                //Serialize message
                 message = Encoding.Default.GetString(deliveryArgs.Body);
                    _model.BasicAck(deliveryArgs.DeliveryTag, false);
            }
            return message;
        }
    

提交回复
热议问题