JmsTemplate is Spring's central helper class for the Java Message Service. It opens connections and sessions, creates producers and consumers, converts Java objects to messages and back, translates JMS exceptions into Spring's unchecked hierarchy, and closes everything afterwards. In Spring Boot you inject it, send with convertAndSend(), and use receiveAndConvert() only when you need to pull a message synchronously.
Spring Boot auto-configures a JmsTemplate when a JMS ConnectionFactory is on the classpath, for example through spring-boot-starter-activemq or the Artemis starter. This article covers setup, sending, receiving, message conversion, delivery settings and when to prefer @JmsListener or JmsClient.
Plain JMS code needs a connection, a session, a producer, try/finally cleanup and checked JMSException handling for every operation. JmsTemplate wraps that boilerplate in a template method. You pass the destination and the payload, and it handles resource acquisition and release.
Boot 3 uses the jakarta.jms namespace, so any code that imports javax.jms must be migrated. RabbitMQ is not a JMS broker by default, so a JmsTemplate talks to JMS providers such as ActiveMQ Classic, ActiveMQ Artemis, IBM MQ or Amazon MQ, not to RabbitMQ unless you use a JMS client library for it.
Add a broker starter and set properties in application.properties:
Boot builds the JmsTemplate from the spring.jms.template.* properties. Setting spring.jms.pub-sub-domain=true makes it, and the listener containers, use topics instead of queues. Time-to-live and delivery mode only take effect when explicit QoS is enabled, which Boot does automatically if you set any of those properties.
Inject the template and call convertAndSend:
The lambda is a MessagePostProcessor. Use it to add properties, which message selectors can then filter on. For raw control over the JMS message, use send(destination, session -> session.createTextMessage(text)).
By default JmsTemplate uses SimpleMessageConverter. It maps String to TextMessage, byte[] to BytesMessage, Map to MapMessage and Serializable objects to ObjectMessage. Avoid ObjectMessage in new code, because Java deserialization is a security risk and ties both sides to the same class.
For JSON, register a MessageConverter bean. Boot applies a single MessageConverter bean to the auto-configured template and listener containers:
The _type property tells the receiver which class to build. Map it to short names with setTypeIdMappings when producers and consumers do not share class names.
JmsTemplate receives synchronously and blocks the calling thread:
receive blocks until a message arrives, or until the receive timeout passes, and then returns null. Without a timeout it can wait forever. Because every call may open a session and consumer, this style is slow for continuous consumption, unless you use a caching connection factory. Boot wraps the connection factory in a pooling or caching one by default for supported brokers.
For continuous consumption use @JmsListener, which runs a message listener container with concurrency, transactions and recovery built in:
Key settings to know:
| Setting | Effect |
|---|---|
sessionTransacted | Sends and receives commit with the surrounding transaction |
deliveryMode | Persistent survives a broker restart, non-persistent does not |
timeToLive | Messages expire after the given time |
priority | Higher priority messages can be delivered first |
receiveTimeout | Maximum blocking time for receive |
Do not create a new JmsTemplate per call. It is thread-safe once configured, so share one bean. When sending inside a database transaction, remember that JMS and the database are separate resources, so a failed commit does not unsend a message unless you use XA or an outbox pattern.
Yes, after configuration. Inject one shared instance and call it from many threads, but avoid changing its settings at runtime.
Use @JmsListener for continuous consumption. Use JmsTemplate.receive only for one-off, on-demand pulls.
It waits for a message until one arrives or the receive timeout expires. Set setReceiveTimeout to avoid waiting forever.
Not out of the box. RabbitMQ uses AMQP, so Spring AMQP's RabbitTemplate is the natural choice, unless you enable a JMS client for RabbitMQ.
JmsTemplate removes JMS boilerplate by managing connections, sessions and exceptions while you call convertAndSend and receiveAndConvert. Let Spring Boot create it from the broker starter and spring.jms.* properties, switch to JSON with a MessageConverter bean, set a receive timeout for synchronous reads, and use @JmsListener for continuous consumption.