六、死信队列
6.1 死信的概念
==死信,顾名思义就是无法被消费的消息==字面意思可以这样理解,一般来说,producer 将消息投递到 broker 或者直接到queue 里了,consumer 从 queue 取出消息 进行消费,但某些时候由于特定的原因导致 queue 中的某些消息无法被消费,这样的消息如果没有后续的处理,就变成了死信,有死信自然就有了死信队列。
应用场景:为了保证订单业务的消息数据不丢失,需要使用到 RabbitMQ 的死信队列机制,当消息消费发生异常时,将消息投入死信队列中。还有比如说:用户在商城下单成功并点击去支付后在指定时间未支付时自动失效
6.2 死信的来源
- 消息 TTL 过期
TTL是Time To Live的缩写, 也就是生存时间 - 队列达到最大长度
队列满了,无法再添加数据到 mq 中 - 消息被拒绝
(basic.reject 或 basic.nack) 并且 requeue=false.
6.3 死信实战
6.3.1 代码架构图
6.3.2 消息TTL过期
生产者代码
package com.caq.rabbitmq.deadqueue; import com.caq.rabbitmq.utils.RabbitMqUtils; import com.rabbitmq.client.AMQP; import com.rabbitmq.client.Channel; import com.rabbitmq.client.impl.AMQBasicProperties; import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; /** * 死信队列生产者 */ public class Producer { //交换机的名字 public static final String NORMAL_EXCHANGE = "normal_exchange"; //发送消息 public static void main(String[] args) throws Exception { Channel channel = RabbitMqUtils.getChannel(); //死信消息,设置TTL时间 AMQP.BasicProperties properties = new AMQP.BasicProperties() .builder().expiration("10000").build(); for (int i = 0; i < 11; i++) { String message = "info" + i; channel.basicPublish(NORMAL_EXCHANGE, "zs", properties,message.getBytes(StandardCharsets.UTF_8)); System.out.println("生产者发送消息:"+message); } } }
消费者 C1 代码:
package com.caq.rabbitmq.deadqueue; import com.caq.rabbitmq.utils.RabbitMqUtils; import com.rabbitmq.client.BuiltinExchangeType; import com.rabbitmq.client.Channel; import com.rabbitmq.client.DeliverCallback; import java.util.HashMap; import java.util.Map; public class Consumer01 { public static final String NORMAL_EXCHANGE = "normal_exchange"; public static final String DEAD_EXCHANGE = "dead_exchange"; public static final String NORMAL_QUEUE = "normal_queue"; public static final String DEAD_QUEUE = "dead_queue"; public static void main(String[] args) throws Exception { Channel channel = RabbitMqUtils.getChannel(); // 声明死信和普通交换机 类型为direct channel.exchangeDeclare(NORMAL_EXCHANGE, BuiltinExchangeType.DIRECT); channel.exchangeDeclare(DEAD_EXCHANGE, BuiltinExchangeType.DIRECT); // 声明普通队列 Map<String, Object> arguments = new HashMap<>(); //过期时间 // 正常队列设置死信交换机 arguments.put("x-dead-letter-exchange", DEAD_EXCHANGE); // 设置死信routingkey arguments.put("x-dead-letter-routing-key", "lisi"); // 设置队列长度 // arguments.put("x-max-length", 6); channel.queueDeclare(NORMAL_QUEUE, false, false, false, arguments); // 声明死信队列 channel.queueDeclare(DEAD_QUEUE, false, false, false, null); //绑定普通交换机与普通队列 channel.queueBind(NORMAL_QUEUE, NORMAL_EXCHANGE, "zs"); //绑定死信交换机与死信队列 channel.queueBind(DEAD_QUEUE, DEAD_EXCHANGE, "lisi"); System.out.println("等待接受消息.........."); DeliverCallback deliverCallback = (consumerTag, message) -> { String msg = new String(message.getBody()); System.out.println("Consumer01 接收到消息"+message); }; //开启手动应答 channel.basicConsume(NORMAL_QUEUE, true, deliverCallback, consumerTag -> { }); } }
启动 C1 ,之后关闭消费者,模拟其接收不到消息。再启动 Producer
消费者 C2 代码:
以上步骤完成后,启动 C2 消费者,它消费死信队列里面的消息
package com.caq.rabbitmq.deadqueue; import com.caq.rabbitmq.utils.RabbitMqUtils; import com.rabbitmq.client.BuiltinExchangeType; import com.rabbitmq.client.Channel; import com.rabbitmq.client.DeliverCallback; import java.util.HashMap; import java.util.Map; /** * 死信队列 * 消费者2 */ public class Consumer02 { public static final String NORMAL_QUEUE = "dead_queue"; public static void main(String[] args) throws Exception { Channel channel = RabbitMqUtils.getChannel(); System.out.println("等待接受消息.........."); DeliverCallback deliverCallback = (consumerTag, message) -> { System.out.println("Consumer02接受的消息是:" + new String(message.getBody())); }; channel.basicConsume(NORMAL_QUEUE, true, deliverCallback, consumerTag -> { }); } }
6.3.3 队列达到最大长度
消息生产者代码去掉 TTL 属性
AMQP.BasicProperties properties = new AMQP.BasicProperties() .builder().expiration("10000").build();
消息生产者代码去掉 TTL 属性
package com.caq.rabbitmq.deadqueue; import com.caq.rabbitmq.utils.RabbitMqUtils; import com.rabbitmq.client.AMQP; import com.rabbitmq.client.Channel; import com.rabbitmq.client.impl.AMQBasicProperties; import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; /** * 死信队列生产者 */ public class Producer { //交换机的名字 public static final String NORMAL_EXCHANGE = "normal_exchange"; //发送消息 public static void main(String[] args) throws Exception { Channel channel = RabbitMqUtils.getChannel(); for (int i = 0; i < 11; i++) { String message = "info" + i; channel.basicPublish(NORMAL_EXCHANGE, "zs", null,message.getBytes(StandardCharsets.UTF_8)); System.out.println("生产者发送消息:"+message); } } }
C1 消费者修改以下代码**(启动之后关闭该消费者 模拟其接收不到消息)**
//设置正常队列的长度限制,例如发10个,4个则为死信 params.put("x-max-length",6);
注意此时需要把原先队列删除 因为参数改变了
package com.caq.rabbitmq.deadqueue; import com.caq.rabbitmq.utils.RabbitMqUtils; import com.rabbitmq.client.BuiltinExchangeType; import com.rabbitmq.client.Channel; import com.rabbitmq.client.DeliverCallback; import java.util.HashMap; import java.util.Map; public class Consumer01 { public static final String NORMAL_EXCHANGE = "normal_exchange"; public static final String DEAD_EXCHANGE = "dead_exchange"; public static final String NORMAL_QUEUE = "normal_queue"; public static final String DEAD_QUEUE = "dead_queue"; public static void main(String[] args) throws Exception { Channel channel = RabbitMqUtils.getChannel(); // 声明死信和普通交换机 类型为direct channel.exchangeDeclare(NORMAL_EXCHANGE, BuiltinExchangeType.DIRECT); channel.exchangeDeclare(DEAD_EXCHANGE, BuiltinExchangeType.DIRECT); // 声明普通队列 Map<String, Object> arguments = new HashMap<>(); // 正常队列设置死信交换机 arguments.put("x-dead-letter-exchange", DEAD_EXCHANGE); // 设置死信routingkey arguments.put("x-dead-letter-routing-key", "lisi"); // 设置队列长度 arguments.put("x-max-length", 6); channel.queueDeclare(NORMAL_QUEUE, false, false, false, arguments); /******************************************************************/ // 声明死信队列 channel.queueDeclare(DEAD_QUEUE, false, false, false, null); //绑定普通交换机与普通队列 channel.queueBind(NORMAL_QUEUE, NORMAL_EXCHANGE, "zs"); //绑定死信交换机与死信队列 channel.queueBind(DEAD_QUEUE, DEAD_EXCHANGE, "lisi"); System.out.println("等待接受消息.........."); DeliverCallback deliverCallback = (consumerTag, delivery) -> { String message = new String(delivery.getBody(), "UTF-8"); System.out.println("Consumer01 接收到消息"+message); channel.basicConsume(NORMAL_QUEUE, true, deliverCallback, consumerTag -> { }); } }
C2 消费者代码不变(启动 C2 消费者)
6.3.4 消息被拒
生产者,C2消费者代码同上生产者一致
C1 消费者代码(启动之后关闭该消费者 模拟其接收不到消息)
package com.caq.rabbitmq.deadqueue; import com.caq.rabbitmq.utils.RabbitMqUtils; import com.rabbitmq.client.BuiltinExchangeType; import com.rabbitmq.client.Channel; import com.rabbitmq.client.DeliverCallback; import java.util.HashMap; import java.util.Map; /** * 死信队列 * <p> * 消费者1 */ public class Consumer01 { public static final String NORMAL_EXCHANGE = "normal_exchange"; public static final String DEAD_EXCHANGE = "dead_exchange"; public static final String NORMAL_QUEUE = "normal_queue"; public static final String DEAD_QUEUE = "dead_queue"; public static void main(String[] args) throws Exception { Channel channel = RabbitMqUtils.getChannel(); // 声明死信和普通交换机 类型为direct channel.exchangeDeclare(NORMAL_EXCHANGE, BuiltinExchangeType.DIRECT); channel.exchangeDeclare(DEAD_EXCHANGE, BuiltinExchangeType.DIRECT); // 声明普通队列 Map<String, Object> arguments = new HashMap<>(); //过期时间 // 正常队列设置死信交换机 arguments.put("x-dead-letter-exchange", DEAD_EXCHANGE); // 设置死信routingkey arguments.put("x-dead-letter-routing-key", "lisi"); // 设置队列长度 // arguments.put("x-max-length", 6); channel.queueDeclare(NORMAL_QUEUE, false, false, false, arguments); /******************************************************************/ // 声明死信队列 channel.queueDeclare(DEAD_QUEUE, false, false, false, null); //绑定普通交换机与普通队列 channel.queueBind(NORMAL_QUEUE, NORMAL_EXCHANGE, "zs"); //绑定死信交换机与死信队列 channel.queueBind(DEAD_QUEUE, DEAD_EXCHANGE, "lisi"); System.out.println("等待接受消息.........."); DeliverCallback deliverCallback = (consumerTag, message) -> { String msg = new String(message.getBody()); if (msg.equals("info5")) { System.out.println("消费者1接受的消息是" + msg + ":此消息是被c1拒绝的"); channel.basicReject(message.getEnvelope().getDeliveryTag(),false); } else { System.out.println("Consumer01接受的消息是:" + msg); } }; //开启手动应答 channel.basicConsume(NORMAL_QUEUE, false, deliverCallback, consumerTag -> { }); } }