Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use rabbitmq to realize current limit and concurrency in springboot

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report