JMS defines two messaging models: point-to-point, where a message goes through a queue to exactly one consumer, and publish-subscribe, where a message is sent to a topic and every subscriber receives a copy. Since JMS 1.1 both are used through the same unified API, and JMS 2.0 added shared subscriptions that let several consumers split a topic's messages.
The specification is now called Jakarta Messaging. Version 3.0 and later use the jakarta.jms package, while JMS 2.0 and earlier use javax.jms. The two models behave the same in both.
A producer sends a message to a queue. The provider keeps it until one consumer receives and acknowledges it, then removes it. If several consumers listen on the same queue, each message still reaches only one of them, so a queue naturally balances work across consumers.
Key properties:
Typical uses are job queues, order processing and any task that must run exactly once.
JMSContext is the simplified API introduced in JMS 2.0. It combines the older Connection and Session objects and is closed with try-with-resources.
A publisher sends a message to a topic. The provider delivers a copy to every active subscription. The publisher does not know who the subscribers are, which decouples the two sides.
Key properties:
Typical uses are price updates, notifications, cache invalidation events and audit feeds.
JMS 2.0 reworked topic subscriptions into four kinds:
| Subscription | Created with | Stored while offline | Several consumers share messages |
|---|---|---|---|
| Non-durable | createConsumer(topic) | No | No |
| Durable | createDurableConsumer(topic, name) | Yes | No |
| Shared non-durable | createSharedConsumer(topic, name) | No | Yes |
| Shared durable | createSharedDurableConsumer(topic, name) | Yes | Yes |
A shared subscription gives topics queue-like load balancing: consumers that use the same subscription name split the messages instead of each receiving all of them. Before JMS 2.0 you could not scale a topic subscriber across several instances without receiving duplicates, so this removed a major limitation.
| Aspect | Point-to-point | Publish-subscribe |
|---|---|---|
| Destination | Queue | Topic |
| Receivers per message | One | Every subscription |
| Storage with no consumer | Kept until consumed | Dropped unless a durable subscription exists |
| Coupling | Sender targets a work channel | Publisher is unaware of subscribers |
| Common use | Tasks, commands | Events, broadcasts |
Both models support the same delivery modes (persistent or non-persistent), priorities, time-to-live, message selectors and transactions.
Choose a queue when a message is a command that must be handled once, and a topic when a message is an event that several independent parts of a system care about. Many systems use both: a topic announces that an order was placed, and each interested service forwards it to its own queue for reliable processing.
For reliability:
Session.CLIENT_ACKNOWLEDGE so a message is only removed after your code finishes its work.A queue delivers each message to one consumer. A topic delivers each message to all of its subscriptions.
Yes, with shared subscriptions added in JMS 2.0. Consumers that share a subscription name divide the messages between them.
It is a topic subscription that the provider keeps while the subscriber is disconnected, so messages sent in the meantime are delivered when it reconnects.
Yes. Jakarta Messaging is part of Jakarta EE and brokers such as ActiveMQ Artemis and IBM MQ implement it. Spring offers JmsTemplate and @JmsListener on top of the same API.
JMS has two messaging models: point-to-point queues for one-consumer-per-message delivery and publish-subscribe topics for broadcast. Durable and shared subscriptions let topics survive disconnects and scale across consumers. Pick queues for commands, topics for events, and add acknowledgements and idempotent consumers for reliability.