JMS has no "topic exchange". That term comes from AMQP and RabbitMQ. In JMS you publish to a topic and every active subscriber receives a copy. In Spring Boot you get topic behaviour by switching the JMS destination domain to publish/subscribe, either globally with spring.jms.pub-sub-domain=true or per listener and per template with a dedicated container factory.
This article shows both approaches, durable and shared subscriptions, and a working producer and consumer on Spring Boot 3.x with ActiveMQ or Artemis.
A queue delivers each message to exactly one consumer. A topic delivers each message to every subscriber that is active when it is published, unless the subscription is durable. Spring Boot picks the destination type from a single flag on JmsTemplate and on the listener container.
| Feature | Queue | Topic |
|---|---|---|
| Delivery | One consumer per message | Every subscriber gets a copy |
| Default in Spring Boot | Yes | No, needs pub-sub-domain |
| Offline consumers | Message waits in the queue | Missed unless the subscription is durable |
| Typical use | Work distribution | Events, notifications |
Use the starter for your broker. This example uses ActiveMQ Classic, and spring-boot-starter-artemis works the same way with spring.artemis.* properties.
Do not commit real credentials. Read them from environment variables or a secret store.
If every destination in your application is a topic, one property is enough:
Spring Boot applies it to the auto-configured JmsTemplate and to the default listener container factory. A plain @JmsListener(destination = "notifications") is then a topic subscriber, and jmsTemplate.convertAndSend("notifications", ...) publishes to a topic.
The catch is that the flag is global. If you also need queues, use the per-destination setup below.
Define a listener container factory and a template that are explicitly publish/subscribe. Spring Boot's configurer applies the properties and your overrides win.
The default JmsTemplate and the default factory keep working with queues. Spring Boot backs off its own JmsTemplate only if you define a bean of that type, so name yours and inject it by name or qualifier, and check that the queue code still receives the template you expect.
The producer sends to a topic through the topic template:
Two subscribers, which each receive every message:
A normal topic subscription only receives messages sent while it is connected. If the consumer restarts, it misses messages. A durable subscription makes the broker store messages for it. It needs a client ID and a subscription name, and a durable subscriber must run with a concurrency of 1.
If you run several instances of the same service, a durable subscription with one client ID causes a conflict, because only one connection can use that ID. Use a shared subscription instead (JMS 2.0), set with factory.setSubscriptionShared(true). Instances that share a subscription name split the messages between them, which gives you topic fan-out between services and load balancing inside each service. Check that your broker and client library support JMS 2.0 shared subscriptions.
setPubSubDomain(true).No. A JMS topic fans out to all subscribers by name. A RabbitMQ topic exchange routes messages by pattern to bound queues. ActiveMQ supports wildcards in topic names, such as orders.*, which covers part of that use.
Yes. Keep the default queue template and add a separate template and listener factory with setPubSubDomain(true) for topics.
No. The broker creates a topic when the first producer or subscriber uses it, unless your broker is configured to forbid auto-creation.
Remove subscriptions you no longer use and set message expiry. A durable subscriber that never reconnects makes the broker keep every message.
JMS topics replace the idea of a topic exchange. Enable spring.jms.pub-sub-domain if everything is a topic, or use a dedicated JmsTemplate and listener factory with setPubSubDomain(true) when queues and topics coexist. Add durable or shared subscriptions when consumers must not miss messages or must scale out.