@JmsListener turns a method into a durable topic subscriber only when three things line up: the container factory uses the publish/subscribe domain, it sets subscriptionDurable to true together with a unique client ID, and the annotation's subscription attribute names the subscription. With that in place, the broker stores messages published while your application is offline and delivers them when the listener reconnects. The annotation alone, without the factory settings, does not make a subscription durable.
This article shows the full Spring Boot 3 configuration, how the broker identifies a durable subscription, how to filter messages with selectors, and the pitfalls that cause lost or duplicated messages.
A JMS topic delivers each message to every active subscriber. A non-durable subscriber only sees messages published while it is connected. A durable subscriber is registered with the broker under a stable identity, so the broker keeps its unread messages until it reconnects or the subscription is removed.
The broker identifies a durable subscription by two values: the connection clientId and the subscription name. If either changes between restarts, the broker treats it as a new subscription and the messages stored for the old one are never delivered. Queues do not need this: a queue already keeps messages until a consumer takes them.
Spring Boot auto-configures a listener container factory, but it does not make subscriptions durable for you. Define your own factory and refer to it from the annotation. This example uses Spring Boot 3, where the JMS API lives in the jakarta.jms package:
setPubSubDomain(true) switches the container from queues to topics. setClientId must be stable across restarts and unique per running instance, because the broker allows only one active connection per client ID.
With the factory in place, the annotation names the subscription:
The pair of clientId (order-service-1) and subscription (order-service-orders) is the identity the broker stores. Start the application, stop it, publish a few messages to order.events, then start it again, and the listener receives the messages it missed.
The selector attribute takes an SQL-92 style expression evaluated against message headers and properties:
Selectors cannot read the message body, so the publisher must set the properties you filter on. Changing a selector for an existing subscription name makes most brokers recreate the subscription, which discards messages stored under the old one.
Durability also depends on the publisher and the broker:
JmsTemplate defaults to persistent delivery, but a custom delivery mode of non-persistent loses messages on a broker restart.sessionTransacted set on the factory, an exception rolls back the receive and the broker redelivers.For a durable subscription with several consumers sharing the work, use a shared subscription (setSubscriptionShared(true), JMS 2.0), which lets multiple instances use the same subscription name. A plain durable subscription allows only one active consumer.
A durable subscription keeps accumulating messages even if its application is gone. Remove unused subscriptions in the broker console, or with Session.unsubscribe(name), otherwise storage grows without limit.
Common problems:
containerFactory, so the default factory (queue domain, non-durable) is used.No. The container factory must also have subscriptionDurable set to true, topic mode enabled, and a client ID. The attribute only supplies the subscription name.
No. Durable subscriptions apply to topics. A queue already stores messages until a consumer receives them.
Only with a shared durable subscription from JMS 2.0, enabled with setSubscriptionShared(true). A regular durable subscription supports a single active consumer.
The broker stores them for the subscription and delivers them once the same client ID and subscription name reconnect, provided the messages were persistent and did not expire.
A durable @JmsListener needs a topic-based container factory with subscriptionDurable enabled, a stable client ID, and a subscription name in the annotation. The broker keys stored messages on that pair, so keep both constant across restarts. Send persistent messages, make handlers idempotent, use selectors only on headers and properties, and clean up subscriptions you no longer need.