一 并发
步骤:
1 在rabbitListener中配置concurency=“min-max”
如下代表最小并发数是5
@Component
public class pricon {
@RabbitListener(queues ="textQueue",concurrency = "5-10")
public void hand(String str){
System.out.println(Thread.currentThread().getName()+"接受到了一个消息:"+str+"现在时间:"+System.currentTimeMillis()/1000);
}
}
2测试
@Component
public class priConsumer {
private static final String EXCHANGE = "textExchange";
public static final String QUEUE = "textQueue";
private static final String ROUTING_KEY = "textQueue";
@Autowired
RabbitTemplate template;
public void test(){
for(int i=50;i>1;i--){
int finalI = i;
template.convertAndSend(EXCHANGE,ROUTING_KEY,"queue:"+i);
}
}
}
3在rabbitmq的控制面板中会显示:
二 限流
1 配置Bean
setPrefetchCount 表示单位时间最多能处理多少消息
@Autowired
CachingConnectionFactory connectionFactory;
@Bean(name = "mqlistenerContainer")
public SimpleRabbitListenerContainerFactory simpleRabbitListenerContainerFactory(){
SimpleRabbitListenerContainerFactory factory=new SimpleRabbitListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
factory.setPrefetchCount(10);
return factory;
}
2在rabbitListener中配置工厂
@RabbitListener(queues ="textQueue",concurrency = "5-10",containerFactory = "mqlistenerContainer")
public void hand(String str){
System.out.println(Thread.currentThread().getName()+"接受到了一个消息:"+str+"现在时间:"+System.currentTimeMillis()/1000);
}
在控制面板中会显示:
3结果: