0
# Spring Boot AMQP Starter
1
2
Spring Boot starter that provides AMQP messaging capabilities with RabbitMQ integration, including auto-configuration for connection factories, message templates, annotation-driven listeners, and comprehensive configuration options.
3
4
## Package Information
5
6
- **Package Name**: spring-boot-starter-amqp
7
- **Package Type**: maven
8
- **Language**: Java
9
- **Installation**: Add to Maven `<dependencies>` or Gradle dependencies
10
11
```xml
12
<dependency>
13
<groupId>org.springframework.boot</groupId>
14
<artifactId>spring-boot-starter-amqp</artifactId>
15
</dependency>
16
```
17
18
```gradle
19
implementation 'org.springframework.boot:spring-boot-starter-amqp'
20
```
21
22
## Core Imports
23
24
```java { .api }
25
import org.springframework.amqp.rabbit.annotation.RabbitListener;
26
import org.springframework.amqp.rabbit.core.RabbitTemplate;
27
import org.springframework.amqp.rabbit.core.RabbitAdmin;
28
import org.springframework.amqp.core.Queue;
29
import org.springframework.amqp.core.Exchange;
30
import org.springframework.amqp.core.Binding;
31
import org.springframework.boot.autoconfigure.amqp.RabbitProperties;
32
import org.springframework.boot.autoconfigure.amqp.ConnectionFactoryCustomizer;
33
import org.springframework.boot.autoconfigure.amqp.RabbitRetryTemplateCustomizer;
34
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
35
import org.springframework.amqp.rabbit.config.DirectRabbitListenerContainerFactory;
36
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
37
import org.springframework.rabbit.stream.config.StreamRabbitListenerContainerFactory;
38
import org.springframework.rabbit.stream.producer.RabbitStreamTemplate;
39
import org.springframework.rabbit.stream.support.StreamAdmin;
40
```
41
42
## Basic Usage
43
44
### Simple Message Publishing and Consuming
45
46
```java
47
@Component
48
public class MessageReceiver {
49
50
@RabbitListener(queues = "hello")
51
public void receive(String message) {
52
System.out.println("Received: " + message);
53
}
54
}
55
56
@Service
57
public class MessageSender {
58
59
@Autowired
60
private RabbitTemplate rabbitTemplate;
61
62
public void send(String message) {
63
rabbitTemplate.convertAndSend("hello", message);
64
}
65
}
66
```
67
68
### Queue and Exchange Declaration
69
70
```java
71
@Configuration
72
public class RabbitConfig {
73
74
@Bean
75
public Queue hello() {
76
return new Queue("hello");
77
}
78
79
@Bean
80
public TopicExchange exchange() {
81
return new TopicExchange("topic-exchange");
82
}
83
84
@Bean
85
public Binding binding(Queue queue, TopicExchange exchange) {
86
return BindingBuilder.bind(queue).to(exchange).with("routing.key");
87
}
88
}
89
```
90
91
### Stream Processing (High Throughput)
92
93
```java
94
@Configuration
95
@EnableRabbitStreaming
96
public class StreamConfig {
97
98
@Bean
99
public RabbitStreamTemplate streamTemplate(Environment env) {
100
return new RabbitStreamTemplate(env, "my-stream");
101
}
102
}
103
104
@Service
105
public class StreamService {
106
107
@Autowired
108
private RabbitStreamTemplate streamTemplate;
109
110
public void publishToStream(String message) {
111
streamTemplate.convertAndSend(message);
112
}
113
114
@RabbitStreamListener(id = "stream-listener", queues = "my-stream")
115
public void handleStreamMessage(String message) {
116
System.out.println("Stream message: " + message);
117
}
118
}
119
```
120
121
## Architecture
122
123
The Spring Boot AMQP starter provides several key components that work together:
124
125
- **Auto-Configuration**: Automatically configures RabbitMQ connections, templates, and listeners via `RabbitAutoConfiguration`, `RabbitAnnotationDrivenConfiguration`, and `RabbitStreamConfiguration`
126
- **Connection Management**: Provides caching connection factories with connection pooling and customization through `ConnectionFactoryCustomizer`
127
- **Message Templates**: RabbitTemplate and RabbitMessagingTemplate for message operations, with `RabbitTemplateConfigurer` for customization
128
- **Listener Containers**: Support for Simple, Direct, and Stream message listener containers with dedicated factory configurers
129
- **Stream Support**: Native RabbitMQ Streams integration for high-throughput scenarios with `RabbitStreamTemplate` and stream-specific listeners
130
- **Configuration Properties**: Comprehensive configuration through `spring.rabbitmq.*` properties via `RabbitProperties`
131
- **Admin Operations**: RabbitAdmin for queue, exchange, and binding management
132
- **Retry and Error Handling**: Built-in retry mechanisms with `RabbitRetryTemplateCustomizer` and comprehensive error handling strategies
133
134
## Capabilities
135
136
### Message Template Operations
137
138
Core messaging operations using RabbitTemplate and RabbitMessagingTemplate for publishing and consuming messages.
139
140
```java { .api }
141
@Service
142
public class MessagingService {
143
144
@Autowired
145
private RabbitTemplate rabbitTemplate;
146
147
public void sendMessage(String routingKey, Object message) {
148
rabbitTemplate.convertAndSend(routingKey, message);
149
}
150
151
public <T> T receiveMessage(String queueName, Class<T> type) {
152
return rabbitTemplate.receiveAndConvert(queueName, type);
153
}
154
}
155
```
156
157
[Message Operations](./message-operations.md)
158
159
### Annotation-Driven Listeners
160
161
Configuration and usage of @RabbitListener annotation for creating message consumers with various listener container types.
162
163
```java { .api }
164
@Component
165
public class MessageListeners {
166
167
@RabbitListener(queues = "user.registration")
168
public void handleUserRegistration(UserRegistrationEvent event) {
169
// Process registration event
170
}
171
172
@RabbitListener(bindings = @QueueBinding(
173
value = @Queue(value = "order.processed"),
174
exchange = @Exchange(value = "orders", type = ExchangeTypes.TOPIC),
175
key = "order.processed.*"
176
))
177
public void handleOrderProcessed(OrderProcessedEvent event) {
178
// Process order event
179
}
180
}
181
```
182
183
[Listener Configuration](./listener-configuration.md)
184
185
### Connection and Connection Factory Configuration
186
187
Auto-configuration and customization of RabbitMQ connection factories, including connection pooling, SSL, and clustering support.
188
189
```java { .api }
190
@Configuration
191
public class ConnectionConfig {
192
193
@Bean
194
public ConnectionFactoryCustomizer connectionFactoryCustomizer() {
195
return (connectionFactory) -> {
196
connectionFactory.setRequestedHeartbeat(60);
197
connectionFactory.setConnectionTimeout(30000);
198
};
199
}
200
201
@Bean
202
public CachingConnectionFactory connectionFactory() {
203
CachingConnectionFactory factory = new CachingConnectionFactory();
204
factory.setHost("localhost");
205
factory.setPort(5672);
206
return factory;
207
}
208
}
209
```
210
211
[Connection Configuration](./connection-configuration.md)
212
213
### Queue, Exchange, and Binding Management
214
215
Administrative operations for managing RabbitMQ topology using RabbitAdmin and declarative configuration.
216
217
```java { .api }
218
@Configuration
219
public class TopologyConfig {
220
221
@Bean
222
public Queue userQueue() {
223
return QueueBuilder.durable("user.events").build();
224
}
225
226
@Bean
227
public DirectExchange userExchange() {
228
return ExchangeBuilder.directExchange("user.exchange").build();
229
}
230
231
@Bean
232
public Binding userBinding(Queue userQueue, DirectExchange userExchange) {
233
return BindingBuilder.bind(userQueue).to(userExchange).with("user.created");
234
}
235
}
236
```
237
238
[Queue and Exchange Management](./queue-exchange-management.md)
239
240
### Configuration Properties
241
242
Comprehensive configuration options available through application properties for all aspects of AMQP messaging.
243
244
```yaml { .api }
245
spring:
246
rabbitmq:
247
host: localhost
248
port: 5672
249
username: guest
250
password: guest
251
virtual-host: /
252
connection-timeout: 60000ms
253
publisher-confirm-type: correlated
254
publisher-returns: true
255
listener:
256
simple:
257
concurrency: 1
258
max-concurrency: 3
259
prefetch: 1
260
template:
261
retry:
262
enabled: true
263
initial-interval: 2s
264
max-attempts: 3
265
```
266
267
[Configuration Properties](./configuration-properties.md)
268
269
### Error Handling and Retry Mechanisms
270
271
Configuration of retry policies, error handlers, and dead letter queue processing for robust message handling.
272
273
```java { .api }
274
@Configuration
275
public class ErrorHandlingConfig {
276
277
@Bean
278
public RabbitRetryTemplateCustomizer retryCustomizer() {
279
return (Target target, RetryTemplate template) -> {
280
template.setRetryPolicy(new SimpleRetryPolicy(3));
281
template.setBackOffPolicy(new ExponentialBackOffPolicy());
282
};
283
}
284
285
@Bean
286
public MessageRecoverer messageRecoverer() {
287
return new RejectAndDontRequeueRecoverer();
288
}
289
}
290
```
291
292
[Error Handling](./error-handling.md)
293
294
### Stream Processing Support
295
296
Configuration and usage of RabbitMQ Streams for high-throughput message processing scenarios.
297
298
```java { .api }
299
@Configuration
300
@EnableRabbitStreaming
301
public class StreamConfig {
302
303
@Bean
304
public RabbitStreamTemplate streamTemplate(Environment env) {
305
return new RabbitStreamTemplate(env, "my-stream");
306
}
307
308
@RabbitStreamListener(id = "stream-listener", queues = "my-stream")
309
public void handleStreamMessage(String message) {
310
// Process stream message
311
}
312
}
313
```
314
315
[Stream Processing](./stream-processing.md)
316
317
## Auto-Configuration Classes
318
319
The Spring Boot AMQP starter provides comprehensive auto-configuration through several key classes:
320
321
```java { .api }
322
// Main AMQP auto-configuration
323
@Configuration(proxyBeanMethods = false)
324
@ConditionalOnClass({ RabbitTemplate.class, Channel.class })
325
@EnableConfigurationProperties(RabbitProperties.class)
326
public class RabbitAutoConfiguration {
327
// Configures CachingConnectionFactory, RabbitTemplate, RabbitMessagingTemplate, and AmqpAdmin
328
}
329
330
// Annotation-driven listener configuration
331
@Configuration(proxyBeanMethods = false)
332
@ConditionalOnClass(EnableRabbit.class)
333
public class RabbitAnnotationDrivenConfiguration {
334
// Configures SimpleRabbitListenerContainerFactory and DirectRabbitListenerContainerFactory
335
// Enables @RabbitListener annotation processing
336
}
337
338
// Stream support configuration
339
@Configuration(proxyBeanMethods = false)
340
@ConditionalOnClass(StreamRabbitListenerContainerFactory.class)
341
public class RabbitStreamConfiguration {
342
// Configures StreamRabbitListenerContainerFactory, Environment, and RabbitStreamTemplate
343
}
344
```
345
346
These auto-configuration classes work together to provide zero-configuration setup while allowing full customization through properties and component beans.