- listener
The listeners in RabbitMQ include ReturnListener, ConfirmListener, and ShutdownListener. In this exercise, ReturnListener is used. When publishing a message, set mandatory equal to true to monitor whether the message has a matching queue. If not, ReturnListener will execute the handleReturn method, and the message will be returned to the sender. Mandatory=true, return to the message sender when the route cannot reach the queue, receive and
set immediate=true in the return listener, and return when the route cannot reach the consumer. Versions after 3.0 have been abandoned, which will affect the performance of the mirror queue. It is recommended to use Message TTL and DLX
- Points to Note
Mandatory is set to true when sending a message :
//Set mandatory=true, return to the message sender when the route cannot be queued, and receive it in the return listener channel.basicPublish("header_exchange", "" ,true,false,properties.build(), SerializationUtils.serialize(object));
Create a Return listener :
//Add a return listener, when a message is published and there is no matching queue, the message is returned to the receiver channel.addReturnListener(new ReturnListener() { @Override public void handleReturn(int replyCode, String replyText, String exchange, String routingKey, BasicProperties properties, byte[] body) throws IOException { System.out.println(SerializationUtils.deserialize(body)); } });
In addition to the Return listener, there are also ConfirmListener and ShutdownListener listeners
- mandatory, Return listener exercises
package com.demo.mq.rabbitmq.example09; import java.io.IOException; import java.io.Serializable; import java.util.HashMap; import java.util.Map; import org.apache.commons.lang3.SerializationUtils; import com.demo.mq.rabbitmq.MqManager; import com.rabbitmq.client.BuiltinExchangeType; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ReturnListener; import com.rabbitmq.client.AMQP.BasicProperties; import com.rabbitmq.client.AMQP.BasicProperties.Builder; /** * Mandatory, listener use exercise * @author sheungxin * */ public class SendListener { /** * 1. Set mandatory=true, return to the message sender when the route cannot be queued, and receive it in the return listener * 2. Immediate, when the route cannot reach the consumer, it will be returned. The version after 3.0 has been abandoned, which will affect the performance of the mirror queue. It is recommended to use message TTL and DLX. * 3. Listener: ReturnListener: When mandatory=true, the return message is received when there is no matching queue * ConfirmListener: Ack, Nack, confirm mode, server monitoring * ShutdownListener: monitor shutdown * @param object message body * @throws IOException */ public static void sendAToB(Serializable object) throws Exception{ Connection conn=MqManager.newConnection(); Channel channel=conn.createChannel(); //declare the headers forwarder channel.exchangeDeclare("header_exchange", BuiltinExchangeType.HEADERS); //Define the key-value pair stored by headers Map<String, Object> headers=new HashMap<String, Object>(); headers.put("key", "123456"); headers.put("token", "654321"); // put the key-value pair in properties Builder properties=new BasicProperties.Builder(); properties.headers(headers); properties.deliveryMode(2);//Persistence //The specified message expiration time is 12 seconds, and the message expiration time can also be specified on the queue, whichever is smaller. // properties.expiration("12000");//Delayed for 30 seconds, it will not be deleted in time (it is determined whether it expires when consumemr consumes, because the expiration time of each message is inconsistent, deleting expired messages requires scanning the entire queue) //Add a return listener, when a message is published and there is no matching queue, the message is returned to the receiver channel.addReturnListener(new ReturnListener() { @Override public void handleReturn(int replyCode, String replyText, String exchange, String routingKey, BasicProperties properties, byte[] body) throws IOException { System.out.println(SerializationUtils.deserialize(body)); } }); //Set mandatory=true, return to the message sender when the route cannot be queued, and receive it in the return listener channel.basicPublish("header_exchange", "" ,true,false,properties.build(), SerializationUtils.serialize(object)); System.out.println("Send '"+object+"'"); } public static void main(String[] args) throws Exception { sendAToB("Hello World !"); } }