目录
一、死信的概念
先从概念解释上搞清楚这个定义,死信,顾名思义就是无法被消费的消息,字面意思可以这样理
解,一般来说,producer 将消息投递到 broker 或者直接到 queue 里了,consumer 从 queue 取出消息进行消费,但某些时候由于特定的原因导致 queue 中的某些消息无法被消费,这样的消息如果没有后续的处理,就变成了死信,有死信自然就有了死信队列。
应用场景:为了保证订单业务的消息数据不丢失,需要使用到 rabbitmq 的死信队列机制,当消息
消费发生异常时,将消息投入死信队列中.还有比如说: 用户在商城下单成功并点击去支付后在指定时间未支付时自动失效
二、死信的来源
三、死信实战
1、代码架构图
生产者正常情况下走的是普通的交换机,这个交换机的类型是 direct ,它和普通队列之间的关系是一个叫 "zhangsan" 的路由 key, 正常情况下会被 c1 消费。
但是发生了上面所说的三种情况中的一种,成为了死信,然后被转换到死信交换机中,这个死信交换机也是 direct 类型,它们之间的 routingkey 是 "lisi",然后就进入了死信队列,死信队列由 c2 消费。
2、消息 ttl 过期
(1)消费者
// 死信队列 实战
// 消费者 1
public class comsumer01 {
// 普通交换机名称
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 {
channel channel = rabbitmqutils.getchannel();
// 交换机的声明
channel.exchangedeclare(normal_exchange, builtinexchangetype.direct);
channel.exchangedeclare(dead_exchange,builtinexchangetype.direct);
// 普通队列的声明
map<string, object> arguments = new hashmap<>();
// 过期时间
//arguments.put("x-message-ttl",100000);
// 正常队列设置死信交换机
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");
system.out.println("等待接收消息.....");
delivercallback delivercallback = ( consumertag, message) ->{
system.out.println("consumer01 接收的消息是: " + new string(message.getbody(),"utf-8"));
};
cancelcallback cancelcallback = consumertag->{};
channel.basicconsume(normal_queue,true,delivercallback,cancelcallback);
}
}
// 消费者 2
public class comsumer02 {
// 死信队列名称
public static final string dead_queue = "dead_queue";
public static void main(string[] args) throws ioexception, timeoutexception {
channel channel = rabbitmqutils.getchannel();
system.out.println("等待接收消息.....");
delivercallback delivercallback = ( consumertag, message) ->{
system.out.println("consumer02 接收的消息是: " + new string(message.getbody(),"utf-8"));
};
cancelcallback cancelcallback = consumertag->{};
channel.basicconsume(dead_queue,true,delivercallback,cancelcallback);
}
}
(2)生产者
// 死信队列 生产者代码
public class producer {
// 普通交换机名称
public static final string normal_exchange = "normal_exchange";
public static void main(string[] args) throws ioexception, timeoutexception {
channel channel = rabbitmqutils.getchannel();
// 死信消息 设置 ttl 的时间
amqp.basicproperties properties = new amqp.basicproperties().
builder().expiration("10000").build();
for (int i = 1; i < 11; i++) {
string message = "info" + i;
channel.basicpublish(normal_exchange,"zhangsan",properties,message.getbytes());
}
}
}
(3)结果展示
3、队列达到最大长度
(1)消费者
// 死信队列 实战
// 消费者 1
public class comsumer01 {
// 普通交换机名称
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 {
channel channel = rabbitmqutils.getchannel();
// 交换机的声明
channel.exchangedeclare(normal_exchange, builtinexchangetype.direct);
channel.exchangedeclare(dead_exchange,builtinexchangetype.direct);
// 普通队列的声明
map<string, object> arguments = new hashmap<>();
// 过期时间
//arguments.put("x-message-ttl",100000);
// 正常队列设置死信交换机
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");
system.out.println("等待接收消息.....");
delivercallback delivercallback = ( consumertag, message) ->{
system.out.println("consumer01 接收的消息是: " + new string(message.getbody(),"utf-8"));
};
cancelcallback cancelcallback = consumertag->{};
channel.basicconsume(normal_queue,true,delivercallback,cancelcallback);
}
}
// 死信队列 实战
// 消费者 2
public class comsumer02 {
// 死信队列名称
public static final string dead_queue = "dead_queue";
public static void main(string[] args) throws ioexception, timeoutexception {
channel channel = rabbitmqutils.getchannel();
system.out.println("等待接收消息.....");
delivercallback delivercallback = ( consumertag, message) ->{
system.out.println("consumer02 接收的消息是: " + new string(message.getbody(),"utf-8"));
};
cancelcallback cancelcallback = consumertag->{};
channel.basicconsume(dead_queue,true,delivercallback,cancelcallback);
}
}
(2)生产者
// 死信队列 生产者代码
public class producer {
// 普通交换机名称
public static final string normal_exchange = "normal_exchange";
public static void main(string[] args) throws ioexception, timeoutexception {
channel channel = rabbitmqutils.getchannel();
// 死信消息 设置 ttl 的时间
/*amqp.basicproperties properties = new amqp.basicproperties().
builder().expiration("10000").build();*/
for (int i = 1; i < 11; i++) {
string message = "info" + i;
channel.basicpublish(normal_exchange,"zhangsan",null,message.getbytes());
}
}
}
(3)结果展示
4、消息被拒
(1)消费者
// 死信队列 实战
// 消费者 1
public class comsumer01 {
// 普通交换机名称
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 {
channel channel = rabbitmqutils.getchannel();
// 交换机的声明
channel.exchangedeclare(normal_exchange, builtinexchangetype.direct);
channel.exchangedeclare(dead_exchange,builtinexchangetype.direct);
// 普通队列的声明
map<string, object> arguments = new hashmap<>();
// 过期时间
//arguments.put("x-message-ttl",100000);
// 正常队列设置死信交换机
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");
system.out.println("等待接收消息.....");
delivercallback delivercallback = ( consumertag, message) ->{
string msg = new string(message.getbody(),"utf-8");
if (msg.equals("info5")){
system.out.println("consumer01 接收的消息是: " + msg + ": 此消息是被 c1 拒绝的");
// 拒绝,且不放囧普通队列
channel.basicreject(message.getenvelope().getdeliverytag(),false);
}else {
system.out.println("consumer01 接收的消息是: " + new string(message.getbody(),"utf-8"));
channel.basicack(message.getenvelope().getdeliverytag(),false);
}
};
cancelcallback cancelcallback = consumertag->{};
// 开启手动应答
channel.basicconsume(normal_queue,false,delivercallback,cancelcallback);
}
}
// 死信队列 实战
// 消费者 2
public class comsumer02 {
// 死信队列名称
public static final string dead_queue = "dead_queue";
public static void main(string[] args) throws ioexception, timeoutexception {
channel channel = rabbitmqutils.getchannel();
system.out.println("等待接收消息.....");
delivercallback delivercallback = ( consumertag, message) ->{
system.out.println("consumer02 接收的消息是: " + new string(message.getbody(),"utf-8"));
};
cancelcallback cancelcallback = consumertag->{};
channel.basicconsume(dead_queue,true,delivercallback,cancelcallback);
}
}
(2)生产者
// 死信队列 生产者代码
public class producer {
// 普通交换机名称
public static final string normal_exchange = "normal_exchange";
public static void main(string[] args) throws ioexception, timeoutexception {
channel channel = rabbitmqutils.getchannel();
// 死信消息 设置 ttl 的时间
/*amqp.basicproperties properties = new amqp.basicproperties().
builder().expiration("10000").build();*/
for (int i = 1; i < 11; i++) {
string message = "info" + i;
channel.basicpublish(normal_exchange,"zhangsan",null,message.getbytes());
}
}
}
(3)结果展示
发表评论