RabbitTemplate is the central class of Spring AMQP for talking to RabbitMQ from application code. It publishes messages to exchanges, converts Java objects to and from message bodies, receives messages on demand and supports request-reply calls. Spring Boot auto-configures one for you when spring-boot-starter-amqp is on the classpath.
You use it for producing messages and for synchronous receive or RPC patterns. For continuous consumption, use @RabbitListener, which is built on the same connection and message converter infrastructure.
RabbitTemplate doesRabbitTemplate doesRabbitTemplate implements AmqpTemplate and RabbitOperations. It wraps a ConnectionFactory, opens channels as needed, applies a MessageConverter and exposes high-level methods so you do not handle channels and byte arrays yourself.
| Responsibility | Methods |
|---|---|
Send a raw Message | send(exchange, routingKey, message) |
| Convert a Java object, then send | convertAndSend(exchange, routingKey, object) |
| Receive one message | receive(queue), receiveAndConvert(queue) |
| Request and reply (RPC) | sendAndReceive, convertSendAndReceive |
| Typed request and reply | convertSendAndReceiveAsType |
| Publisher confirms and returns | setConfirmCallback, setReturnsCallback |
Add the starter, then set the connection in application.yml.
Declare the topology as beans so Spring creates the exchange, queue and binding on startup.
Spring Boot applies a single MessageConverter bean to the auto-configured RabbitTemplate and to listener containers, so objects travel as JSON. In Spring AMQP 4.0, JacksonJsonMessageConverter for Jackson 3 supersedes Jackson2JsonMessageConverter, so check which Spring Boot line you run.
convertAndSend is the method you will use most. It serialises the object with the configured converter and publishes it.
Constructor injection replaces the field @Autowired used in older examples. To control headers, priority or expiry, use a MessagePostProcessor:
For event-driven consumption, prefer a listener:
receiveAndConvert polls a queue once and returns null if it is empty. It blocks only up to the template's receiveTimeout when you use the overload with a timeout.
For RPC, convertSendAndReceive publishes a message with a reply-to address and waits for the answer. It returns null when the replyTimeout, 5 seconds by default, elapses, so always handle that case.
By default, publishing is fire and forget. To know whether the broker accepted a message:
spring.rabbitmq.publisher-confirm-type=correlated. The broker then acknowledges each publish, and a ConfirmCallback receives the result.spring.rabbitmq.publisher-returns=true and spring.rabbitmq.template.mandatory=true. Unroutable messages come back through a ReturnsCallback instead of vanishing.CorrelationData object in convertAndSend to match a confirm to your message.spring.rabbitmq.template.retry.enabled=true for connection-level failures during publishing.RabbitTemplate is thread-safe, so inject one shared instance. A template used from many threads shares its connection and creates channels per operation, or one channel per thread when you use invoke.
send and convertAndSend?send takes a ready Message with bytes and properties. convertAndSend takes any Java object and runs it through the MessageConverter first.
Define a MessageConverter bean, for example Jackson2JsonMessageConverter on Spring AMQP 3.x. Spring Boot picks it up for both the template and listeners.
RabbitTemplate or AmqpTemplate?Inject RabbitTemplate when you need RabbitMQ features such as confirms or invoke, and AmqpTemplate when you want to stay broker neutral.
convertSendAndReceive return null?No consumer replied within replyTimeout. Check that a listener exists for the routing key and that it returns a value.
RabbitTemplate is the Spring AMQP client for publishing, converting, receiving and request-reply messaging with RabbitMQ. Declare your exchanges and queues as beans, use convertAndSend with a JSON converter, consume with @RabbitListener, and turn on publisher confirms and returns for reliable delivery.