O'Reilly logo

RabbitMQ Cookbook by Sigismondo Boschi, Gabriele Santomaggio

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required