Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what happens when message published on queue with no Subscriber?

this was asked to me in an interview.

what happens when message published on queue with no Subscriber at 10 AM? and a subscriber with proper filter subscribes at 10.02 AM to same Queue. Does the message gets delivered when a subscriber subscribes after the message has reached to the broker (I mean does it store in the memory until it finds the subscriber)? what is default behavior? also is it different in JMS, STOMP and AMQP standerds?

like image 684
Madara Avatar asked Mar 09 '23 22:03

Madara


2 Answers

In AMQP brokers, messages are either deliver to consumers that are subscribing to queues, or when consumers fetch/pull messages from queues on demand. Messages will stay in the queue be default even if no subscriber is active at the moment. A message will be stored on disk if it's a persistent message and in memory if not persistent. https://www.rabbitmq.com/tutorials/amqp-concepts.html

STOMP: The RabbitMQ STOMP adapter supports a number of different destination types. Messages sent when no subscriber exists will be queued until a subscriber connects to the queue (while a topic will drop messages when there are no connected subscribers). https://www.rabbitmq.com/stomp.html

JMS: JMS is an API, it doesn't use any protocol. AMQP on other hand is a protocol between a messaging client and messaging server. A JMS client can use AMQP as the protocol to communicate with the messaging server. (check this article for more information about the subject https://spring.io/understanding/AMQP)

However, messages sent to a queue remain in the queue until the message consumer for that queue consumes them. http://docs.oracle.com/javaee/6/tutorial/doc/bnceh.html and https://en.wikipedia.org/wiki/Java_Message_Service

like image 188
Lovisa Johansson Avatar answered Mar 21 '23 19:03

Lovisa Johansson


As the question mentions about Publisher and Subscriber, I think the question is for Publish-Subscribe messaging pattern. In Pub/Sub pattern, publications are made to a topic, not queue.

The behavior depends on messaging provider. A messaging provider may discard a publication if there are no subscribers. So if a message was published to a topic at 10AM, the publication is discarded as there are no subscribers. Now when a new subscriber comes in 10:02AM, the publication will not be delivered to the subscriber.

There is a concept of "Retain Publication" in IBM MQ. When a publication has the "Retain Publication" attribute set, IBM MQ Queue Manager will keep a copy of such publication for a topic until a new publication is made for the same topic. Assuming a publication with "Retain Publication" is made at 10AM, when a subscriber comes at 10:02AM, the subscriber will get that publication.

Hope this helps.

like image 25
Shashi Avatar answered Mar 21 '23 19:03

Shashi