In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains the "RabbitMQ message validity and dead letter processing process is what", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "RabbitMQ message validity and dead letter processing process is what" it!
one。 Preface
The full name of RabbitMQ's TTL is Time-To-Live, which indicates the validity period of the message. If a message is not consumed in the queue and lasts longer than TTL, the message becomes "Dead Message" and can no longer be consumed later. If TTL is not set, the message is permanently valid (the default message is not invalidated). If TTL is set to 0, it means that if the message cannot be consumed immediately, it will be discarded immediately. This feature can partially replace the immediate parameter previously supported by RabbitMQ3.0. The reason for this is that when the immediate parameter fails to deliver, the basic.return method will return the message body (this function can be implemented using dead letter queues).
There are two ways to set up TTL:
Queue validity: when you declare a queue, set it in the properties of the queue so that all messages in the queue have the same validity period
Message validity period: set properties for messages when sending messages. You can set different TTL for each message.
If both methods are set, the smaller one shall prevail.
Difference: if the validity period is set when the queue is declared, the message will be deleted if it expires; if it is the validity period set when the message is sent, the message will not be deleted immediately if it expires, because whether the message expires or not is judged when it is delivered to the consumer.
two。 Set the message validity period 1. Set the validity period of the queue TTL
Queues are defined as follows:
Queue.DeclareOk queueDeclare (String queue, boolean durable, boolean exclusive, boolean autoDelete, Map arguments) throws IOException
The arguments parameter of this method sets the property of the queue, which is named x-message-ttl, in milliseconds. The words added at the backend are as follows:
The settings in the code are as follows:
Map arguments= new HashMap (); arguments.put ("x-message-ttl", 10000); / / 10 seconds in millisecond channel.queueDeclare (queueName, durable, exclusive, autoDelete, arguments)
Command line mode to set:
Rabbitmqctl set_policy TTL ". *" {"message-ttl": 100000}'--apply-to queues
Call through the API HTTP:
$curl-I-u guest:guest-H "content-type:application/json"-XPUT-d' {"auto_delete": false, "durable": true, "arguments": {"x-message-ttl": 100000}} 'http://ip:15672/api/queues/{vhost}/{queuename}2. Set the validity period of the queue Expire
The validity period Expire allows the queue to expire and delete automatically if it is "unused" within a specified period of time, which means that no consumer,queue on the queue has not been redeclared and the basic.get command has not been called within the expiration period. This method can be used, for example, for RPC-style 's reply queue, in which many queue are created but never used.
The server ensures that the queue is deleted after the expiration time, but does not guarantee how timely the deletion will be. After the server restarts, the timeout of the persisted queue will be recalculated. The x-expires parameter value is in milliseconds, obeys the same constraints as x-message-ttl, and cannot be set to 0. Therefore, if this parameter is set to 10000, it means that the queue will be deleted if it is not used within 10 seconds.
The code is as follows:
Map args = new HashMap (); args.put ("x-expires", 10000); channel.queueDeclare ("queue", false, args); 3. Set the validity period by sending a message
The method of sending messages is as follows:
Void basicPublish (String exchange, String routingKey, BasicProperties props, byte [] body) throws IOException
The validity period can be set in the props parameter of this method:
Map headers = new HashMap () AMQP.BasicProperties properties = new AMQP.BasicProperties () .builder () .deliveryMode (2) / message persistence. ContentEncoding ("UTF-8") / / Encoding .contentType ("text/plain") .origination ("100000") .headers (headers) .build () Channel.basicPublish ("", queueName, properties, message.getBytes ())
Set through the HTTPAPI API:
$curl-I-u guest:guest-H "content-type:application/json"-XPOST-d' {"properties": {"expiration": "100000"}, "routing_key": "routingkey", "payload": "bodys", "payload_encoding": "string"} 'http://localhost:15672/api/exchanges/{vhost}/{exchangename}/publish III. Dead letter switch DLX
Introduction
Dead letter queue: DLX,dead-letter-exchange
With DLX, when a message becomes a dead message in one queue, it can be re-publish to another Exchange, which is DLX.
There are several situations in which the news becomes a dead letter.
Message is rejected (Basic.Reject/Basic.Nack), and the requeue parameter is set to false
Message expires
The queue reaches the maximum length
Dead letter processing process
DLX is also a normal Exchange, no different from a normal Exchange, it can be specified on any queue, which is actually setting the properties of a queue.
When there is a dead letter in this queue, RabbitMQ will automatically republish the message to the set Exchange and be routed to another queue.
You can monitor the messages in this queue for processing accordingly.
Use
Observe and analyze the data by monitoring the messages in the consumer dead letter queue.
Implement delay queue with TTL (for example, automatically shut down for how long it takes to place an order)
Use
The code is as follows:
Channel.exchangeDeclare ("dlx_exchange", "direct"); / / create DLX: dlx_exchangeMap args = new HashMap (); args.put ("x-dead-letter-exchange", "dlx_exchange"); / / set the dead-letter switch args.put ("x-dead-letter-routing-key", "dlx-routing-key"); / / set the routing key of DLX (may not be set) channel.queueDeclare ("myqueue", false, false, false, args)
Example
Public static void main (String [] args) throws Exception {Connection connection = ConnectionUtil.getConnection (); Channel channel = connection.createChannel (); / / declare a switch and use channel.exchangeDeclare ("dlx_exchange", "topic", true, false, null) as a dead-letter switch. / / declare a queue and use channel.queueDeclare ("dlx_queue", true, false, false, null) as a dead-letter queue; / / bind the queue to the switch channel.queueBind ("dlx_queue", "dlx_exchange", "dlx.*") Channel.exchangeDeclare ("normal_exchange", "fanout", true, false, null); Map arguments=new HashMap (); arguments.put ("x-message-ttl", 1000) / / set the validity period of the message for 1 second. After it expires, it becomes a private message, and then enters DLX arguments.put ("x-dead-letter-exchange", "dlx_exchange"); / / sets DLX arguments.put ("x-dead-letter-routing-key", "dlx.test") / / set the routing key of DLX / / add DLX channel.queueDeclare ("normal_queue", true, false, false, arguments) to queue normal_queue; channel.queueBind ("normal_queue", "normal_exchange", ""); channel.basicPublish ("normal_exchange", ", MessageProperties.PERSISTENT_TEXT_PLAIN, (" Test Dead message ") .getBytes ()) System.out.println ("send message time:" + ConnectionUtil.formatDate (new Date (); channel.close (); connection.close ();}
Description:
Declare that the dead-letter queue dlx_queue is bound as follows, which is bound to the dead-letter switch dlx_exchange (topic type). The routing key is "dlx.*".
Declare queue normal_queue and bind to switch normal_exchange (fanout type)
Perform the process:
The message is sent to switch normal_exchange and then routed to queue normal_queue
Because the queue normal_queue has no consumer, the message becomes a dead message when it expires.
The dead letter message carries the set x-dead-letter-routing-key=dlx.test into the dead letter switch dlx_exechage.
The routing key bound by dlx_exechage and dlx_queue is "dlx.*", and the routing key dlx.test of the dead message is routed to the dlx.queue in accordance with this rule.
Thank you for your reading, the above is the content of "what is the validity period of RabbitMQ message and the processing process of dead letter". After the study of this article, I believe you have a deeper understanding of the validity period of RabbitMQ message and the processing process of dead letter, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 225
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.