Outlining the difference between a Topic and a Queue in JMS, and the advantages of each.
When working in integration, one of the most fundamental concepts to understand is the difference between topics and queues when using a messaging API, such as JMS.
What do each of them do? And when would you choose to use a topic over a queue?
First let’s make sure you’re up to speed with messaging. There are usually three parties involved in messaging:
- Producer – this is the component that wants to send a message.
- Consumer – this is the component that receives a message.
- Message Broker – this is the bit of software in the middle (such as ActiveMQ) which handles the messages.
Of the two types of destinations, queues are the most commonly seen.
Queues are used in a scenario when one or more producers wants to send a message to a single consumer.
With a queue, messages arrive on the queue in order. As a consumer, you pick up messages from the queue, one after another.
Queues are very powerful because if the consumer goes away, or disconnects from the queue for some reason, the broker won’t throw an error. Instead, the broker will safely keep every message sent to the queue. Then, when the consumer connects again, it will receive all of the messages while it was away.
This kind of integration of consumer and producer is sometimes called point-to-point integration, because a message is delivered from one place to another.
Topics are seen less often, but they are very useful.
Topics are used when one or more producers want to broadcast out a message to a bunch of consumers (known as subscribers).
The clever thing about topics is that a producer doesn’t need to worry about sending a message to every subscriber, or indeed who all the subscribers are. Instead, all of that is handled by the message broker.
Consumers can come to the message broker and say they’d like to subscribe to a topic. The broker knows who all the subscribers are, so when a message is published to the topic, it makes sure all the subscribers get a copy.
Topics are even more clever, because a consumer can choose whether or not they want the message broker to keep messages for them while they’re away. If a consumer registers itself as a durable subscriber with the broker, then any messages sent to the topic while the subscriber is away, will be sent to the consumer when they come back online.
Otherwise, if a consumer registers as a non-durable subscriber, they won’t receive messages that were sent to the topic while they were away.
When would you use a topic?
Topics come into their own when we have a message that we want to broadcast, but we don’t know who the consumers are. You might think that sounds quite unlikely, but actually has a lot of potential uses.
For example, you might have a topic that messages are published to, whenever a new member of staff joins your organisation. Then, you could have various applications that subscribe to that topic, such as your payroll application and your ID card application. These applications will each be notified about the new member of staff and can process his/her payroll, staff card, and so on.
The advantage of a topic is that you only need to publish the message once, and the broker takes care of distributing it to all the interested subscribers.
The key differences
I’m going to wrap up this post with a list of the key differences between queues and topics:
- Point-to-point style of integration
- A message is received by exactly one consumer
- Publish-subscribe style of integration
- A message is distributed to all subscribers of the topic
I hope you found this article useful. If you have anything to contribute I’d love to see your feedback in the comments below!
Leave a Comment
You can use Markdown in your comment. To write code, indent lines by 4 spaces.