The @JmsListener annotation marks a method as a message-driven consumer: Spring creates a listener container behind the scenes that receives messages from a JMS queue or topic and calls your method for each one. It replaces the manual MessageListener implementations and container setup that older JMS code required.
In Spring Boot 3 and later, JMS uses the jakarta.jms namespace, and Boot auto-configures the connection factory and listener container factory for ActiveMQ Classic, Artemis or a JNDI provider. You usually only need the broker starter and the annotation.
When Spring finds a bean method annotated with @JmsListener, it registers a JmsListenerEndpoint and builds a MessageListenerContainer from a JmsListenerContainerFactory. The container owns the threads, the connection, the session, message acknowledgement and transactions. Your method only handles the payload.
Key attributes:
| Attribute | Purpose |
|---|---|
destination | Queue or topic name to consume from |
containerFactory | Name of the container factory bean to use |
concurrency | Number of consumer threads, such as 3 or 2-5 |
selector | JMS message selector, for example type = 'order' |
subscription | Durable subscription name for topics |
errorHandler | Bean name of a JmsListenerErrorHandler |
Add a JMS starter, for example spring-boot-starter-artemis or spring-boot-starter-activemq. Spring Boot enables JMS listener detection automatically, so @EnableJms is only needed when you do not use auto-configuration.
Configure the broker connection in application.properties:
Method parameters can be a String, a jakarta.jms.Message, a Spring Message<T>, or a POJO if you configure a MessageConverter. Headers can be injected with @Header.
Spring Boot detects a single MessageConverter bean and applies it to the default container factory.
Queues deliver each message to one consumer. Topics deliver each message to every subscriber, so you need a container factory in publish-subscribe mode.
For a durable subscription, also set factory.setSubscriptionDurable(true) and a subscription name plus client ID. To consume from several queues, declare one @JmsListener method per queue, since each annotation takes one destination.
Set concurrency = "2-5" to let the container scale between two and five consumer threads. Remember that more threads means no ordering guarantee across messages.
Use a JmsListenerErrorHandler for failures that happen inside your method. The interface used in some older examples (org.springframework.util.ErrorHandler) is not what the errorHandler attribute expects.
If the handler swallows the exception, the message counts as consumed. Rethrow it to trigger redelivery, and configure a dead-letter queue on the broker so poison messages do not loop forever.
Usually not. Boot's JMS auto-configuration enables listener detection when a JMS ConnectionFactory is present. Add @EnableJms only in non-Boot Spring applications or when auto-configuration is disabled.
Return a value from the method and annotate it with @SendTo("reply-queue"), or rely on the incoming JMSReplyTo header. Spring converts and sends the return value as the reply.
Set spring.jms.listener.session.transacted or provide a JmsTransactionManager on the container factory. A transacted session rolls back and redelivers the message when your method throws.
@JmsListener consumes from a JMS broker with queue and topic semantics. @KafkaListener consumes from partitioned Kafka topics using offsets. Both are annotation-driven but target different brokers.
@JmsListener turns a plain method into a JMS consumer with almost no code. Use a MessageConverter for typed payloads, a pub-sub container factory for topics, concurrency for throughput, and a JmsListenerErrorHandler that rethrows so the broker can redeliver failed messages.