RabbitMQ系列之死信队列

代码片段

channel.exchangeDeclare("exchange.dlx","direct",true);
channel.exchangeDeclare("exchange.normal","fanout",true);
Map map = new HashMap();
map.put("x-message-ttl",10000);
map.put("x-dead-letter-exchange","exchange.dlx");
map.put("x-dead-letter-routing-key",ROUTING_KEY);
channel.queueDeclare("queue.normal",durable,exclusive,autoDelete,map);
channel.queueBind("queue.normal","exchange.normal","testR");

channel.queueDeclare("queue.dlx",durable,exclusive,autoDelete,null);
channel.queueBind("queue.dlx","exchange.dlx",ROUTING_KEY);

String message="测试消息死信队列";
channel.basicPublish("exchange.normal","rk",MessageProperties.PERSISTENT_TEXT_PLAIN,message.getBytes());

运行结果:

RabbitMQ系列之死信队列_第1张图片

运行流程:

首先生产者携带路由键rk的消息,然后经过交换机exchange.normal  顺利存储到队列queue.normal中,由于队列设置了过期时间,10秒之内没有消费,消息变成过期消息,由于设定了该消息为dlx消息因此消息丢给了交换机exchange.dlx,这是交换机找到了与之绑定的队里,存储起来,即为死信队列。

你可能感兴趣的:(后端)