Send.java
package com.xzp.rabbitmq.ps; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.xzp.rabbitmq.util.ConnectionUtil; /** * Publish/Subscribe * 交换机指定采用-》发布/订阅(模式) * 该消息先发送到交换机, 再由交换机发送到已绑定队列之中, 所绑定交换机队列都会接收到该消息。 * */ public class Send { //交换机名称 private final static String EXCHANGE_NAME = "exchange_ps"; 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, "fanout"); // 消息内容 String message = "Hello World!"; channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes()); System.out.println("Send '" + message + "'"); channel.close(); connection.close(); } }
Recv1.java
package com.xzp.rabbitmq.ps; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.QueueingConsumer; import com.xzp.rabbitmq.util.ConnectionUtil; /** * Publish/Subscribe * 发布/订阅模式 * 该消息先发送到交换机, 再由交换机发送到已绑定队列之中, 所绑定交换机队列都会接收到该消息。 * 接收消息1 */ public class Recv1 { //交换机名称 private final static String EXCHANGE_NAME = "exchange_ps"; //队列1名称 private final static String QUEUE_NAME = "ps_queue_1"; public static void main(String[] args) throws Exception { //获取链接 Connection connection = ConnectionUtil.getConnection(); //声明及创建通道 Channel channel = connection.createChannel(); //声明队列 channel.queueDeclare(QUEUE_NAME, false, false, false, null); //绑定交换机 channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, ""); //同一时刻服务器只会发一条消息给消费者 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.ps; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.QueueingConsumer; import com.xzp.rabbitmq.util.ConnectionUtil; /** * Publish/Subscribe * 发布/订阅模式 * 该消息先发送到交换机, 再由交换机发送到已绑定队列之中, 所绑定交换机队列都会接收到该消息。 * 接收消息2 */ public class Recv2 { //交换机名称 private final static String EXCHANGE_NAME = "exchange_ps"; //队列1名称 private final static String QUEUE_NAME = "ps_queue_2"; public static void main(String[] args) throws Exception { //获取链接 Connection connection = ConnectionUtil.getConnection(); //声明及创建通道 Channel channel = connection.createChannel(); //声明队列 channel.queueDeclare(QUEUE_NAME, false, false, false, null); //绑定交换机 channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, ""); //同一时刻服务器只会发一条消息给消费者 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; } }