package com.sf.deadletter; import com.rabbitmq.client.Channel; import com.sf.util.MqUtils; import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class DeadLetter { public static void main(String[] args) throws Exception { Channel channel = MqUtils.getChannel(); channel.exchangeDeclare("exchange.dlx", "direct", true, false, null); channel.queueDeclare("queue.dlx", true, false, false, null); channel.queueBind("queue.dlx", "exchange.dlx", "dlx-routing-key"); Map param = new HashMap(); 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()); } } }