Send.java package com.xzp.rabbitmq.routing; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.xzp.rabbitmq.util.ConnectionUtil; /** * Routing * 交换机指定采用-》路由(模式) * 该消息先发送到交换机, 再由交换机发送到指定路由key已绑定队列之中, 所指定路由key都会接收消息 * */ public class Send { //交换机名称 private final static String EXCHANGE_NAME = "exchange_direct"; public static void main(String[] args) throws Exception { //获取链接 Connection connection = ConnectionUtil.getConnection(); //创建通道 Channel channel = connection.createChannel(); //声明并绑定交换机 //交换机类型 // fanout: 代表 "订阅模式" 《订阅就接收到消息》 // direct: 代表 "路由模式" 《可选择性接收消息, 指定路由Key》 // topic: 代表 "通配符模式"《可选择性接收消息, 采用通配符模式 * 符号代表1个, # 符号代表一个或多个》 //headers: 代表 "" channel.exchangeDeclare(EXCHANGE_NAME, "direct"); // 消息内容 String message = "Hello World!"; channel.basicPublish(EXCHANGE_NAME, "wh.public.news", null, message.getBytes()); System.out.println("Send '" + message + "'"); channel.close(); connection.close(); } }
Recv1.java package com.xzp.rabbitmq.routing; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.QueueingConsumer; import com.xzp.rabbitmq.util.ConnectionUtil; /** * Routing * 消息接收者1 * 交换机指定采用-》路由(模式) * 该消息先发送到交换机, 再由交换机发送到指定路由key已绑定队列之中, 所指定路由key都会接收消息 * */ public class Recv1 { //交换机名称 private final static String EXCHANGE_NAME = "exchange_direct"; //队列1名称 private final static String QUEUE_NAME = "exchange_direct_queue_1"; public static void main(String[] argv) throws Exception { // 获取到连接以及mq通道 Connection connection = ConnectionUtil.getConnection(); Channel channel = connection.createChannel(); // 声明队列 channel.queueDeclare(QUEUE_NAME, false, false, false, null); // 绑定队列到交换机 channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "wh.public.news"); // 同一时刻服务器只会发一条消息给消费者 channel.basicQos(1); // 定义队列的消费者 QueueingConsumer consumer = new QueueingConsumer(channel); // 监听队列,手动返回完成 channel.basicConsume(QUEUE_NAME, false, consumer); // 获取消息 while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String message = new String(delivery.getBody()); System.out.println("Recv1 received '" + message + "'"); Thread.sleep(10); channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); } } }
Recv2.java
package com.xzp.rabbitmq.routing; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.QueueingConsumer; import com.xzp.rabbitmq.util.ConnectionUtil; /** * Routing * 消息接收者2 * 交换机指定采用-》路由(模式) * 该消息先发送到交换机, 再由交换机发送到指定路由key已绑定队列之中, 所指定路由key都会接收消息 * */ public class Recv2 { //交换机名称 private final static String EXCHANGE_NAME = "exchange_direct"; //队列2名称 private final static String QUEUE_NAME = "exchange_direct_queue_2"; public static void main(String[] argv) throws Exception { // 获取到连接以及mq通道 Connection connection = ConnectionUtil.getConnection(); Channel channel = connection.createChannel(); // 声明队列 channel.queueDeclare(QUEUE_NAME, false, false, false, null); // 绑定队列到交换机 channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "wh.public"); // 同一时刻服务器只会发一条消息给消费者 channel.basicQos(1); // 定义队列的消费者 QueueingConsumer consumer = new QueueingConsumer(channel); // 监听队列,手动返回完成 channel.basicConsume(QUEUE_NAME, false, consumer); // 获取消息 while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String message = new String(delivery.getBody()); System.out.println("Recv2 received '" + message + "'"); Thread.sleep(10); channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); } } }
ConnectionUtil.java package com.xzp.rabbitmq.util; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; /** * Rabbit获取链接工具类 */ public class ConnectionUtil { public static Connection getConnection() throws Exception { //定义连接工厂 ConnectionFactory factory = new ConnectionFactory(); //设置服务地址 factory.setHost("localhost"); //端口 factory.setPort(5672); //设置账号信息,用户名、密码、vhost factory.setVirtualHost("/baseup"); factory.setUsername("baseup"); factory.setPassword("wukong@123"); // 通过工程获取连接 Connection connection = factory.newConnection(); return connection; } }