muasif80
muasif80

Reputation: 6016

A message sent to JMS Queue will be consumed by only a single consumer?

A case where senders are sending messages to a Queue, for example message1 is sent by sender1 to a queue. Now a consumer named consumer1 connects to queue and reads the message message1.

There is another consumer named consumer2. But the message message1 is already consumed by consumer1 so it will not be available for consumer2.

When a next message arrives in queue, consumer2 might receive that message if it reads the queue before consumer1.

Does it mean that it all is a case whether one consumer reads the queue before the other in order to get the first message available from the queue?

Upvotes: 0

Views: 2187

Answers (1)

Tim Bish
Tim Bish

Reputation: 18376

This is the nature of a Queue in JMS, messages are sent to one consumer and once ack'd they are gone, the next consumer can get the next message and so on. This is often referred to as competing consumers or load balancing. The consumers can share the work as jobs or work items are enqueued which allows for higher throughput when the work associated with the items in the Queue can take significant time.

There are options depending on the messaging broker to make a consumer exclusive such that only that consumer can read messages from the queue while the other consumers sit and wait for the exclusive consumer to leave which makes them backups of a sort.

Other options are to use something like Apache Camel to route a given message to more than one queue, or to use AcitveMQ Virtual Topics to send messages to a Topic and have that message then enqueue onto specific consumer Queues.

The solution depends on the broker you are using and the problem you are trying to solve, none of which you've really made clear in the question.

Upvotes: 1

Related Questions