Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

Compare JMS queues and topics, and explain durable and shared subscriptions in Jakarta Messaging. How does Spring's @JmsListener consume them?

A JMS queue gives each message to exactly one consumer, while a topic gives each message to every current subscriber. A durable subscription keeps a topic's messages for a subscriber while it is offline, and a shared subscription (new in JMS 2.0) lets several consumers split the messages of one subscription. In Spring, @JmsListener runs on a DefaultMessageListenerContainer that listens to queues by default and to topics when the container factory is set to pub-sub mode.

Background to this question

Queues and topics are the two basic shapes of JMS messaging, and choosing the wrong one gives bugs that are hard to see: a notification that only one instance receives, or an order that every instance processes. Durable and shared subscriptions are the details that decide whether a topic loses messages during a deployment and whether it can scale. In this answer, we will recap the two destination types, explain durable and shared subscriptions with a small example, see how Spring's @JmsListener consumes each of them, and note what changed with the move to Jakarta Messaging.

1A Quick Recap of Queues and Topics

JMS, now called Jakarta Messaging, is the standard Java API for talking to a message broker such as ActiveMQ Artemis or IBM MQ. It defines two kinds of destination. A queue is point-to-point: many consumers may listen, but each message is given to exactly one of them. That makes a queue a natural work queue, where adding consumers spreads the load.

A topic is publish-subscribe: each message is given to every subscriber that exists when the message arrives. A price update published to a topic reaches the pricing cache, the audit service and the dashboard. But there is a catch: a plain subscription only exists while its consumer is connected, so a subscriber that is down for a deployment simply misses what was published meanwhile.

Queue, topic and subscription types
KindWho gets a messageWhile offline
Queueone consumer out of all listeningkept in the queue
Topic, non-durableevery connected subscriberlost for that subscriber
Topic, durableevery subscription, by namekept for the subscription
Topic, shared durableone consumer per subscription namekept, consumers share the work
A shared durable subscription behaves like a queue that is fed by a topic.

2Durable Subscriptions: Not Missing Messages

A durable subscription is a subscription the broker remembers by name. Once it is created, the broker stores every message published to the topic for that subscription, even while no consumer is attached, and delivers the backlog when a consumer with the same name reconnects. In the classic, unshared form, the subscription is identified by the connection's client id together with the subscription name, and only one consumer may be attached at a time.

Java
// Jakarta Messaging 3, plain API
try (JMSContext ctx = cf.createContext()) {
ctx.setClientID("audit-service"); // needed for an unshared durable subscription
Topic prices = ctx.createTopic("prices");
JMSConsumer c = ctx.createDurableConsumer(prices, "price-audit");
String body = c.receiveBody(String.class, 1000); // "EURUSD 1.0842", published while we were down
}

Notice that the subscription outlives the consumer. If we stop using it, we must remove it with unsubscribe("price-audit"); otherwise the broker keeps storing messages for a subscriber that will never come back, and its storage grows.

3Shared Subscriptions: Scaling a Topic Subscriber

Before JMS 2.0, a durable subscription could only have one active consumer, so a slow subscriber could not be scaled out. JMS 2.0 added shared subscriptions: several consumers, even on different connections and machines, attach to the same subscription name, and each message of that subscription goes to only one of them. Shared subscriptions can be durable or non-durable, and a shared durable subscription does not need a client id.

Java
JMSConsumer c1 = ctx1.createSharedDurableConsumer(prices, "price-audit"); // instance 1
JMSConsumer c2 = ctx2.createSharedDurableConsumer(prices, "price-audit"); // instance 2
// publish 4 messages to "prices"
// c1 receives 2, c2 receives 2 (the split depends on the broker)
// a separate subscription "price-cache" still receives all 4

So each service gets one subscription name, every instance of the service shares it, and every service still sees every message. This is the same shape as consumer groups in Kafka.

4How @JmsListener Consumes Them

In Spring, @JmsListener marks a method that should be called for each message. Spring creates a listener container for it, by default a DefaultMessageListenerContainer, which runs a loop of receive calls on its own threads, converts the message and calls our method. The container is built from a container factory, and the factory decides whether the destination name means a queue or a topic. By default it means a queue.

Java
@JmsListener(destination = "orders") // queue: one instance gets each order
void onOrder(OrderPlaced order) { ... }
@Bean
DefaultJmsListenerContainerFactory topicFactory(ConnectionFactory cf,
DefaultJmsListenerContainerFactoryConfigurer configurer) {
var factory = new DefaultJmsListenerContainerFactory();
configurer.configure(factory, cf); // Spring Boot defaults first
factory.setPubSubDomain(true); // destination names are topics
factory.setSubscriptionDurable(true);
factory.setSubscriptionShared(true); // JMS 2.0 shared durable subscription
return factory;
}
@JmsListener(destination = "prices", containerFactory = "topicFactory",
subscription = "price-audit") // the subscription name
void onPrice(PriceUpdate update) { ... }

If every listener in an application uses topics, setting spring.jms.pub-sub-domain=true switches Spring Boot's default factory to topics. Most applications mix both, so a second factory for topics, as above, is clearer. A common bug is a listener that silently consumes from a queue named prices instead of the topic, because the default factory was used: the broker creates the queue on demand and nothing fails.

5Jakarta Messaging and Spring Boot 3

Spring Boot 3 moved from Java EE to Jakarta EE, so the JMS API lives in the jakarta.jms package instead of javax.jms. The broker's client library must implement Jakarta Messaging: for example the ActiveMQ Artemis client works with Boot's Artemis starter, while the old ActiveMQ Classic 5.x clients needed a Jakarta build. When we upgrade, every import changes from javax.jms to jakarta.jms, and a client jar that still uses javax.jms will not even compile against the new API.

6Follow-up questions

What is the difference between a non-durable shared subscription and a queue?

Both give each message to one consumer out of several. The difference is the source and lifetime: a shared non-durable subscription receives messages from a topic and exists only while at least one consumer is attached, so messages published while all instances are down are lost. A queue exists on its own and keeps its messages until someone consumes them.

Why does an unshared durable subscription need a client id?

Because the broker identifies it by client id plus subscription name, so a reconnecting consumer can be matched to its stored messages. The client id must be unique per connection, which also means two instances cannot attach to the same unshared durable subscription. Shared durable subscriptions remove that limit and do not require a client id.

How many threads does a @JmsListener use?

The DefaultMessageListenerContainer starts the number of consumers set by concurrency, for example concurrency = "3-10" on the annotation or the factory, scaling between the two numbers. For a queue that gives parallel processing. For an unshared topic subscription only one consumer makes sense, which is another reason to use shared subscriptions.

7Conclusion

In this answer, we saw that a queue delivers each message to one consumer and a topic to every subscriber. Durable subscriptions keep a topic's messages for a named subscriber while it is offline, and JMS 2.0 shared subscriptions let several consumers split one subscription's messages. In Spring, @JmsListener runs on a DefaultMessageListenerContainer that uses queues unless the factory is in pub-sub mode, where we can also make the subscription durable and shared. With Spring Boot 3, all of this uses the jakarta.jms API.

What to say in the interviewAbout 90 seconds aloud

Open with

A JMS queue is point-to-point: several consumers can listen, but each message goes to exactly one of them, so it suits work distribution. A topic is publish-subscribe: every subscriber that exists when a message is published gets a copy.

Then, if they want more

The catch is that a normal topic subscription only exists while the consumer is connected, so a service that is down during a deployment misses messages. A durable subscription fixes that: the broker remembers the subscription by name and stores messages for it while it's offline. Classic durable subscriptions need a client id and allow only one active consumer.

JMS 2.0 added shared subscriptions, where several consumers attach to the same subscription name and each message goes to one of them, so I can scale a topic subscriber the way I'd scale a Kafka consumer group: one subscription per service, shared by its instances. In Spring, @JmsListener is backed by a DefaultMessageListenerContainer.

By default the destination is treated as a queue. For topics I either set spring.jms.pub-sub-domain to true, or better, define a second container factory with pubSubDomain, subscriptionDurable and subscriptionShared set, and give the listener a subscription name.

One thing to remember with Spring Boot 3 is that everything moved to the jakarta.jms namespace, so the broker client has to support Jakarta Messaging.

About this answer

Last updated
12 November 2026
Versions it names
Spring Boot 3
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
RabbitMQ documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Compare JMS queues and topics, and explain durable and shared subscriptions in Jakarta...": https://scrivio-interview-library.netlify.app/spring-boot/rabbitmq-and-jms/35d53affc7/