How to read RabbitMQ unacknowledged messages / RabbitMQ loop

I want to read the payload or messageId of unacknowledged messages on a RabbitMQ queue. Is it possible?

The reason I want to do this is I am trying to use RabbitMQ's dead letter function to create a loop to automatically generate a message periodically. In short, create two queues - a work queue and a waiting queue.

  • Set the TTL of the message in the waiting queue as the periodic rate required. Can have different messages with different TTLs for different work purposes;
  • put the message on the waiting queue. When a message expires, it is reissued to the work queue. The message can remain in the work queue for as long as necessary until the consumer consumes it.
  • One consumer takes a message and processes it. If processing succeeds, the consumer must acknowledge the work queue and then write the message back to the hold queue; If processing fails (for example, a stream failed), no acknowledgment is made. Then the message will automatically appear in the work queue automatically. Then another consumer can do the job. When the message sent back to the waiting queue expires again, it is reissued, then re-consumed by the consumer ...... Loop created, workload distributed.

I want there to be no missing or duplicate messages in the loop as I don't want a missing job or a duplicate job running at the same time. However, there are tiny tiny random repeating messages. The following shows that the consumer first writes a message to delay the queue and confirms the work queue. If the thread fires right between the two lines, the message will be in the waiting queue and Rabbit will publish the message to the work queue again. Ultimately, messages are duplicated in a loop.

  channel.basicPublish(DELAY_EXCHANGE, "", null, message.getBytes());
  channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);

      

To prevent the above, I want to add the dog view logic after two lines:

  • Check the total number of messages in the loop (total number of messages in both queues) to see if it is equal to my expected number (I expected it to be less than 10);

  • If the number does not match, I want to figure out which one is missing or some kind of duplicate and then deal with it. I don’t care about the sequence of these messages, or the frequency was violated, as this is really a very important question. I can easily get those messages that are ready and require them. But the problem is how to deal with these unrecognized messages?

Thank you in advance!

Roy

+3


source to share


1 answer


Unable to read unacknowledged messages from another context from which the original messages were consumed and saved as un-aked.



+2


source







All Articles