死信,顾名思义就是无法被消费的消息,字面意思可以这样理解,一般来说,生产者直接将消息投递给交换机或者队列,消费者从队列取出消息进行消费,但某些时候由于特定的原因导致队列中的某些消息无法被消费,这样的消息如果没有后续的处理,就变成了死信,有死信自然就有了死信队列。
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 IOException, TimeoutException {
//创建连接工厂
ConnectionFactory factory = new ConnectionFactory();
// 工厂IP,连接RabbitMQ队列(安装RabbitMQ机器的IP地址)
factory.setHost("xxx.xxx.xxx.xxx");
//用户名
factory.setUsername("username");
//密码
factory.setPassword("password");
// 创建连接
Connection connection = factory.newConnection();
// 获取信道
Channel channel = connection.createChannel();
// 声明死信和普通交换机的类型为DIRECT
channel.exchangeDeclare(NORMAL_EXCHANGE, BuiltinExchangeType.DIRECT);
channel.exchangeDeclare(DEAD_EXCHANGE, BuiltinExchangeType.DIRECT);
Map<String,Object> map = new HashMap<>();
// 设置死信交换机
map.put("x-dead-letter-exchange",DEAD_EXCHANGE);
// 设置死信的routingkey
map.put("x-dead-letter-routing-key","lisi");
// 声明死信和普通队列
channel.queueDeclare(NORMAL_QUEUE, false, false, false, map);
channel.queueDeclare(DEAD_QUEUE, false, false, false, null);
// 绑定普通交换机与队列
channel.queueBind(NORMAL_QUEUE,NORMAL_EXCHANGE,"zhangsan");
// 绑定死信交换机与队列
channel.queueBind(DEAD_QUEUE,DEAD_EXCHANGE,"lisi");
System.out.println("等待接收消息。。。。");
DeliverCallback deliverCallback = (consumerTag, message) -> {
String msg = new String(message.getBody(),"UTF-8");
System.out.println("Consumer01 接收到消息"+message);
};
channel.basicConsume(NORMAL_QUEUE, true, deliverCallback, consumerTag -> {
});
}
}
public class Consumer02 {
//死信队列的名称
public static final String DEAD_QUEUE = "dead_queue";
public static void main(String[] args) throws IOException, TimeoutException {
==========上述的方式获取信道=============
System.out.println("Consumer02等待接收消息。。。。");
DeliverCallback deliverCallback = (consumerTag, message) -> {
System.out.println("Consumer02接收到消息:" + new String(message.getBody(), "UTF-8") + "绑定键:"
+ message.getEnvelope().getRoutingKey());
};
channel.basicConsume(DEAD_QUEUE, true, deliverCallback, consumerTag -> {
});
}
}
public class Producer {
// 普通交换机名称
public static final String NORMAL_EXCHANGE = "normal_exchange";
public static void main(String[] args) throws IOException, TimeoutException {
==========上述的方式获取信道===========
// 发送延迟消息,设置TTL 单位是毫秒
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());
}
}
}
当消费者01停止后,由于发送者发送的消息过期时间到了,所以发送的十条消息发送到死信队列,正常队列中的未确认数变为0
启动消费者02后收到消息
与上述相比,增加了最大队列长度属性map.put("x-max-length",6);
,切记,如果要改变队列和交换机的属性,需要在管理端删除这个队列或者交换机,再重新创建,否则会报错,无法创建,同上面一样,启动后停止运行正常队列,模拟队列无法接受消息
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 IOException, TimeoutException {
=============同初始一样获取信道===========
// 声明死信和普通交换机的类型为DIRECT
channel.exchangeDeclare(NORMAL_EXCHANGE, BuiltinExchangeType.DIRECT);
channel.exchangeDeclare(DEAD_EXCHANGE, BuiltinExchangeType.DIRECT);
Map<String,Object> map = new HashMap<>();
// 设置死信交换机
map.put("x-dead-letter-exchange",DEAD_EXCHANGE);
// 设置死信的routingkey
map.put("x-dead-letter-routing-key","lisi");
// 设置正常队列的长度
map.put("x-max-length",6);
// 声明死信和普通队列
channel.queueDeclare(NORMAL_QUEUE, false, false, false, map);
channel.queueDeclare(DEAD_QUEUE, false, false, false, null);
// 绑定普通交换机与队列
channel.queueBind(NORMAL_QUEUE,NORMAL_EXCHANGE,"zhangsan");
// 绑定死信交换机与队列
channel.queueBind(DEAD_QUEUE,DEAD_EXCHANGE,"lisi");
System.out.println("等待接收消息。。。。");
DeliverCallback deliverCallback = (consumerTag, message) -> {
String msg = new String(message.getBody(),"UTF-8");
};
channel.basicConsume(NORMAL_QUEUE, true, deliverCallback, consumerTag -> {
});
}
}
与上述的最大长度的生产者相同,移除掉过期时间的配置
正常消费者移除掉设置队列最大长度,同时设置手动确认,当消息体为info05时,拒绝接收该消息
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 IOException, TimeoutException {
===========与初始获取信道的方式相同===========
// 声明死信和普通交换机的类型为DIRECT
channel.exchangeDeclare(NORMAL_EXCHANGE, BuiltinExchangeType.DIRECT);
channel.exchangeDeclare(DEAD_EXCHANGE, BuiltinExchangeType.DIRECT);
Map<String,Object> map = new HashMap<>();
// 设置死信交换机
map.put("x-dead-letter-exchange",DEAD_EXCHANGE);
// 设置死信的routingkey
map.put("x-dead-letter-routing-key","lisi");
// 声明死信和普通队列
channel.queueDeclare(NORMAL_QUEUE, false, false, false, map);
channel.queueDeclare(DEAD_QUEUE, false, false, false, null);
// 绑定普通交换机与队列
channel.queueBind(NORMAL_QUEUE,NORMAL_EXCHANGE,"zhangsan");
// 绑定死信交换机与队列
channel.queueBind(DEAD_QUEUE,DEAD_EXCHANGE,"lisi");
System.out.println("等待接收消息。。。。");
DeliverCallback deliverCallback = (consumerTag, message) -> {
String msg = new String(message.getBody(),"UTF-8");
if(msg.equals("info5")){
System.out.println("Consumer01接收到消息是:"+msg+"但是被拒绝");
/**
* 1. 消息的标识
* 2. 是否重新添加到队列
*/
channel.basicReject(message.getEnvelope().getDeliveryTag(),false);
}else {
System.out.println("Consumer01接收到消息:" + new String(message.getBody(), "UTF-8") + "绑定键:"
+ message.getEnvelope().getRoutingKey());
/**
* 1. 消息的标签
* 2. 是否批量应答
*/
channel.basicAck(message.getEnvelope().getDeliveryTag(),false);
}
};
// 设为不自动应答,手动应答
channel.basicConsume(NORMAL_QUEUE, false, deliverCallback, consumerTag -> {
});
}
}
以上就是死信的三种产生方式,当因为各种情况导致上述的三种情景发生时,我们可以使用死信队列来接收消息从而分析为什么会产生死信