Sorry, your browser cannot access this site
This page requires browser support (enable) JavaScript
Learn more >

死信概念

顾名思义就是无法被消费的消息,一般来说,product将消息投递到broke或者直接到queue里,consumer从queue取出消息进行消费,但某些时候由于特定的原因导致queue中的某些消息无法被消费这样的消息如果没有后续的处理么就变成了死信,有死信自然就有私信队列。

死信的来源

  • 消息TTL过期(超过了设定的消费时间)
  • 消息队列达到了最大长度(队列满了,无法再添加)
  • 消息被拒绝(basic.reject或basic.nack)并且reueue = false

代码练习

image-20220117150921809

消费者

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.bestrookie.rabbitmq.eight;
import com.bestrookie.rabbitmq.utils.RabbitUtils;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
public class Consumer01 {
public static final String NORMAL_EXCHANGE = "normal_exchange";
public static final String NORMAL_QUEUE = "normal_queue";
public static final String DEAL_EXCHANGE = "deal_exchange";
public static final String DEAL_QUEUE = "queue_exchange";
public static void main(String[] args) throws Exception{
Channel channel = RabbitUtils.getChannel();
//声明交换机
channel.exchangeDeclare(NORMAL_EXCHANGE, BuiltinExchangeType.DIRECT);
channel.exchangeDeclare(DEAL_EXCHANGE,BuiltinExchangeType.DIRECT);
//声明队列
Map<String, Object> arguments = new HashMap<>();
//设置过期时间
//arguments.put("x-message-ttl",10000);
//正常队列设置死信队列
arguments.put("x-dead-letter-exchange",DEAL_EXCHANGE);
//设置死信RoutingKey
arguments.put("x-dead-letter-routing-key","rookie");
//设置最大长度
// arguments.put("x-max-length",6);
//声明正常的队列
channel.queueDeclare(NORMAL_QUEUE,false,false,false,arguments);
//声明死信队列
channel.queueDeclare(DEAL_QUEUE,false,false,false,null);
//绑定普通的交换机与队列
channel.queueBind(NORMAL_QUEUE,NORMAL_EXCHANGE,"bestrookie");
channel.queueBind(DEAL_QUEUE,DEAL_EXCHANGE,"rookie");
System.out.println("等待接收消息.....");
DeliverCallback deliverCallback = (tag,msg)->{
String message = new String(msg.getBody(),StandardCharsets.UTF_8);
if (message.equals("info5")){
channel.basicReject(msg.getEnvelope().getDeliveryTag(),false);
}else {
System.out.println("01接收消息:" + message);
channel.basicAck(msg.getEnvelope().getDeliveryTag(),false);
}
};
channel.basicConsume(NORMAL_QUEUE,false,deliverCallback,cancel->{});
}
}

包含三个死信产生的三种情况

消费者(死信队列)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.bestrookie.rabbitmq.eight;
import com.bestrookie.rabbitmq.utils.RabbitUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import java.nio.charset.StandardCharsets;
public class Consumer02 {
public static final String DEAL_QUEUE = "queue_exchange";
public static void main(String[] args) throws Exception{
Channel channel = RabbitUtils.getChannel();
System.out.println("等待接收消息.....");
DeliverCallback deliverCallback = (tag, msg)->{
System.out.println("02接收消息:" + new String(msg.getBody(), StandardCharsets.UTF_8));
};
channel.basicConsume(DEAL_QUEUE,true,deliverCallback,cancel->{});

}
}

生产者

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.bestrookie.rabbitmq.eight;

import com.bestrookie.rabbitmq.utils.RabbitUtils;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
public class Product {
public static final String NORMAL_EXCHANGE = "normal_exchange";
public static void main(String[] args) throws Exception{
Channel channel = RabbitUtils.getChannel();

for (int i = 0; i < 10; i++) {
String msg = "info" + i;
channel.basicPublish(NORMAL_EXCHANGE,"bestrookie",null,msg.getBytes());
System.out.println("消息发送成功: "+msg);
}
}
}

评论