I am new to JMS. I have started with "hello world" where I am publishing the message from java application on Topic and
listening it from client (node.js Javascript). I have gone through this wikipedia entry, but I have some questions based on my previous theoretical understanding.
As per my understanding, point-to-point is the queue implementation where there can be at most one consumer subscribed on queue and can
be consumed by that only. Neither producer nor the consumer knows about each other. Queue is hosted on message brokers in my case Apache ActiveMQ. Queue can be created by producer before publishing the message (or it can be created from console in advance).
In case of publish/subscribe model, it's almost same as point-to-point except the fact we use Topic instead of queue. In this model there can be more than more consumer on the topic. Once the message is published, all the subscribers will be notified. Now if any of the subscriber, send the acknowledgment for the published message, message will taken as consumed and it will no longer be available for new subscriber?
Point to Point means message(s) is sent from one application(producer or sender) to another application(consumer/receiver) via a queue. There can be more than one consumer listening on a queue but only one of them will be get the message. Hence it is Point to Point or One to One.
On the other hand Publish/Subscribe is another messaging model where a message(or publication as it is commonly called) is sent to multiple consumers(or subscribers) through a topic. The topic is the link between publisher and subscriber. The subscribers may or may not acknowledge the published message. Implementations like JMS acknowledge the message the messaging providers but not the sender of the message. Publications will be received by all subscribers, durable and non-durable. Any new subscribers on the same topic will not get the publication unless it is a Retained publication.
I would recommend you to read further on,
Durable subscription
Non-durable subscription
Retained publication
Related
I have two instances of my app subscribing to a topic. Since there are two instances(i.e two subscribers), two events(messages) will be generated and written to a queue. (Now i have duplicate message in the queue and one by one each is going to be processed) But I want to have a solution where only one event is processed/or only one message is written to the queue. How can i achieve that?. I must have two subscribers instead one goes down
JMS topics follow publish-subscribe semantics where every subscriber will get the message. However, JMS queues follow point-to-point semantics where only 1 of the connected consumers will receive the message. Therefore, if you want the message to be consumed by only one client then all of your consumers should be connected to a JMS queue rather than a topic.
I have a topic. I have 10 consumers subscribed for it. As per my understanding, a message will be removed from
topic when all consumers have received it. Right? Once it is removed, any further subscriber
will not be notified for that specific message. I could not confirm this in the JMS specification anywhere.
A broker (in your case Active MQ) will deliver a publication to all active subscribers, both durable and non-durable (meaning consumer applications which are running when a publication was made on a topic and consuming messages and any durable subscribers which are not active). The broker will then discard the publication. If there are no active subscribers or durable subscribers for a topic, the broker will discard the publication immediately. It will not wait for any subscribers to become active. The only exception is in the case of "Retained Publication" option being exercised, where the broker will cache a publication and deliver to consumers who may arrive later. But note that broker will not wait for all consumers to receive publication before removing it from a topic. I would say there is nothing like 'removing from topic'.
Hope I am clear.
Only active subscribers will get your message in that case, after that your message is removed.
If you want to send your message also to inactive subscribers you can configure durable subscription.
I am struggling to understand Durable subscription. I understand that when a Listener registers itself as a Durable Subscriber to a Topic, it tells JMS - "Hey, I am durable subscriber, from now onwards you need to store all the messages in Topic if I am not there and pass me those messages when I come back"
Now, if that's the case, why can't two subscribers ask for this durable subscription?
Am I missing something?
Quoting from the Java EE tutorial
A durable subscriber registers a durable subscription by specifying a
unique identity that is retained by the JMS provider. Subsequent
subscriber objects that have the same identity resume the subscription
in the state in which it was left by the preceding subscriber. If a
durable subscription has no active subscriber, the JMS provider
retains the subscription’s messages until they are received by the
subscription or until they expire.
To make durable subscriptions work for multiple subscribers on 1 durable subscription the broker would have to store each individual message from the creation of the topic (by the first-ever subscriber) until its expiry, ie potentially forever if no message TTL is specified, because at any point in time a new subscriber can pop in and claim all the messages it "missed" (that is, all messages since the subscription was created). That's just not feasible.
I may be missing the point here, but I can't see how having multiple simultaneous subscribers sharing a subscription would be more practical than defining two separate subscriptions?
The understanding of Duplicate Durable Subscription is incorrect.
Durable subscription does not mean that no multiple subscribers can subscribe. It means no One subscriber can have two different identifiers for durable.
So I was wondering about a problem. Consider a publisher creates a topic at 10:00 am and immediately starts publishing messages on to the topic. It notifies the consumers of creation of the topic and then they subscribe to the topic at 10:01 am. These consumers will not receive messages sent by the publishers between 10:00 am - 10:01 am. So should the messaging provider immediately discard these messages if it finds that there are no active subscribers for this topic to receive any messages. Can we provide a subscription mechanism by which consumers can specify from which point they want to receive messages, (for eg. from the start of the topic or 12/25/2011 10:00 am PST etc.)
In Publish/Subscribe messaging model, publisher does not notify creation of a topic to subscribers.
Publishers and Subscribers are loosely coupled via the topic. So a publisher will not know whether there are any subscribers or not. Messaging provider discards a publication on topic if there are no subscribers for that topic. Some messaging providers inform the publisher if there are no takers for publication. This way publisher can make a decision on whether to continue publishing or not.
Subscribers will start getting publication from the time when a subscription is created. There are two types of subscriptions, durable and non-durable. Non-durable subscriptions are the ones where publications are delivered to subscribers as long as they are active. Durable subscription is the one where publication are delivered even when the subscribers are not active.
There is concept of Retain Publication where in messaging provider retains one latest publication and delivers to subscribers who join late.
I want to understand a thing: When a durable subscriber is activated on a topic, in add on this topic there is another subscriber (mdb). Is possible that the durable subscriber take the messages and don't provide it to the MDB?
Thanks a lot..
When a message is sent to a topic, by intent there's a separation between the publisher and the consumers.
Although there are ways you can create transactional delivery around sending to all consumers, there's no way a consumer can take a message and prevent other consumers from receiving a message as well.