How to let messages expire

In this recipe, we are going to see how to let messages expire. The recipe sources can be found at Chapter02/Recipe01/Java/src/rmqexample in the code bundle that contains the following three executable classes:

  • Producer.java
  • Consumer.java
  • GetOne.java

Getting ready

In order to use this recipe, we need to set up the Java development environment, as indicated in the Introduction section of Chapter 1, Working with AMQP.

How to do it...

The core of this example is in the Producer.java file. To produce messages that expire with a given Time-To-Live (TTL), we have to perform the following steps:

  1. Create or declare an exchange to send messages and bind it to a queue, as seen in Chapter 1, Working with AMQP:
    channel.exchangeDeclare(exchange, ...

Get RabbitMQ Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.