推荐阅读1 推荐阅读2 推荐阅读3
死信队列全称 Dead-Letter-Exchange 简称 DLX 是 RabbitMQ 中交换器的一种类型,消息在一段时间之后没有被消费就会变成死信被重新 publish 到另一个 DLX 交换器队列中,因此称为死信队列。死信队列用于处理无法被正常消费的消息,即死信消息。
死信队列产生几种情况:
basic.reject / basic.nack
),并且requeue = false
应用的场景
为了保证订单业务的消息数据不丢失,需要使用到RabbitMQ的死信队列机制,当消息消费发生异常时,将消息投入死信队列中。
死信处理过程
消费者1
package com.uin.dead_Exchange;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.uin.utils.RabbitMQUtils;
import java.io.IOException;
import java.util.HashMap;
import java.util.concurrent.TimeoutException;
/**
* @author wanglufei
* @description: TODO
* @date 2022/2/1/7:17 PM
*/
public class Consumer1 {
//普通的交换机
public static final String NORMAL_EXCHANGE = "normal_exchange2";
//死信的交换机
public static final String DEAD_EXCHANGE = "dead_exchange2";
//普通的队列
public static final String NORMAL_QUEUE = "normal_queue2";
//死信的队列
public static final String DEAD_QUEUE = "dead_queue2";
public static void main(String[] args) throws IOException, TimeoutException {
Channel channel = RabbitMQUtils.getChannel();
//声明交换机
channel.exchangeDeclare(NORMAL_EXCHANGE, BuiltinExchangeType.DIRECT);
channel.exchangeDeclare(DEAD_EXCHANGE, BuiltinExchangeType.DIRECT);
//普通队列要转发到死信交换机需要一些参数 arguments
/**
* 消息拒绝
* 消息TTl过期
* 队列达到最长的长度
*/
HashMap arguments = new HashMap<>();
//设置过期时间
//arguments.put("x-dead-ttl",10000);
//设置正常队列为死信交换机
arguments.put("X-dead-letter-exchange", DEAD_EXCHANGE);
//设置死信的routingkey
arguments.put("X-dead-letter-routing-key", "lisi");
//声明队列
channel.queueDeclare(NORMAL_QUEUE, true, false, false,arguments );
channel.queueDeclare(DEAD_QUEUE, true, false, false, null);
//普通的队列和普通的交换进行绑定
channel.queueBind(NORMAL_QUEUE, NORMAL_EXCHANGE, "zhangsan");
//死信的交换机和死信的队列 进行绑定
channel.queueBind(DEAD_QUEUE, DEAD_EXCHANGE, "lisi");
System.out.println("C1等待接受消息。。。");
//消费消息
DeliverCallback deliverCallback = (consumerTag, message) -> {
String s = new String(message.getBody(), "UTF-8");
System.out.println("C1接收到的消息" + s);
};
channel.basicConsume(NORMAL_QUEUE, true, deliverCallback, consumerTag -> {
System.out.println("消息取消消费的接口回调");
});
}
}
生产者:
package com.uin.dead_Exchange;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.uin.utils.RabbitMQUtils;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;
/**
* @author wanglufei
* @description: TODO
* @date 2022/2/1/7:17 PM
*/
public class Producer {
private static final String NORMAL_EXCHANGE = "normal_exchange2";
public static void main(String[] args) throws IOException, TimeoutException {
Channel channel = RabbitMQUtils.getChannel();
//声明交换机
channel.exchangeDeclare(NORMAL_EXCHANGE, BuiltinExchangeType.DIRECT);
//设置消息的过期时间 TTL 参数
AMQP.BasicProperties properties = new AMQP.BasicProperties().builder().expiration(
"1000").build();
//模拟发送消息
for (int i = 0; i < 10; i++) {
String message = "info " + i;
channel.basicPublish(NORMAL_EXCHANGE, "zhangsan", properties,
message.getBytes(StandardCharsets.UTF_8));
}
}
}
队列的名称为2 。
消费者2
package com.uin.dead_Exchange;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.uin.utils.RabbitMQUtils;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
* @author wanglufei
* @description: TODO
* @date 2022/2/1/7:17 PM
*/
public class Consumer2 {
//死信的队列
public static final String DEAD_QUEUE = "dead_queue2";
public static void main(String[] args) throws IOException, TimeoutException {
Channel channel = RabbitMQUtils.getChannel();
System.out.println("C2等待接受消息。。。");
//消费消息
DeliverCallback deliverCallback = (consumerTag, message) -> {
String s = new String(message.getBody(), "UTF-8");
System.out.println("C2接收到的消息" + s);
};
channel.basicConsume(DEAD_QUEUE, true, deliverCallback, consumerTag -> {
System.out.println("消息取消消费的接口回调");
});
}
}
普通的队列将消息转发给死信队列接受的消息
消费者代码的改动
生产者代码的改动:
运行c1,并断掉c1,运行生产者,运行死信的消费者c2
package com.uin.dead_Exchange.max_length;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.uin.utils.RabbitMQUtils;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeoutException;
/**
* @author wanglufei
* @description: TODO
* @date 2022/2/1/7:17 PM
*/
public class Consumer1 {
//普通的交换机
public static final String NORMAL_EXCHANGE = "normal_exchange3";
//死信的交换机
public static final String DEAD_EXCHANGE = "dead_exchange3";
//普通的队列
public static final String NORMAL_QUEUE = "normal_queue3";
//死信的队列
public static final String DEAD_QUEUE = "dead_queue3";
public static void main(String[] args) throws IOException, TimeoutException {
Channel channel = RabbitMQUtils.getChannel();
//声明交换机
channel.exchangeDeclare(NORMAL_EXCHANGE, BuiltinExchangeType.DIRECT);
channel.exchangeDeclare(DEAD_EXCHANGE, BuiltinExchangeType.DIRECT);
//普通队列要转发到死信交换机需要一些参数 arguments
/**
* 消息拒绝
* 消息TTl过期
* 队列达到最长的长度
*/
Map arguments = new HashMap<>();
//设置过期时间
//arguments.put("x-dead-ttl",10000);
//设置正常队列为死信交换机
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, true, false, false, arguments);
channel.queueDeclare(DEAD_QUEUE, true, false, false, null);
//普通的队列和普通的交换进行绑定
channel.queueBind(NORMAL_QUEUE, NORMAL_EXCHANGE, "zhangsan");
//死信的交换机和死信的队列 进行绑定
channel.queueBind(DEAD_QUEUE, DEAD_EXCHANGE, "lisi");
System.out.println("C1等待接受消息。。。");
//消费消息
DeliverCallback deliverCallback = (consumerTag, message) -> {
String s = new String(message.getBody(), "UTF-8");
if (s.equals("info_message 5")) {
System.out.println("Consumer01 接收到消息" + s + "并拒绝签收该消息");
//requeue 设置为 false 代表拒绝重新入队 该队列如果配置了死信交换机将发送到死信队列中
channel.basicReject(message.getEnvelope().getDeliveryTag(), false);
} else {
System.out.println("Consumer01 接收到消息" + s);
channel.basicAck(message.getEnvelope().getDeliveryTag(), false);
}
};
channel.basicConsume(NORMAL_QUEUE, false, deliverCallback, consumerTag -> {
System.out.println("消息取消消费的接口回调");
});
}
}
package com.uin.dead_Exchange.reject_message;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.uin.utils.RabbitMQUtils;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
* @author wanglufei
* @description: TODO
* @date 2022/2/1/7:17 PM
*/
public class Consumer2 {
//死信的队列
public static final String DEAD_QUEUE = "dead_queue3";
public static void main(String[] args) throws IOException, TimeoutException {
Channel channel = RabbitMQUtils.getChannel();
System.out.println("C2等待接受消息。。。");
//消费消息
DeliverCallback deliverCallback = (consumerTag, message) -> {
String s = new String(message.getBody(), "UTF-8");
System.out.println("C2接收到的消息" + s);
};
channel.basicConsume(DEAD_QUEUE, true, deliverCallback, consumerTag -> {
System.out.println("消息取消消费的接口回调");
});
}
}
测试结果: