赞
踩
死信,顾名思义就是无法被消费的消息,字面意思可以这样解释,一般来说,producer 将消息投递到 broker 或者直接到 queue 里了,consumer 从 queue 取出消息进行消费,但某些时候由于特定的 原因导致 queue 中的某些消息无法被消费,这样的消息如果没有后续的处理,就变成了死信,有死信自然就有了死信队列。
应用场景:为了保证订单业务的消息数据不丢失,需要使用到 RabbitMQ 的死信队列机制,当消息消费发生异常时,将消息投入死信队列中.还有比如说: 用户在商城下单成功并点击去支付后在指定时间未支付时自动失效
生产者代码
/** * @author dell * @date 2023/7/9 19:41 */ public class Producer { public static final String NORMAL_EXCHANGE = "normal_exchange"; public static void main(String[] args) throws Exception { Channel channel = RabbitMqUtils.getChannel(); channel.exchangeDeclare(NORMAL_EXCHANGE, BuiltinExchangeType.DIRECT); // 设置消息的 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, "zhangsan", properties, message.getBytes()); System.out.println("生产者发送消息: " + message); } } }
消费者C1代码
启动之后关闭该消费者 模拟其接收不到消息
/** * @author dell * @date 2023/7/9 22:05 */ public class Consumer01 { // 普通交换机名称 private static final String NORMAL_EXCHANGE = "normal_exchange"; // 死信交换机名称 private static final String DEAD_EXCHANGE = "dead_exchange"; 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); // 声明死信队列 String deadQueue = "dead-queue"; channel.queueDeclare(deadQueue, false, false, false, null); // 死信队列绑定死信交换机与routingKey channel.queueBind(deadQueue, DEAD_EXCHANGE, "lisi"); // 正常队列绑定死信队列信息 Map<String, Object> params = new HashMap<>(); // 正常队列设置死信交换机 参数 key 是固定值 params.put("x-dead-letter-exchange", DEAD_EXCHANGE); // 正常队列设置死信 routing-key 参数 key 是固定值 params.put("x-dead-letter-routing-key", "lisi"); String normalQueue = "normal-queue"; channel.queueDeclare(normalQueue, false, false, false, params); channel.queueBind(normalQueue, NORMAL_EXCHANGE, "zhangsan"); System.out.println("等待接收消息......"); DeliverCallback deliverCallback = (consumerTag, delivery) -> { String message = new String(delivery.getBody(), "UTF-8"); System.out.println("Consumer01接收消息: " + message); }; channel.basicConsume(normalQueue, true, deliverCallback, consumerTag -> {}); } }
生产者为发送消息
生产者发送了10条消息,此时正常消息队列有10条为消费信息
时间过去 10 秒 正常队列里面的消息由于没有被消费 消息进入死信队列
消费者C2代码
以上步骤完成后 启动 C2 消费者 它消费死信队列里面的消息
/** * @author dell * @date 2023/7/9 23:08 */ public class Consumer02 { public static final String DEAD_EXCHANGE = "dead_exchange"; public static final String DEAD_QUEUE = "dead_queue"; public static void main(String[] args) throws Exception { Channel channel = RabbitMqUtils.getChannel(); channel.exchangeDeclare(DEAD_EXCHANGE, BuiltinExchangeType.DIRECT); channel.queueDeclare(DEAD_QUEUE, false, false, false, null); channel.queueBind(DEAD_QUEUE, DEAD_EXCHANGE, "lisi"); System.out.println("等待接收死信队列消息"); DeliverCallback deliverCallback = (consumerTag, message) -> { System.out.println("Consumer02接收的消息是" + new String(message.getBody(), "UTF-8")); }; channel.basicConsume(DEAD_QUEUE, true, deliverCallback, consumerTag -> {}); } }
/** * @author dell * @date 2023/7/9 19:41 */ public class Producer { public static final String NORMAL_EXCHANGE = "normal_exchange"; public static void main(String[] args) throws Exception { Channel channel = RabbitMqUtils.getChannel(); channel.exchangeDeclare(NORMAL_EXCHANGE, BuiltinExchangeType.DIRECT); // 该信息是用作演示队列个数限制 for (int i = 1; i < 11; i++) { String message = "info" + i; channel.basicPublish(NORMAL_EXCHANGE, "zhangsan", null, message.getBytes()); System.out.println("生产者发送消息: " + message); } } }
启动之后关闭该消费者 模拟其接收不到消息
注意此时需要原先队列删除 因为参数改变了
消息生产者代码同上生产者一致
C1 消费者代码
启动之后关闭该消费者 模拟其接收不到消息
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。