|
@@ -1,32 +1,36 @@
|
|
package com.sf.deadletter;
|
|
package com.sf.deadletter;
|
|
|
|
|
|
import com.rabbitmq.client.Channel;
|
|
import com.rabbitmq.client.Channel;
|
|
|
|
+import com.rabbitmq.client.MessageProperties;
|
|
import com.sf.util.MqUtils;
|
|
import com.sf.util.MqUtils;
|
|
|
|
|
|
|
|
+
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
-import java.util.Map;
|
|
|
|
-import java.util.Scanner;
|
|
|
|
|
|
|
|
public class DeadLetter {
|
|
public class DeadLetter {
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
public static void main(String[] args) throws Exception {
|
|
Channel channel = MqUtils.getChannel();
|
|
Channel channel = MqUtils.getChannel();
|
|
|
|
+ // 定义 dlx
|
|
channel.exchangeDeclare("exchange.dlx", "direct", true, false, null);
|
|
channel.exchangeDeclare("exchange.dlx", "direct", true, false, null);
|
|
|
|
+ // 定义 dlx queue
|
|
channel.queueDeclare("queue.dlx", true, false, false, null);
|
|
channel.queueDeclare("queue.dlx", true, false, false, null);
|
|
channel.queueBind("queue.dlx", "exchange.dlx", "dlx-routing-key");
|
|
channel.queueBind("queue.dlx", "exchange.dlx", "dlx-routing-key");
|
|
|
|
|
|
- Map<String, Object> param = new HashMap<String, Object>();
|
|
|
|
- param.put("x-dead-letter-exchange", "exchange.dlx");
|
|
|
|
- param.put("x-dead-letter-routing-key", "dlx-routing-key");
|
|
|
|
- channel.exchangeDeclare("exchange.normal2", "direct");
|
|
|
|
- channel.queueDeclare("queue.normal2", false, false, false, param);
|
|
|
|
- channel.queueBind("queue.normal2", "exchange.normal2", "zhangsan");
|
|
|
|
- Scanner scanner = new Scanner(System.in);
|
|
|
|
- System.out.println("请输入消息:");
|
|
|
|
- while (scanner.hasNext()) {
|
|
|
|
- String message = scanner.next();
|
|
|
|
- System.out.println(message);
|
|
|
|
- channel.basicPublish("exchange.normal2", "dlx-routing-key", null, message.getBytes());
|
|
|
|
- }
|
|
|
|
|
|
+ // 定义正常的交换器
|
|
|
|
+ channel.exchangeDeclare("exchange.normal", "fanout", true, false, null);
|
|
|
|
+ HashMap<String, Object> arguments = new HashMap<>();
|
|
|
|
+ // 定义队列时,通过该属性给该队列设置 DLX
|
|
|
|
+ arguments.put("x-dead-letter-exchange", "exchange.dlx");
|
|
|
|
+ // 还可以通过该属性重新设置消息的路由键,否则使用原消息的路由键
|
|
|
|
+ arguments.put("x-dead-letter-routing-key", "dlx-routing-key");
|
|
|
|
+ // 设置该队列的 ttl
|
|
|
|
+ arguments.put("x-message-ttl", 10000);
|
|
|
|
+ channel.queueDeclare("queue.normal", true, false, false, arguments);
|
|
|
|
+ channel.queueBind("queue.normal", "exchange.normal", "routing");
|
|
|
|
+
|
|
|
|
+ String message = "hello world111";
|
|
|
|
+ channel.basicPublish("exchange.normal", "key", MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes());
|
|
|
|
+
|
|
}
|
|
}
|
|
}
|
|
}
|