工作队列:一个生产者对应多个消费者,生产者直接将消息发送到rabbitmq的队列之中。
消息分配模式:轮询分配(默认)
即不管消费者处理信息的效率,队列给所有消费者轮流发送一条信息,直至消息发送完毕
假如:生产者发送了10条信息
那么
消费者A收到的信息:1,4,7,10
消费者B收到的信息:2,5,8
消费者C收到的信息:3,6,9
生产者类:
package com.example.demo.queue.work;import java.io.IOException;import java.util.concurrent.TimeoutException;import com.example.demo.utils.ConnectionUtil;import com.rabbitmq.client.Channel;import com.rabbitmq.client.Connection;public class Producer { // 队列名称 private static final String QUEUE_NAME="work_queue"; public static void main(String[] args) { Connection connection = null; Channel channel = null; try { // 获取连接 connection = ConnectionUtil.getConnection(); // 创建通道 channel = connection.createChannel(); // 声明队列 channel.queueDeclare(QUEUE_NAME, false, false, false, null); // 生产者发送的信息 String sendMsg = "msg from producer"; for(int i=0 ;i<10; i++) { sendMsg = "msg from producer :" + i; // 发送信息 channel.basicPublish("", QUEUE_NAME, null, sendMsg.getBytes()); } } catch (Exception e) { e.printStackTrace(); } finally { // 关闭通道 try { channel.close(); } catch (IOException e) { e.printStackTrace(); } catch (TimeoutException e) { e.printStackTrace(); } // 关闭连接 try { connection.close(); } catch (IOException e) { e.printStackTrace(); } } } }
消费者1:
package com.example.demo.queue.work;import java.io.IOException;import java.util.concurrent.TimeoutException;import com.example.demo.utils.ConnectionUtil;import com.rabbitmq.client.AMQP.BasicProperties;import com.rabbitmq.client.Channel;import com.rabbitmq.client.Connection;import com.rabbitmq.client.DefaultConsumer;import com.rabbitmq.client.Envelope;public class Consumer01 { // 队列名称 private static final String QUEUE_NAME="work_queue"; public static void main(String[] args) { Connection connection = null; Channel channel = null; try { // 获取连接 connection = ConnectionUtil.getConnection(); // 创建通道 channel = connection.createChannel(); // 声明队列 channel.queueDeclare(QUEUE_NAME, false, false, false, null); // 定义消费者 DefaultConsumer consumer = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body) throws IOException { String msg = new String(body,"UTF-8"); System.out.println("[1]:receive msg:"+msg); try { Thread.sleep(1000); } catch (Exception e) { e.printStackTrace(); } finally { System.out.println("[1]:deal msg successful."); } } }; // 接收信息 channel.basicConsume(QUEUE_NAME, true, consumer); } catch (Exception e) { e.printStackTrace(); } finally { // 关闭通道// try {// channel.close();// } catch (IOException e) {// e.printStackTrace();// } catch (TimeoutException e) {// e.printStackTrace();// } // 关闭连接// try {// connection.close();// } catch (IOException e) {// e.printStackTrace();// } } } }
消费者2:
package com.example.demo.queue.work;import java.io.IOException;import com.example.demo.utils.ConnectionUtil;import com.rabbitmq.client.AMQP.BasicProperties;import com.rabbitmq.client.Channel;import com.rabbitmq.client.Connection;import com.rabbitmq.client.DefaultConsumer;import com.rabbitmq.client.Envelope;public class Consumer02 { // 队列名称 private static final String QUEUE_NAME="work_queue"; public static void main(String[] args) { Connection connection = null; Channel channel = null; try { // 获取连接 connection = ConnectionUtil.getConnection(); // 创建通道 channel = connection.createChannel(); // 声明队列 channel.queueDeclare(QUEUE_NAME, false, false, false, null); // 定义消费者 DefaultConsumer consumer = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body) throws IOException { String msg = new String(body,"UTF-8"); System.out.println("[2]:receive msg:"+msg); try { Thread.sleep(2000); } catch (Exception e) { e.printStackTrace(); } finally { System.out.println("[2]:deal msg successful."); } } }; // 接收信息 channel.basicConsume(QUEUE_NAME, true, consumer); } catch (Exception e) { e.printStackTrace(); } finally { // 关闭通道// try {// channel.close();// } catch (IOException e) {// e.printStackTrace();// } catch (TimeoutException e) {// e.printStackTrace();// } // 关闭连接// try {// connection.close();// } catch (IOException e) {// e.printStackTrace();// } } } }
执行顺序:
先执行两个消费者(Consumer01,Consumer02)类的main方法,再执行生产者(Producer)的main方法即可
顺序反过来亦可,但是可能会影响到轮询分配的效果
消费者1终端:
消费者2终端: