How to use rabbitmq to realize current limit and concurrency in springboot
It is believed that many inexperienced people are at a loss about how to use rabbitmq to achieve current limitation and concurrency in springboot. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Concurrence
Steps:
1. Configure concurency= "min-max" in rabbitListener
The following means that the minimum number of concurrency is 5
@ Component
Public class pricon {
@ RabbitListener (queues = "textQueue", concurrency = "5-10")
Public void hand (String str) {
System.out.println (Thread.currentThread (). GetName () + "received a message:" + str+ "now time:" + System.currentTimeMillis () / 1000)
}
}
2. Testing
@ 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 iTune50 witch I > 1p imuri -) {
Int finalI = I
Template.convertAndSend (EXCHANGE,ROUTING_KEY, "queue:" + I)
}
}
}
3. The control panel of rabbitmq will display:
Second current limit
1. Configure Bean
SetPrefetchCount indicates the maximum number of messages that can be processed per unit time
@ Autowired
CachingConnectionFactory connectionFactory
@ Bean (name = "mqlistenerContainer")
Public SimpleRabbitListenerContainerFactory simpleRabbitListenerContainerFactory () {
SimpleRabbitListenerContainerFactory factory=new SimpleRabbitListenerContainerFactory ()
Factory.setConnectionFactory (connectionFactory)
Factory.setPrefetchCount (10)
Return factory
}
2. Configure the factory in rabbitListener
@ RabbitListener (queues = "textQueue", concurrency = "5-10", containerFactory = "mqlistenerContainer")
Public void hand (String str) {
System.out.println (Thread.currentThread (). GetName () + "received a message:" + str+ "now time:" + System.currentTimeMillis () / 1000)
}
It is displayed in the dashboard:
3 results:
After reading the above, have you mastered how to use rabbitmq to achieve current limiting and concurrency in springboot? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!