Send.java
package com.xzp.rabbitmq.work; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.xzp.rabbitmq.util.ConnectionUtil; /** * "Work Queues" * 工作模式 * 消息发送者 - R - 发送消息(生产者) * */ public class Send { private final static String QUEUE_NAME = "work_queue"; 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); //生成100个消息 for (int i = 1; i <= 100; i++) { // 消息内容 String message = "第 " + i + " 次消息"; channel.basicPublish("", QUEUE_NAME, null, message.getBytes()); System.out.println("Send '" + message + "'"); Thread.sleep(i * 10); } channel.close(); connection.close(); } }
Recv1.java
package com.xzp.rabbitmq.work; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.QueueingConsumer; import com.xzp.rabbitmq.util.ConnectionUtil; /** * "Work Queues" * 工作模式 * 消息接收者1 - R - 接收消息(接收者) * */ public class Recv1 { private final static String QUEUE_NAME = "work_queue"; 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.basicQos(1); //定义队列的消费者 QueueingConsumer consumer = new QueueingConsumer(channel); //监听队列,手动返回完成 //这里false表示"手动模式", 需反馈,拿走并标记后才认为消费成功 channel.basicConsume(QUEUE_NAME, false, consumer); //累计消息次数 int count = 0; // 获取消息 while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String message = new String(delivery.getBody()); count++; System.out.println("Recv1 received '" + message + "'"); System.out.println("累计接收: " + count); //休眠0.01秒 Thread.sleep(10); //标记返回 //如未标记返回,又为"手动模式", 无法再次获取消息 channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); } } }
Recv2.java
package com.xzp.rabbitmq.work; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.QueueingConsumer; import com.xzp.rabbitmq.util.ConnectionUtil; /** * "Work Queues" * 工作模式 * 消息接收者2 - R - 接收消息(接收者) * */ public class Recv2 { private final static String QUEUE_NAME = "work_queue"; 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.basicQos(1); // 定义队列的消费者 QueueingConsumer consumer = new QueueingConsumer(channel); // 监听队列,手动返回完成状态 //这里false表示"手动模式", 需反馈,拿走并标记后才认为消费成功 channel.basicConsume(QUEUE_NAME, false, consumer); //累计消息次数 int count = 0; // 获取消息 while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String message = new String(delivery.getBody()); count++; System.out.println("Recv2 received '" + message + "'"); System.out.println("累计接收: " + count); // 休眠1秒 Thread.sleep(1000); //标记返回 //如未标记返回,又为"手动模式", 无法再次获取消息 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; } }