RabbitMQ死信交换/队列
我不太明白这种死信交换/排队的情况。网上文件说:RabbitMQ死信交换/队列,rabbitmq,dead-letter,Rabbitmq,Dead Letter,我不太明白这种死信交换/排队的情况。网上文件说: republished to another exchange when any of the following events occur: The message is rejected (basic.reject or basic.nack) with requeue=false, The TTL for the message expires; or The queue length limit is exceed
republished to another exchange when any of the following events occur:
The message is rejected (basic.reject or basic.nack) with requeue=false,
The TTL for the message expires; or
The queue length limit is exceeded.
这是否意味着当这些事件发生时,消息将自动移动到死信队列?或者我必须在我的代码中明确地将那些“死”消息移动到该DLQ
另外,如何为我的正常队列设置DLX/DLQ?假设当我的正常队列中的消息失败/过期时,它会移动到DLX/DLQ?以下是一个完整的示例:
public class DXtest {
public static void main(String[] argv)
throws IOException,
InterruptedException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.exchangeDeclare("my.exchange", "fanout");
Map<String, Object> args = new HashMap<String, Object>();
args.put("x-dead-letter-exchange", "my.exchange");
args.put("x-max-length", 2);
channel.queueDeclare("myqueue", false, false, false, args); // here you setup your queue,
//for example with x-max-length, when the limit is reach, the message head message queue will be redirect
//to the my.exchange and then to my-dead-letter-queue
channel.queueDeclare("my-dead-letter-queue", false, false, false, null);
channel.queueBind("my-dead-letter-queue","my.exchange","");
Consumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body)
throws IOException {
String message = new String(body, "UTF-8");
System.out.println(" [x] Message '" + message + "'" + new Date());
}
};
channel.basicConsume("my-dead-letter-queue", true, consumer);
}
}
公共类测试{
公共静态void main(字符串[]argv)
抛出一个异常,
中断异常{
ConnectionFactory工厂=新的ConnectionFactory();
setHost(“localhost”);
Connection Connection=factory.newConnection();
Channel=connection.createChannel();
channel.exchangedecare(“my.exchange”、“fanout”);
Map args=new HashMap();
args.put(“x-dead-letter-exchange”、“my.exchange”);
参数放置(“x最大长度”,2);
channel.queueDeclare(“myqueue”,false,false,false,args);//在这里设置队列,
//例如,对于x-max-length,当达到限制时,消息头消息队列将被重定向
//到我的交易所,然后到我的死信队列
queueDeclare(“我的死信队列”,false,false,false,null);
channel.queueBind(“我的死信队列”、“我的.exchange”和“”);
消费者=新的默认消费者(频道){
@凌驾
public void handleDelivery(字符串consumerTag、信封信封、AMQP.BasicProperties属性、字节[]正文)
抛出IOException{
字符串消息=新字符串(正文,“UTF-8”);
System.out.println(“[x]Message'”+Message+“'”+新日期());
}
};
channel.basicConsume(“我的死信队列”,true,consumer);
}
}