- 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.
- TypeScript入門與實戰(zhàn)
- 從零開始:數(shù)字圖像處理的編程基礎(chǔ)與應(yīng)用
- jQuery EasyUI網(wǎng)站開發(fā)實戰(zhàn)
- 單片機應(yīng)用技術(shù)
- Python程序設(shè)計
- 深度學(xué)習(xí):算法入門與Keras編程實踐
- Building Minecraft Server Modifications
- C程序設(shè)計實踐教程
- 深入剖析Java虛擬機:源碼剖析與實例詳解(基礎(chǔ)卷)
- 區(qū)塊鏈架構(gòu)之美:從比特幣、以太坊、超級賬本看區(qū)塊鏈架構(gòu)設(shè)計
- 玩轉(zhuǎn).NET Micro Framework移植:基于STM32F10x處理器
- Shopify Application Development
- 川哥教你Spring Boot 2實戰(zhàn)
- 現(xiàn)代JavaScript編程:經(jīng)典范例與實踐技巧
- VBA Automation for Excel 2019 Cookbook