How to Dynamically Adjust RabbitMQ Listener Concurrency in Spring Boot
This tutorial explains how to configure, test, and dynamically modify the concurrency of RabbitMQ listeners in a Spring Boot application, covering dependency setup, YAML configuration, code examples, runtime adjustments, and best‑practice recommendations for optimal message processing.
Environment: SpringBoot 2.7.16 + RabbitMQ 3.8.35
1. Introduction
RabbitMQ is an open‑source message broker. In Spring Boot projects we usually listen to messages with @RabbitListener . Adjusting the listener’s concurrency can improve processing capacity.
Depending on the business scenario we may need to increase concurrency when messages backlog, or decrease it when the system is under heavy load. This article demonstrates how to adjust the concurrency of a running listener.
2. Practical Example
Dependency
<code><dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
</code>2.1 Configuration
<code>spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
virtualHost: test
publisherConfirmType: correlated
publisherReturns: true
listener:
simple:
acknowledgeMode: manual
concurrency: 2
max-concurrency: 2
</code>2.3 Create Exchange and Queue
Exchange: test.exchange (type: topic)
Queue: test
Binding key: akf.#
2.4 Prepare Messages
Insert 100 messages into the queue via a REST endpoint.
<code>@Resource
private RabbitTemplate rabbitTemplate;
@GetMapping("/send")
public String send() {
new Thread(() -> {
for (int i = 0; i < 100; i++) {
rabbitTemplate.convertAndSend("test.exchange", "akf.a", "message - " + i);
}
}).start();
return "success";
}
</code>2.5 Listener
<code>@RabbitListener(queues = "test")
public void listener1(String message) {
System.out.printf("%s - Received: %s%n", Thread.currentThread().getName(), message);
try { TimeUnit.SECONDS.sleep(2); } catch (InterruptedException e) {}
}
</code>2.7 Adjust Concurrency
Initially only one thread appears because the default prefetch is 250 and only 100 messages exist. Change prefetch to 5:
<code>spring:
rabbitmq:
listener:
simple:
prefetch: 5
</code>Now two threads alternate. To modify concurrency at runtime, give the listener a unique id and use RabbitListenerEndpointRegistry .
<code>@RabbitListener(id = "test-queue", queues = "test", ackMode = "AUTO")
public void listener1(String message) { /* ... */ }
@Resource
private RabbitListenerEndpointRegistry registry;
@GetMapping("/modify/{count}")
public Object modify(@PathVariable("count") Integer count) {
MessageListenerContainer container = registry.getListenerContainer("test-queue");
if (container instanceof SimpleMessageListenerContainer c) {
c.setConcurrentConsumers(count);
}
return String.format("Concurrent consumers: %d%n", count);
}
</code>Testing shows two threads initially, then three after calling /modify/3 , and the system rejects values beyond spring.rabbitmq.listener.simple.max-concurrency .
Listeners can also be paused and resumed via start/stop methods.
Conclusion
Dynamically adjusting RabbitMQ listener concurrency in Spring Boot is an effective optimization. Properly tuning concurrency based on load improves throughput, saves resources, and maintains system stability.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.