- RabbitMQ Cookbook
- Sigismondo Boschi Gabriele Santomaggio
- 334字
- 2021-07-19 18:52:50
How to let queues expire
In this third case, the TTL is not related to messages anymore, but to queues. This case is a perfect fit to manage server restarts and updates. The RabbitMQ drops the queue as soon as the TTL has elapsed, that is, after the last consumer has stopped consuming messages.
As in the previous TTL-related recipes, you can find the Producer.java
, Consumer.java
, and GetOne.java
files in Chapter02/Recipe03/Java/src/rmqexample
.
Getting ready
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...
As in the previous example, the extension regards only Consumer.java
:
- Create or declare the exchange using the following code:
channel.exchangeDeclare(exchange, "direct", false);
- Create or declare the queue, specifying a 30,000 milliseconds timeout to the
x-expires
optional argument as follows:Map<String, Object> arguments = new HashMap<String, Object>(); arguments.put("x-expires", 30000); channel.queueDeclare(queue, false, false, false, arguments);
- Bind the queue to the exchange where messages are going to come from as follows:
channel.queueBind(queue, exchange, routingKey);
How it works...
When we run either the Consumer.java
or GetOne.java
file, the timed queue is created and until there is a consumer attached to the queue or we call channel.basicGet()
, it will continue to exist.
Only when we stop both the operations for at least 30 seconds, the queue is dropped and all the messages that it contains are lost.
In the course of the experiments, we can monitor the RabbitMQ queue status issuing rabbitmqctl list_queues
and see this happening.
So, we can imagine a scenario where we have a statistics analysis program that needs to be restarted for an update of the code itself. It will restart without losing any messages, since its named queues will have a longer timeout. Alternatively, if we stop it, its queues will be deleted after the given TTL and worthless messages won't be stored at all.
- DBA攻堅指南:左手Oracle,右手MySQL
- iOS面試一戰(zhàn)到底
- Building Modern Web Applications Using Angular
- 從學(xué)徒到高手:汽車電路識圖、故障檢測與維修技能全圖解
- 深入淺出PostgreSQL
- HTML5與CSS3基礎(chǔ)教程(第8版)
- Arduino可穿戴設(shè)備開發(fā)
- Web Developer's Reference Guide
- Java Hibernate Cookbook
- Applied Deep Learning with Python
- Android應(yīng)用開發(fā)攻略
- H5頁面設(shè)計與制作(全彩慕課版·第2版)
- Eclipse開發(fā)(學(xué)習(xí)筆記)
- Learning PrimeFaces Extensions Development
- 快速搞定Spring Boot+Vue全棧開發(fā)