概念
先从概念解释上搞清楚这个定义,死信,顾名思义就是无法被消费的消息,字面意思可以这样理解,一般来说,producer 将消息投递到 broker 或者直接到queue 里了,consumer 从 queue 取出消息 进行消费,但某些时候由于特定的原因导致 queue 中的某些消息无法被消费,这样的消息如果没有后续的处理,就变成了死信,有死信自然就有了死信队列。
应用场景:为了保证订单业务的消息数据不丢失,需要使用到 RabbitMQ 的死信队列机制,当消息消费发生异常时,将消息投入死信队列中。还有比如说:用户在商城下单成功并点击去支付后在指定时间未支付时自动失效。
案例
TLL过期
交换机类型是 direct,两个消费者,一个生产者,两个队列:消息队列和死信队列
/**
* @author shaoshao
* @Date 2022/11/23 19:01
* @Description: 死信队列消费者
*/
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);
//声明普通队列
HashMap<String, Object> arguments = new HashMap<>();
// 过期时间 10s 在生产者那边声明更灵活
// arguments.put("x-message-ttl", 10000);
//正常的队列设置死信交换机
arguments.put("x-dead-letter-exchange", DEAD_EXCHANGE);//图中红箭头
//设置死信routingKey
arguments.put("x-dead-letter-routing-key", "lisi");
channel.queueDeclare(NORMAL_QUEUE, false, false, false, arguments);
//声明死信队列
channel.queueDeclare(DEAD_QUEUE, false, false, false, null);
//绑定普通的交换机与队列
channel.queueBind(NORMAL_QUEUE, NORMAL_EXCHANGE, "zhangSan");
//绑定死信的交换机与队列
channel.queueBind(DEAD_QUEUE, DEAD_EXCHANGE, "lisi");
DeliverCallback deliverCallback = (consumerTag, message) -> {
System.out.println("Consumer01接受的消息是:" + new String(message.getBody(), "UTF-8"));
};
channel.basicConsume(NORMAL_QUEUE, true, deliverCallback, consumerTag -> {
});
}
}
/**
* @author shaoshao
* @Date 2022/11/23 19:01
* @Description: 死信队列消费者
*/
public class Consumer02 {
public static final String DEAD_QUEUE = "dead_queue";
public static void main(String[] args) throws Exception {
Channel channel = RabbitMqUtils.getChannel();
DeliverCallback deliverCallback = (consumerTag, message) -> {
System.out.println("Consumer02接受的消息是:" + new String(message.getBody(), "UTF-8"));
};
channel.basicConsume(DEAD_QUEUE, true, deliverCallback, consumerTag -> {
});
}
}
/**
* @author shaoshao
* @Date 2022/11/23 19:11
* @Description: 死信队列之生产者
*/
public class Product {
public static final String NORMAL_EXCHANGE = "normal_exchange";
public static void main(String[] args) throws Exception {
Channel channel = RabbitMqUtils.getChannel();
// 死信队列 设置TTL时间 10s
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());
}
}
}
先启动C1,然后关闭,模拟C1无法收到消息,然后启动生产者,查看效果。再启动C2接收消息即可
- 未发消息两个队列的状态
- 产者发送了10条消息,此时正常消息队列有10条未消费消息
- 时间过去10秒,正常队列里面的消息由于没有被消费,消息进入死信队列
- 再启动C2(死信队列)接收消息
队列达到最大长度
- C1 消费者修改以下代码(启动之后关闭该消费者 模拟其接收不到消息
/**
* @author shaoshao
* @Date 2022/11/23 19:01
* @Description: 死信队列消费者
*/
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);
//声明普通队列
HashMap<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, "zhangSan");
//绑定死信的交换机与队列
channel.queueBind(DEAD_QUEUE, DEAD_EXCHANGE, "lisi");
DeliverCallback deliverCallback = (consumerTag, message) -> {
System.out.println("Consumer01接受的消息是:" + new String(message.getBody(), "UTF-8"));
};
channel.basicConsume(NORMAL_QUEUE, true, deliverCallback, consumerTag -> {
});
}
}
- 消息生产者代码去掉 TTL 属性,basicPublish 的第三个参数改为 null
/**
* @author shaoshao
* @Date 2022/11/23 19:11
* @Description: 死信队列之生产者
*/
public class Product {
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, "zhangSan", null, message.getBytes());
}
}
}
因为参数改变了,所以需要把原先队列删除
- 启动消费者C1,创建出队列,然后停止该 C1 的运行,启动生产者
- 启动C2
- 查看控制台
消息被拒
即可以指定拒绝哪个消息
/**
* @author shaoshao
* @Date 2022/11/23 19:01
* @Description: 死信队列消费者
*/
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);
//声明普通队列
HashMap<String, Object> arguments = new HashMap<>();
//正常的队列设置死信交换机
arguments.put("x-dead-letter-exchange", DEAD_EXCHANGE);//图中红箭头
//设置死信routingKey
arguments.put("x-dead-letter-routing-key", "lisi");
channel.queueDeclare(NORMAL_QUEUE, false, false, false, arguments);
//声明死信队列
channel.queueDeclare(DEAD_QUEUE, false, false, false, null);
//绑定普通的交换机与队列
channel.queueBind(NORMAL_QUEUE, NORMAL_EXCHANGE, "zhangSan");
//绑定死信的交换机与队列
channel.queueBind(DEAD_QUEUE, DEAD_EXCHANGE, "lisi");
DeliverCallback deliverCallback = (consumerTag, message) -> {
String msg = new String(message.getBody(),"UTF-8");
if (msg.equals("info5")){
System.out.println("Consumer01接收的消息:"+msg+" 此消息是被拒绝的");
channel.basicReject(message.getEnvelope().getDeliveryTag(), false);
} else {
System.out.println("Consumer01接收的消息:"+msg);
}
};
// 开启手动应答 为消息被拒 false
channel.basicConsume(NORMAL_QUEUE, false, deliverCallback, consumerTag -> {
});
}
}
/**
* @author shaoshao
* @Date 2022/11/23 19:01
* @Description: 死信队列消费者
*/
public class Consumer02 {
public static final String DEAD_QUEUE = "dead_queue";
public static void main(String[] args) throws Exception {
Channel channel = RabbitMqUtils.getChannel();
DeliverCallback deliverCallback = (consumerTag, message) -> {
System.out.println("Consumer02接受的消息是:" + new String(message.getBody(), "UTF-8"));
};
channel.basicConsume(DEAD_QUEUE, true, deliverCallback, consumerTag -> {
});
}
}
/**
* @author shaoshao
* @Date 2022/11/23 19:11
* @Description: 死信队列之生产者
*/
public class Product {
public static final String NORMAL_EXCHANGE = "normal_exchange";
public static void main(String[] args) throws Exception {
Channel channel = RabbitMqUtils.getChannel();
// 死信队列 设置TTL时间 10s
// 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());
channel.basicPublish(NORMAL_EXCHANGE, "zhangSan", null, message.getBytes());
}
}
}
- 先启动C1,然后再关闭C1。启动生产者
- 再启动C1消费消息,此时只剩下拒绝的info5消息
- 启动C2