RabbitTemplate is the central class of Spring AMQP for talking to a RabbitMQ broker. It implements the AmqpTemplate interface and gives you one thread-safe object that converts Java objects into messages, publishes them to an exchange with a routing key, and can also receive messages and do request/reply calls.
You use it where a plain RabbitMQ client would force you to open connections, create channels, serialize payloads and close everything again. Spring manages the connection and channel handling for you, so a publish call is usually a single line.
RabbitTemplate is a helper in the same family as JdbcTemplate and JmsTemplate. It hides the low-level AMQP client code behind a small API:
ConnectionFactory, publishes the message and returns the channel to the cache.Message with a MessageConverter (and back on receive).convertSendAndReceive.AmqpException hierarchy.Consuming is normally done with @RabbitListener, not with the template. The template is mainly the sending side, plus occasional polling receives.
Without Spring Boot you build the connection factory and the template yourself:
CachingConnectionFactory shares one connection and caches channels, which is why a single RabbitTemplate bean can be used from many threads. With Spring Boot you skip this class and set spring.rabbitmq.* properties instead, as described below.
The most used method is convertAndSend. It converts the object, then publishes it to the given exchange with the given routing key:
The overloads work like this:
| Method | Purpose |
|---|---|
convertAndSend(routingKey, obj) | Uses the template's default exchange |
convertAndSend(exchange, routingKey, obj) | Explicit exchange and routing key |
convertAndSend(exchange, routingKey, obj, postProcessor) | Adds headers or properties before sending |
send(exchange, routingKey, message) | Sends a ready-made Message, no conversion |
To set a header or a persistent delivery mode on one message, pass a MessagePostProcessor:
receiveAndConvert(queue, timeout) polls a queue once and returns the converted object, or null on timeout. It is useful in scripts and tests, but for continuous consumption use @RabbitListener.
For RPC style calls, convertSendAndReceive publishes a message and blocks until the consumer replies, or until the reply timeout (5 seconds by default) expires and the call returns null:
The listener on the other side returns a value from its @RabbitListener method, and Spring sends it back to the reply queue. By default the template uses RabbitMQ direct reply-to, so you do not declare a reply queue yourself.
The default converter, SimpleMessageConverter, handles String, byte[] and Serializable objects. Java serialization is fragile and a security risk, so for real applications use a JSON converter:
Spring Boot detects a single MessageConverter bean and applies it to both RabbitTemplate and listener containers. Newer Spring AMQP lines move to Jackson 3 and rename this class to JacksonJsonMessageConverter, so check the version you depend on.
By default a publish call returns before the broker has accepted the message. Two features close that gap:
spring.rabbitmq.publisher-confirm-type=correlated, then register a ConfirmCallback on the template.spring.rabbitmq.publisher-returns=true and spring.rabbitmq.template.mandatory=true, then register a ReturnsCallback.Pass a CorrelationData object to convertAndSend so the confirm can be matched to the message you sent.
Yes. It is designed to be shared as a singleton, and channels come from the connection factory's cache. Avoid changing its settings after it starts being used.
AmqpTemplate is the broker-neutral interface, and RabbitTemplate is its RabbitMQ implementation with extras such as confirms, returns and execute(ChannelCallback). Inject RabbitTemplate when you need those extras.
No consumer replied within the reply timeout. Check that the listener returns a value, that the routing key matches a bound queue, and raise replyTimeout if the work is slow.
Normally not. Use @RabbitListener for push-based consumption and keep receiveAndConvert for rare polling cases.
RabbitTemplate is the Spring AMQP class you inject to publish messages to RabbitMQ, receive them on demand and run request/reply calls. Configure a JSON MessageConverter, enable confirms and returns when you cannot lose messages, and use @RabbitListener for the consuming side.