SpringAMQP工作队列
work queue 设计时多个消费者
实现一个队列绑定多个消费者
基本思路:
1.在publisher服务中定义测试方法,每秒产生50条信息,发送到simple.queue
@Test public void testSendMessage2WokrQueue() throws InterruptedException { String queueName = "simple.queue"; String message = "rule world"; for (int i = 0; i <= 50 ; i++) { rabbitTemplate.convertAndSend(queueName, message + i); Thread.sleep(20); } }
2.在consumer服务中定义两个消息监听者,都监听一个队列
@Component public class SpringrRabbitListener { @RabbitListener(queues = "simple.queue") public void listenSimpleQueue1(String msg) throws InterruptedException { System.out.println("1接收到simple.queue:【"+msg+"】" + LocalTime.now()); Thread.sleep(20); } @RabbitListener(queues = "simple.queue") public void listenSimpleQueue2(String msg) throws InterruptedException { System.err.println("2接收到simple.queue:【"+msg+"】" + LocalTime.now()); Thread.sleep(200); } }
3.消费者每秒处理50和10个消息
消费预取限制
修改yml,设置preFetch这个值,可以控制预取消息上限:
spring: rabbitmq: host: 192.168.240.135 port: 5672 username: itcast password: 123321 virtual-host: / listener: simple: prefetch: 1

浙公网安备 33010602011771号