Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JMS Topic vs Queue - Intent

I am trying to understand the use case of using Queue.

My understanding: Queue means one-to-one. The only use case(if not rare, very few) would be: Message is intended for only one consume.

But even in those cases, I may want to use Topic (just to be future safe). The only extra caution would be to make subscriptions durable. Or, in special situations, I would use bridging / dispatcher mechanism.

Given above, I would always (or in most cases) want to publish to a topic. Subscriber can be either durable topic(s) or dispatched queue(s).

Please let me know what I am missing here or I am missing the original intent?

like image 322
Sandeep Jindal Avatar asked Jul 02 '13 15:07

Sandeep Jindal


People also ask

What is the difference between JMS queue and Topic?

In queue, you only have one receiver or consumer; unlike in topic where in you can have your message be disseminated to a number of subscribers. Also, in topic, the publisher has to be continuously active for a subscriber to receive the messages. Otherwise the message will be reallocated.

What's the difference between a topic and a queue?

Queues and Topics are similar when a sender sends messages, but messages are processed differently by a receiver. A queue can have only one consumer, whereas a topic can have multiple subscribers.

What is a JMS topic?

JMS topic. The term JMS topic is used to refer to the JMS destination (an instance of javax. jms. Topic) that applications interact with, and that an administrator configures as a JMS resource of the default messaging provider.

What is the difference between queue and Topic in ActiveMQ?

Both ActiveMQ queue and ActiveMQ topic are places where messages are sent. The difference between ActiveMQ Queue and Topic comes down to who receives the message.


1 Answers

The design requirements on when to use queues are simple if you think in terms of real-world examples:

  • Submit online order (exactly-once processing to avoid charging credit card twice)
  • Private peer-to-peer chat (exactly one receiver for each message)
  • Parallel task distribution (distribute tasks amongst many workers in a networked system)

...and examples for when to use topics...

  • News broadcast to multiple subscribers; notification service, stock ticker, etc.
  • Email client (unique durable subscriber; you still get emails when you're disconnected)

You said...

But even in those cases, I may want to use Topic (just to be future safe). The only extra case I would have to do is to make (each) subscription durable. Or, I special situations, I would use bridging / dispatcher mechanism.

You're over-engineering the design. It's true, you can achieve exactly-once processing using a topic and durable subscriber, but you'd be limited to a single durable subscriber; the moment you start another subscriber for that topic, you'll get duplicate processing for the same message, not to mention, a single durable subscriber is hardly a solution that scales; it would be a bottleneck in your system for sure. With a queue, you can deploy 1000 receivers on 100 nodes for the same queue, and you'd still get exactly-once processing for a single message.

You said...

Give above, I would always (or in most cases) want to publish to a topic. Subscriber can be either durable topic(s) or dispatched queue(s).

Using a dispatched queue with a topic subscriber is sort of redundant. You basically get asynchronous dispatching when using queues, so why not just use a queue?...no reason to put a topic in front of it.

like image 141
raffian Avatar answered Sep 21 '22 15:09

raffian