Kartik
Kartik

Reputation: 11

How to delete all messages from a rabbit MQ in java

We have a job which will run at only End of day. During EOD it will read all the messages from RabbitMQ and generate a file. After a successful file generation we need to delete all the messages in RabbitMQ. Is there a way to do it from Java code?

We are using channel.basicGet(QueueName,false);

If I pass autoAck as true instead of false then it will delete the messages but if there is any exception while processing then message will be lost.

To overcome this I used channel.queuepurge(QueueName); but it is not working.

So our requirement is we need to read the messages from queue generate a file after successful file generation then we need to delete all messages else If any failures during processing then we should not delete the messages from queue

Upvotes: 0

Views: 1841

Answers (1)

Umeshwaran
Umeshwaran

Reputation: 647

Use RabbitAdmin to purge the queue . Note the difference between purging and deleting .

Delete - Delete the queue
Purge - Empty the message of the queue.

http://docs.spring.io/autorepo/docs/spring-amqp-dist/1.3.4.RELEASE/api/org/springframework/amqp/rabbit/core/RabbitAdmin.html#purgeQueue%28java.lang.String,%20boolean%29

@Autowired private RabbitAdmin admin;

...

admin.purgeQueue("queueName", false);

Upvotes: 1

Related Questions