Get the App
SLTechnology News&Howtos  ›  Servers  › 

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

Shulou Source: shulou.com Published: 2022-06-01 18:38:38 10月01日 Update

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!

Tags: Message configuration content method more present tense problem panel control minimum helpless to this end representative unit reason to this factory skill time step Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Shulou Technology MariaDB Shulou Tech Info Redmi