目录 一、概念 二、出现死信的原因 三、实战 (一)代码架构图 (二)消息被拒 (三)消息TTL过期 (四)队列达到最大长度 一、概念 先从概念解释上搞清楚这个定义,死信,顾名思义就是无法被消费的消息,字面意思可以这样理解,一般来说
目录
生产者
public class Producer { public static final String NORMAL_EXCHANGE = "normal_exchange"; public static void main(String[] args) throws ioException { Channel channel = RabbitMqUtils.getChannel(); for (int i = 0; i < 10; i++) { String message = "info" + i; channel.basicPublish(NORMAL_EXCHANGE, "zhangsan", null, message.getBytes()); } }}
C1 消费者代码( 启动之后关闭该消费者 模拟其接收不到消息 ) public class Consumer01 { public static final String NORMAL_QUEUE = "normal_queue"; public static final String NORMAL_EXCHANGE = "normal_exchange"; public static final String DEAD_QUEUE = "dead_queue"; public static final String DEAD_EXCHANGE = "dead_exchange"; public static void main(String[] args) throws IOException { Channel channel = RabbitMqUtils.getChannel(); // 声明普通和死信交换机 channel.exchangeDeclare(NORMAL_EXCHANGE, BuiltinExchangeType.DIRECT); channel.exchangeDeclare(DEAD_EXCHANGE, BuiltinExchangeType.DIRECT); // 声明死信队列 channel.queueDeclare(DEAD_QUEUE, false, false, false, null); // 死信的绑定 channel.queueBind(DEAD_QUEUE, DEAD_EXCHANGE, "lisi"); Map arguments = new HashMap<>(); // 普通队列设置对应的交换机 arguments.put("x-dead-letter-exchange", DEAD_EXCHANGE); // 设置死信队列的RouteKey arguments.put("x-dead-letter-routing-key", "lisi"); // 设置队列最大长度 arguments.put("x-max-length", 6); // 声明普通队列 channel.queueDeclare(NORMAL_QUEUE, false, false, false, arguments); // 普通的绑定 channel.queueBind(NORMAL_QUEUE, NORMAL_EXCHANGE, "zhangsan"); DeliverCallback deliverCallback = (consumerTag, message) -> { String msg = new String(message.getBody()); if (msg.equals("info5")) { System.out.println("Consumer01接收到消息" + message + "并拒绝签收该消息"); channel.basicReject(message.getEnvelope().getDeliveryTag(), false); } else { System.out.println("consumer01接收到消息:" + msg); channel.basicAck(message.getEnvelope().getDeliveryTag(), false); } }; channel.basicConsume(NORMAL_QUEUE, false, deliverCallback, consumerTag -> { }); }}
C2 消费者代码不变 启动消费者 1 然后再启动消费者 2
public class Producer { public static final String NORMAL_EXCHANGE = "normal_exchange"; public static void main(String[] args) throws IOException { Channel channel = RabbitMqUtils.getChannel(); AMQP.BasicProperties properties = new AMQP.BasicProperties() .builder().expiration("10000").build(); for (int i = 0; i < 10; i++) { String message = "info" + i; channel.basicPublish(NORMAL_EXCHANGE, "zhangsan", properties, message.getBytes()); } }}
消费者 C1 代码 ( 启动之后关闭该消费者 模拟其接收不到消息 ) public class Consumer01 { public static final String NORMAL_QUEUE = "normal_queue"; public static final String NORMAL_EXCHANGE = "normal_exchange"; public static final String DEAD_QUEUE = "dead_queue"; public static final String DEAD_EXCHANGE = "dead_exchange"; public static void main(String[] args) throws IOException { Channel channel = RabbitMqUtils.getChannel(); // 声明普通和死信交换机 channel.exchangeDeclare(NORMAL_EXCHANGE, BuiltinExchangeType.DIRECT); channel.exchangeDeclare(DEAD_EXCHANGE, BuiltinExchangeType.DIRECT); // 声明死信队列 channel.queueDeclare(DEAD_QUEUE, false, false, false, null); // 死信的绑定 channel.queueBind(DEAD_QUEUE, DEAD_EXCHANGE, "lisi"); Map arguments = new HashMap<>(); // 普通队列设置对应的交换机 arguments.put("x-dead-letter-exchange", DEAD_EXCHANGE); // 设置死信队列的RouteKey arguments.put("x-dead-letter-routing-key", "lisi"); // 声明普通队列 channel.queueDeclare(NORMAL_QUEUE, false, false, false, arguments); // 普通的绑定 channel.queueBind(NORMAL_QUEUE, NORMAL_EXCHANGE, "zhangsan"); DeliverCallback deliverCallback = (consumerTag, message) -> { String msg = new String(message.getBody()); System.out.println("consumer01接收到消息:" + msg); }; channel.basicConsume(NORMAL_QUEUE, false, deliverCallback, consumerTag -> { }); }}
消费者 C2 代码(以上步骤完成后 启动 C2 消费者 它消费死信队列里面的消息)
public class Consumer02 { public static final String DEAD_QUEUE = "dead_queue"; public static void main(String[] args) throws IOException { Channel channel = RabbitMqUtils.getChannel(); System.out.println("等待接收死信队列消息....."); DeliverCallback deliverCallback = (consumerTag, message) -> { System.out.println("Consumer02 接收死信队列的消息:" + new String(message.getBody())); }; channel.basicConsume(DEAD_QUEUE, true, deliverCallback, consumerTag -> { }); }}
我们在声明普通队列时添加一个参数x-max-length即可
生产者
public class Producer { public static final String NORMAL_EXCHANGE = "normal_exchange"; public static void main(String[] args) throws IOException { Channel channel = RabbitMqUtils.getChannel(); for (int i = 0; i < 10; i++) { String message = "info" + i; channel.basicPublish(NORMAL_EXCHANGE, "zhangsan", null, message.getBytes()); } }}
C1 消费者修改以下代码 ( 启动之后关闭该消费者 模拟其接收不到消息 ),这里设置了队列最多容纳6条消息,此时由于生产者发送了10条消息,所以有4条会进入死信队列。 public class Consumer01 { public static final String NORMAL_QUEUE = "normal_queue"; public static final String NORMAL_EXCHANGE = "normal_exchange"; public static final String DEAD_QUEUE = "dead_queue"; public static final String DEAD_EXCHANGE = "dead_exchange"; public static void main(String[] args) throws IOException { Channel channel = RabbitMqUtils.getChannel(); // 声明普通和死信交换机 channel.exchangeDeclare(NORMAL_EXCHANGE, BuiltinExchangeType.DIRECT); channel.exchangeDeclare(DEAD_EXCHANGE, BuiltinExchangeType.DIRECT); // 声明死信队列 channel.queueDeclare(DEAD_QUEUE, false, false, false, null); // 死信的绑定 channel.queueBind(DEAD_QUEUE, DEAD_EXCHANGE, "lisi"); Map arguments = new HashMap<>(); // 普通队列设置对应的交换机 arguments.put("x-dead-letter-exchange", DEAD_EXCHANGE); // 设置死信队列的RouteKey arguments.put("x-dead-letter-routing-key", "lisi"); // 设置队列最大长度 arguments.put("x-max-length", 6); // 声明普通队列 channel.queueDeclare(NORMAL_QUEUE, false, false, false, arguments); // 普通的绑定 channel.queueBind(NORMAL_QUEUE, NORMAL_EXCHANGE, "zhangsan"); DeliverCallback deliverCallback = (consumerTag, message) -> { String msg = new String(message.getBody()); System.out.println("consumer01接收到消息:" + msg); }; channel.basicConsume(NORMAL_QUEUE, false, deliverCallback, consumerTag -> { }); }}
注意此时需要把原先队列删除 因为参数改变了C2 消费者代码不变 ( 启动 C2 消费者)
总结:
死信队列可能出现的三种情况为:
①消息被拒(消费者方拒绝签收该消息)
②消息设置的TTL过期(生产者方设置的过期时间)
③消息投放的队列内消息已经满了,放不进入时消息会进入死信队列
来源地址:https://blog.csdn.net/m0_62946761/article/details/129240859
--结束END--
本文标题: RabbitMQ死信队列
本文链接: https://lsjlt.com/news/405989.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-04-01
2024-04-03
2024-04-03
2024-01-21
2024-01-21
2024-01-21
2024-01-21
2023-12-23
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0