赞
踩
目录
生产者
- 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());
- }
-
- }
- }
- 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<String, Object> 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 -> {
- });
- }
- }
- 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());
- }
-
- }
- }
- 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<String, Object> 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());
- }
-
- }
- }
- 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<String, Object> 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 -> {
- });
- }
- }
注意此时需要把原先队列删除 因为参数改变了
总结:
死信队列可能出现的三种情况为:
①消息被拒(消费者方拒绝签收该消息)
②消息设置的TTL过期(生产者方设置的过期时间)
③消息投放的队列内消息已经满了,放不进入时消息会进入死信队列
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。