RabbitTemplate is the central class of Spring AMQP for talking to RabbitMQ from Java. It wraps the low-level AMQP client so you can publish messages with one method call, convert Java objects to message bodies through a MessageConverter, receive messages synchronously, and run request-reply (RPC) calls. In a Spring Boot application it is auto-configured for you and is thread-safe, so a single instance can be injected anywhere.
For consuming messages in production code you normally use @RabbitListener instead of polling with the template. The template is mainly the producer side of your messaging code.
Without Spring, sending a message means opening a connection, creating a channel, declaring or checking the exchange, building AMQP.BasicProperties, serializing the payload, publishing, and then closing resources correctly. RabbitTemplate handles all of that:
ConnectionFactory (Spring Boot configures a CachingConnectionFactory) and returns it afterwards.contentType through a MessageConverter.AmqpException hierarchy.It implements the RabbitOperations / AmqpTemplate interfaces, so you can depend on AmqpTemplate in code that should not know about RabbitMQ-specific features.
Add the spring-boot-starter-amqp dependency and point Boot at the broker:
Boot then creates a RabbitTemplate bean. If your context contains exactly one MessageConverter bean, Boot applies it to the template and to listener containers automatically. The following configuration declares the topology and a JSON converter:
The RabbitAdmin that Boot creates declares these queues, exchanges and bindings when the first connection opens. Jackson2JsonMessageConverter is the Jackson 2 converter; newer Spring AMQP releases built on Jackson 3 add a renamed equivalent, so check the class names for the version you use.
The most common method is convertAndSend. It converts the object, builds the message and publishes it:
Constructor injection is enough; no @Autowired is needed with a single constructor. With the JSON converter the payload becomes {"orderId":42,"customerEmail":"[email protected]","totalCents":1999} and the contentType header is application/json.
You can modify the message just before it is sent with a MessagePostProcessor, for example to set a TTL or a custom header:
If you register no converter, RabbitTemplate uses SimpleMessageConverter. It handles String, byte[] and Serializable objects. Java serialization is a security risk and ties both sides to the same class, so use a JSON converter for anything that crosses service boundaries.
| Converter | Payloads | Typical use |
|---|---|---|
SimpleMessageConverter | String, byte[], Serializable | Default; avoid for objects |
Jackson2JsonMessageConverter | Any Jackson-serializable type | JSON between services |
MessagingMessageConverter | Spring Message objects | When using the messaging abstraction |
On the consumer side, a @RabbitListener method with the same converter configured receives the typed object:
When producer and consumer use different class names or packages, restrict and map types explicitly instead of trusting type headers from the wire.
convertAndSend returns as soon as the message is handed to the client, not when the broker has stored it. For important messages, enable publisher confirms and returns:
Then register callbacks on the template:
A confirm tells you the broker accepted the message. A returned message tells you the broker could not route it to any queue (only when mandatory is true). Set both callbacks with setConfirmCallback and setReturnsCallback on the template if you build it yourself. Combine these with durable queues and persistent messages, which is the default for convertAndSend.
The template can also pull messages, which suits tests and scripts:
For RPC, convertSendAndReceive publishes a message and blocks until a reply arrives or the reply timeout (5 seconds by default) expires, returning null on timeout. Use it sparingly, because it ties up a thread and couples the services in time.
Yes. It can be shared across threads. Channels are taken from the cached connection factory per operation. If you change settings such as callbacks, do it once at startup.
Use RabbitTemplate to publish and @RabbitListener to consume. The listener container gives you concurrency, prefetch, acknowledgements and retry that manual receive calls do not.
The consumer is probably using SimpleMessageConverter. Register the same JSON MessageConverter bean on both sides and make sure the contentType header is application/json.
Enable publisher confirms (correlated) and handle the ConfirmCallback. Without confirms, a successful convertAndSend call does not guarantee delivery.
RabbitTemplate is the Spring AMQP class for publishing and, when needed, receiving RabbitMQ messages. Spring Boot configures it from spring.rabbitmq.* properties, applies a single MessageConverter bean for JSON, and manages channels for you. Use convertAndSend for fire-and-forget publishing, turn on confirms and returns for messages you cannot lose, prefer @RabbitListener for consuming, and avoid the default Java serialization across services.