SpringAMQP 的WorkQueue队列
工作队列,用来提高消息处理速度,避免队列消息堆积,一条通知由多个consumer监听
发送消息给队列,测试work工作模式
@Test
public void testSend2WorkQueue(){
// 队列名
String queueName = "simple.queue";
// 消息
String message = "Hello,world and SpringAMQP!!!——————";
// 发送50条消息给队列
for (int i = 1; i <= 50; i++) {
rabbitTemplate.convertAndSend(queueName,message+i);
Thread.sleep(20);
}
}
指定俩个队列
// 指定队列
@RabbitListener(queues = "simple.queue")
public void getWork1(String msg) throws InterruptedException {
System.out.println("1==监听到的消息:--->"+msg);
Thread.sleep(20);
}
@RabbitListener(queues = "simple.queue")
public void getWork2(String msg) throws InterruptedException {
System.err.println("2==监听到的消息:--->"+msg);
Thread.sleep(200);
}
结果是两个队列监听的数据平均分,这不符合我们需要的要求,我们要求的是能力强的服务可以多处理,能力低的少处理
原因是这个队列默认有个预取数据,默认就把数据平分了,取到的消息要一一处理完才行,
修改方法改yaml配置文件
spring:
rabbitmq:
host: 192.168.88.135
port: 5672
username: itcast
password: 123321
virtual-host: /
listener:
simple:
prefetch: 1 # 修改的预取消息控制,能先拿到的队列就优先处理