死信队列 DLX,Dead-Letter-Exchange
- 利用DLX,当消息在一个队列中变成死信(dead message)之后,它能被重新publish到另一个Exchange,这个Exchange就是DLX
消息变成死信队列的几种情况
- 消息被拒绝(basic.reject/basic.nack)并且requeue=false
- 消息TTL过期
- 队列达到最大长度
死信队列描述
- DLX也是一个正常的Exchange,和一般的额Exchange没有区别,它能在任何的队列上被制定,实际上就是设置某个队列的属性
- 当这个队列中有死信时,RabbitMQ就会自动将这个消息重新发布到设置的Exchange上去,进而被路由到另一个队列
- 可以监听这个队列消息做相应的处理,这个特性可以弥补RabbitMQ3.0以前支持的immediate参数功能
死信队列设置
- 首先需要设置死信队列的exchange和queue,然后进行绑定
- Exchange:dlx.exchange
- Queue:dlx.queue
- RoutingKey: #
- 然后我们进行正常声明交换机、队列、绑定,只不过我们需要在队列上加一个参数即可:arguments.put("x-dead-letter-exchange", "dlx.exchange");
- 这样消息在过期、requeue、队列在达到最大长度时,消息就可以直接路由到死信队列!
代码实现
生产端
package club.yunqiang.rabbitmqapi.dlx;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
* @author 张云强
* @date 2019/12/7
*/
public class Producer {
public static void main(String[] args) throws IOException, TimeoutException {
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setPort(5672);
connectionFactory.setVirtualHost("/");
connectionFactory.setHost("localhost");
Connection connection = connectionFactory.newConnection();
Channel channel = connection.createChannel();
String exchange = "test_dlx_exchange";
String routingKey = "dlx.save";
String msg = "Hello Rabbit MQ DLX Message";
for (int i = 0; i < 1; i++) {
AMQP.BasicProperties properties = new AMQP.BasicProperties().builder()
.deliveryMode(2)
.contentEncoding("UTF-8")
.expiration("10000")
.build();
channel.basicPublish(exchange, routingKey, true, properties, msg.getBytes());
}
}
}
消费端
package club.yunqiang.rabbitmqapi.dlx;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeoutException;
/**
* @author 张云强
* @date 2019/12/7
*/
public class Consumer {
public static void main(String[] args) throws IOException, TimeoutException {
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setPort(5672);
connectionFactory.setVirtualHost("/");
connectionFactory.setHost("localhost");
Connection connection = connectionFactory.newConnection();
Channel channel = connection.createChannel();
// 声明一个普通队列
String exchangeName = "test_dlx_exchange";
String routingKey = "dlx.#";
String queueName = "test_dlx_queue";
Map<String, Object> arguments = new HashMap<>();
arguments.put("x-dead-letter-exchange", "dlx.exchange");
// 通过arguments参数给队列指定死信队列
channel.queueDeclare(queueName, true, false, false, arguments);
channel.exchangeDeclare(exchangeName, "topic", true, false, null);
channel.queueBind(queueName, exchangeName, routingKey);
// 死信队列声明
channel.exchangeDeclare("dlx.exchange", "topic", true, false, null);
channel.queueDeclare("dlx.queue", true, false, false, null);
channel.queueBind("dlx.queue", "dlx.exchange", "#");
// 接收消息
channel.basicConsume(queueName, true, new MyConsumer(channel));
}
}
消费者
package club.yunqiang.rabbitmqapi.dlx;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import java.io.IOException;
/**
* @author 张云强
* @date 2019/12/7
*/
public class MyConsumer extends DefaultConsumer {
/**
* Constructs a new instance and records its association to the passed-in channel.
*
* @param channel the channel to which this consumer is attached
*/
public MyConsumer(Channel channel) {
super(channel);
}
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("---- consume message ----");
System.err.println("consumerTag:" + consumerTag);
System.err.println("envelope:" + envelope);
System.err.println("properties:" + properties);
System.err.println("body:" + new String(body));
}
}
注意:本文归作者所有,未经作者允许,不得转载