[RabbitMQ] 8. mandatory and immediate of RabbitMQ, and rabbitmqmandatory
1. Overview
Mandatory and immediate are two identification bits in the basic. publish method in the AMQP Protocol. They all have the function of returning a message to the producer when the message is delivered to a destination. Friends who are new to RabbitMQ are very likely to be confused by these two parameters. Here, the blogger has compiled the materials and briefly explained the two identifiers.
Mandatory
When the mandatory flag is set to true, if exchange cannot find a qualified queue based on its own type and message routeKey, basic is called. return method returns the message to the producer (Basic. return + Content-Header + Content-Body); when mandatory is set to false, the broker will directly throw the message.
The role of the mandatory flag: when a message is not routed to an appropriate queue, the message will be returned to the Message publisher. We also tested under which conditions the message will not reach the appropriate queue, test 1 demonstrates that messages that have been created but not bound to an exchange queue have not reached the appropriate queue. Test 3 demonstrates that a queue is created when exchange is created, however, when binding the two, the bindingKey used is inconsistent with the rountingKey used by the message publisher, causing the message to not reach the appropriate queue;
Immediate
When the immediate flag is set to true, if exchange routes messages to the queue (s) and finds that there are consumers on the queue, the message will not be placed in the queue. When all the queue (one or more) associated with the message routeKey have no consumers, the message will be returned to the producer through the basic. return method.
In summary, the mandatory mark tells the server to route the message to at least one queue; otherwise, the message is returned to the producer; the immediate mark tells the server that if there is a consumer on the queue associated with the message, the message will be delivered to it immediately. If no queue has a consumer, the message will be directly returned to the producer without waiting for the consumer to wait in the queue.
2. mandatory
When a producer publishes a message through the basicPublish method of channle, several parameters need to be set. Therefore, we need to understand the meanings and functions of these parameters and view the channel interface, three overloaded basicPublish methods are found:
void basicPublish(String exchange, String routingKey, BasicProperties props, byte[] body) throws IOException;void basicPublish(String exchange, String routingKey, boolean mandatory, BasicProperties props, byte[] body) throws IOException;void basicPublish(String exchange, String routingKey, boolean mandatory, boolean immediate, BasicProperties props, byte[] body) throws IOException;
Mandatory and immediate have been explained above, and the remaining parameters are:
Exchange: Switch name
Routingkey: Route key
Props: Message attribute fields, such as message header information
Body: Message body
This section describes mandatory. Below we will write a demo, which is available in RabbitMQ broker:
Exchange: exchange. mandatory. test
Queue: queue. mandatory. test
The routingkey that exchange routes to queue is mandatory.
Here, we will not talk about binding the current exchange to the queue, that is:
channel.basicPublish(exchangeName, "", mandatory, immediate, MessageProperties.PERSISTENT_TEXT_PLAIN, "===mandatory===".getBytes());
The Code is as follows:
package com.vms.test.zzh.rabbitmq.self;import com.rabbitmq.client.*;import java.io.IOException;import java.util.concurrent.TimeUnit;import java.util.concurrent.TimeoutException;/** * Created by hidden on 2017/2/7. */public class RBmandatoryTest { public static final String ip = "10.198.197.73"; public static final int port = 5672; public static final String username = "root"; public static final String password = "root"; public static final String queueName = "queue.mandatory.test"; public static final String exchangeName = "exchange.mandatory.test"; public static final String routingKey = "mandatory"; public static final Boolean mandatory = true; public static final Boolean immediate = false; public static void main(String[] args) { try { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(ip); factory.setPort(port); factory.setUsername(username); factory.setPassword(password); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.basicQos(1); channel.basicPublish(exchangeName, "", mandatory, immediate, MessageProperties.PERSISTENT_TEXT_PLAIN, "===mandatory===".getBytes());// channel.close();// connection.close(); } catch (IOException e) { e.printStackTrace(); } catch (TimeoutException e) { e.printStackTrace(); } }}
Run and then use wireshark to capture packets, as shown in:
Here we can see that the last basic is executed. return Method: returns the message sent by the publisher to the publisher. You can see that the value of reply-text is NO_ROUTE in the arguments parameter section of the Protocol, indicates that the message is not routed to the appropriate queue;
So how can we get messages that are not correctly routed to the appropriate queue? At this time, you can set the ReturnListener listener for the channel. The specific code (main function ):
Try {ConnectionFactory factory = new ConnectionFactory (); factory. setHost (ip); factory. setPort (port); factory. setUsername (username); factory. setPassword (password); Connection connection = factory. newConnection (); Channel channel = connection. createChannel (); channel. basicQos (1); channel. basicPublish (exchangeName, "", mandatory, immediate, MessageProperties. PERSISTENT_TEXT_PLAIN, "=== mandatory = ". getBytes (); channel. addReturnListener (new ReturnListener () {public void handleReturn (int replyCode, String replyText, String exchange, String routingKey, AMQP. basicProperties basicProperties, byte [] body) throws IOException {String message = new String (body); System. out. println ("Basic. return returned result: "+ message) ;}}); // channel. close (); // connection. close ();} catch (IOException e) {e. printStackTrace ();} catch (TimeoutException e) {e. printStackTrace ();}
Running result:
The returned result of Basic. return is: === mandatory =
Next, let's take a look at setting the mandatory flag and routing exchange to the queue. In the code section, you only need:
channel.basicPublish(exchangeName, "", mandatory, immediate, MessageProperties.PERSISTENT_TEXT_PLAIN, "===mandatory===".getBytes());
Change
channel.basicPublish(exchangeName, routingKey, mandatory, immediate, MessageProperties.PERSISTENT_TEXT_PLAIN, "===mandatory===".getBytes());
You can.
The wireshark is used to capture packets as follows:
We can see that no basic. return method is called. View the RabbitMQ management interface and find that the message has arrived in the queue.
3. immediate
In Versions later than RabbitMQ3.0, the support for the immediate parameter is removed. If you send a publish with the immediate mark, the following error is returned:
"{Amqp_error, not_implemented," immediate = true ", 'Basic. publish '}"
For more information about how to remove the immediate tag, see the following version change description:
Removal of "immediate" flag
What changed? We removed support for the rarely-used "immediate" flag on AMQP's basic. publish.
Why on earth did you do that? Support for "immediate" made than parts of the codebase more complex, Fig. It also stood in the way of our being able to deliver substantial performance improvements in mirrored queues.
What do I need to do? If you just want to be able to publish messages that will be dropped if they are not consumed immediately, you can publish to a queue with a TTL of 0.
If you also need your publisher to be able to determine that this has happened, you can also use the DLX feature to route such messages to another queue, from which the publisher can consume them.
This section explains that the immediate tag will affect the image queue performance and increase Code complexity. It is recommended that you use TTL and DLX instead.
Source: https://yq.aliyun.com/articles/238349? Spm = 5176.8091938.0.0.EfSZh4
Http://www.mamicode.com/info-detail-1673003.html? Spm = 5176.100239.blogcont238349.8.FAbhIg