Sender receiver availability in JMS pub/sub domain - jms

Here it states "the sender and the receiver do not have to be available at the same time in order to communicate.". And here it states that in pub/sub domain "A client that subscribes to a topic can consume only messages published after the client has created a subscription, and the subscriber must continue to be active in order for it to consume messages.". To me the italicized statement seems to contradict the first statement("the sender and the receiver do not have to be available at the same time").
If the subscriber must continue to be active to consume messages, it means the sender and the receiver must be available at the same time at least in the pub/sub domain. If they must be available, the pub/sub domain is only as good as RMI. Is this true?

...the sender and the receiver do not have to be available at the same time in order to communicate.
As far as I can tell this is a general statement about messaging and not a nuanced explanation of the semantics the JMS API provides. Notice it is under the "What Is Messaging?" heading before specific discussion of the JMS API begins.
For what it's worth, the JMS API does provide these semantics if you're using the point-to-point style of messaging (also discussed in the tutorial). It also provides a variation of these semantics using the pub-sub style of messaging, but I'll get to that later.
A client that subscribes to a topic can consume only messages published after the client has created a subscription, and the subscriber must continue to be active in order for it to consume messages.
If you read the next sentence after that you'll find some important additional details:
The JMS API relaxes this timing dependency to some extent by allowing subscribers to create durable subscriptions, which receive messages sent while the subscribers are not active.
So, as I mentioned previously, you can get a variation of the inactive sender/receiver semantics using the pub-sub style of messaging via durable subscriptions.
Keep in mind that the document you're referencing is just a tutorial. It's not the JMS specification. I doubt the wording of the tutorial was subjected to the same scrutiny as the specification so you are more likely to find ambiguous statements.

Related

Send, Publish and Request/Response in MasstTransit

Recently I am trying to use MassTransit in our microservice ecosystem.
According to MassTransit vocabulary and from documents my understanding is :
Publish: Sends a message to 1 or many subscribers (Pub/Sub Pattern) to propagate the message.
Send: Used to send messages in fire and forget fashion like publish, but instead It is just used for one receiver. The main difference with Publish is that in Send if your destination didn't receive a message, it would return an exception.
Requests: uses request/reply pattern to just send a message and get a response in a different channel to be able to get response value from the receiver.
Now, my question is according to the Microservice concept, to follow the event-driven design, we use Publish to propagate messages(Events) to the entire ecosystem. but what is exactly the usage (use case) of Send here? Just to get an exception if the receiver doesn't exist?
My next question is that is it a good approach to use Publish, Send and Requests in a Microservices ecosystem at the same time? like publish for propagation events, Send for command (fire and forget), and Requests for getting responses from the destination.
----- Update
I also found here which Chris Patterson clear lots of things. It also helps me a lot.
Your question is not related to MassTransit. MassTransit implements well-known messaging patterns thoughtfully described on popular resources such as Enterprise Integration Patterns
As Eben wrote in his answer, the decision of what pattern to use is driven by intent. There are also technical differences in the message delivery mechanics for each pattern.
Send is for commands, you tell some other service to do something. You do not wait for a reply (fire and forget), although you might get a confirmation of the action success or failure by other means (an event, for example).
It is an implementation of the point-to-point channel, where you also can implement competing consumers to scale the processing, but those will be instances of the same service.
With MassTransit using RabbitMQ it's done by publishing messages to the endpoint exchange rather than to the message type exchange, so no other endpoints will get the message even though they can consume it.
Publish is for events. It's a broadcast type of delivery or fan-out. You might be publishing events to which no one is listening, so you don't really know who will be consuming them. You also don't expect any response.
It is an implementation of the publish-subscribe channel.
MassTransit with RabbitMQ creates exchanges for each message type published and publishes messages to those exchanges. Consumers create bindings between their endpoint exchanges and message exchanges, so each consumer service (different apps) will get those in their independent queues.
Request-response can be used for both commands that need to be confirmed, or for queries.
It is an implementation of the request-reply message pattern.
MassTransit has nice diagrams in the docs explaining the mechanics for RabbitMQ.
Those messaging patterns are frequently used in a complex distributed system in different combinations and variations.
The difference between Send and Publish has to do with intent.
As you stated, Send is for commands and Publish is for events. I worked on a large enterprise system once running on webMethods as the integration engine/service bus and only events were used. I can tell you that it was less than ideal. If the distinction had been there between commands and events it would've made a lot more sense to more people. Anyway, technically one needs a message enqueued and on that level it doesn't matter, which is why a queueing mechanism typically would not care about such semantics.
To illustrate this with a silly example: Facebook places and Event on my timeline that one of my friends is having a birthday on a particular day. I can respond directly (send a message) or I could publish a message on my timeline and hope my friend sees it. Another silly example: You send an e-mail to PersonA and CC 4 others asking "Please produce report ABC". PersonA would be expected to produce the report or arrange for it to be done. If that same e-mail went to all five people as the recipient (no CC) then who gets to do it? I know, even for Publish one could have a 1-1 recipient/topic but what if another endpoint subscribed? What would that mean?
So the sender is responsible, still configurable as subscriptions are, to determine where to Send the message to. For my own service bus I use an implementation of an IMessageRouteProvider interface. A practical example in a system I once developed was where e-mails received had to have their body converted to an image for a content store (IBM FileNet P8 if memory serves). For reasons I will not go into the systems were stopped each night at 20h00 and restarted at 6h00 in the morning. This led to a backlog of usually around 8000 e-mails that had to be converted. The conversion endpoint would process a conversion in about 2 seconds but that still takes a while to work through. In the meantime the web front-end folks could request PDF files for conversion to paged TIFF files. Now, these ended up at the end of the queue and they would have to wait hours for that to come back. The solution was to implement another conversion endpoint, with its own queue, and have the web front-end configured to send the same message type, e.g. ConvertDocumentCommand to that "priority" queue for processing. Pretty easy to do. Now, if that had been a publish how would I do that split? The same event going to 2 different endpoints under different circumstances? Well, you could have another subscription store for your system but now you'd need to maintain both. There could be another answer such as coding this logic into the send bit but that is a design choice and would require coding changes.
In my own Shuttle.Esb service bus I only have Send and Publish. For request/response both the sender and receiver have an inbox and a request would be sent (Send) to the receiver and it in turn could reply (also a Send but uses the sender's URI).

Does Google Pub/Sub queue or topic?

I am familiar with JMS and novice with Google Pub/Sub.
In JMS there are 2 options:
Queue: only one consumer can accept message.
Topic: each consumer accepts each message from the topic
I believe that Google Pub/Sub should support something like this, but a quick Googling didn't help me to answer that question.
Please point me out to the corresponding documentation part.
As the name "Pub/Sub" indicates, Google Pub/Sub supports publish/subscribes semantics which correspond to JMS topics. It doesn't support point-to-point semantics which correspond to JMS queues, at least not directly.
You can see an overview of the semantics in the documentation. The "Publisher-subscriber relationships" section may be helpful. To be clear, this documentation does use the word queue in two places:
In the "Pub/Sub message flow" section: "When a message is acknowledged by the subscriber, it is removed from the subscription's message queue."
In the "Common use cases" section: "For example, a large queue of tasks can be efficiently distributed among multiple workers, such as Google Compute Engine instances."
The term queue here is being used to refer to the actual subscription on the topic (i.e. where the messages are placed for subscribers to consume). Furthermore, the architectural overview includes this diagram:
This diagram demonstrates how multiple subscribers can receive messages from the same subscription (e.g. for balancing workloads). This would be akin to the "shared subscription" functionality added for topics in JMS 2.
I was looking for the answer to this question, and I've found this documentation that describe the behaviour of a queue rather than a topic :
https://cloud.google.com/pubsub/docs/subscriber
While a message is outstanding to a subscriber, however, Pub/Sub tries
not to deliver it to any other subscriber on the same subscription.
So my understanding is that if you want
a topic behaviour (one to many): you create many subscriptions, each will get a copy of the message
a queue behaviour : you create one subscription
Then on each subscription you can run one or more subscribers for load balancing, each message is delivered to only one subscriber
I didn't see what kind of distribution among subscribers of a subscription. likely to be round robin.

JMS QPID Queue vs Topic

I'm confused when it comes to JMS Queue/Topic. What I want is messages should go to every subscriber and I want subscribers to receive messages from inactive time when they become active. However, I don't have control over whether or not subscribers have durable subscription. Is there a way to set up a persistent Queue, and set it up so that every subscriber will receive same message? And how to set this up using spring config
Thanks much.
This is mostly a question where the design of your system affects the outcome.
You could use UI tooling to create durable subscriptions for the clients that need to but that is cumbersome and error prone. You could use something like camel or other configuration on the target broker to fanout messages from an incoming Queue to outgoing Queues that map to the consumer subscriptions.
It all depends mostly on the requirements and your overall design so a real answer is beyond the scope of a SO answer without you doing some more legwork to narrow the scope a bit. JMS itself does not define any answer for this so it will come down a bit to the broker you've chosen and possibly other third party tooling that you might pick to do what you need.

Does WMQ topic save message itself?

If a publisher publish some messages to a WMQ topic, but the subsciber didn't take it, then where the messages are saved? is there any way to know the message count?
As MQ is JMS compliant, the answer is mostly a JMS answer.
If the subscription is not durable and no subscription is registered, the messages for that subscriber are discarded.
If the subscription is durable, MQ creates a queue (or uses a predefined one if specified by the subscriber) to deliver the messages. The messages will collect there if the subscriber is not consuming them.
The 3rd case as Dave points out int he comments is that the non-durable subscriber is holding the subscription open but not consuming the messages. Since a queue is created to receive these that queue depth can be queried to determine if there's a back-up.
Based on there being a queue for every subscription (durable or otherwise) just look in the durable subscriber's queue to determine the number of messages outstanding.
Please also see Publish/subscribe lifecycles in the MQ Knowledge Center for more description of the behavior and specification of durable subscriber queues.
Of course, if that queue fills up the behavior changes. Depending on the settings either the publishers block or the publications continue but the messages are routed to an exception queue (if specified), the DLQ, or discarded.
Thanks Dave Ware for the comments about non-durable subscriptions.
I'm wondering from the question if you're asking if MQ keeps a store of all the messages published to a topic, independent of any registered subscriptions?
If that's the question, then no, it doesn't. When messages are published they are matched to each existing subscription and a copy is sent to each of their associated queues as T.Rob describes.
So the only queue depths to worry about are those of the subscriptions.
(There is a caveat in that MQ supports "retained publications", - it means MQ keeps just the most recent publication on that topic string for late subscriptions if you choose to do that).
I try to explain all this here (slides/video), which may help... http://www.slideshare.net/DavidWare1/ame-2271-mq-publish-subscribe-pdf

Do the JMS spec or the various implementations support delivery confirmation of messages?

Lets say Producer sends a message to the JMS Topic "news". Consumer1 reads the message, but Consumer2 is offline so he hasn't read the message yet.
Is there any build-in (to the spec or impl) way for the Producer to be notified that Consumer1 has read his message, but Consumer2 has not? This would, in fact, mirror the Read Notification of an email.
Clearly you could implement this by having each Consumer send an acknowledgement, but I'm looking for something already a part of JMS or a JMS system.
The JMS FAQ says
JMS API messaging provides guaranteed
delivery via the once-and-only-once
delivery semantics of PERSISTENT
messages. In addition, message
consumers can insure reliable
processing of messages by using either
CLIENT_ACKNOWLEDGE mode or transacted
sessions. This achieves reliable
delivery with minimum synchronization
and is the enterprise messaging model
most vendors and developers prefer.
The JMS API does not define a schema
of systems messages (such as delivery
notifications). If an application
requires acknowledgment of message
receipt, it can define an
application-level acknowledgment
message.
These issues are more clearly
understood when they are examined in
the context of publish/subscribe
applications. In this context,
synchronous delivery and/or system
acknowledgment of receipt are not an
effective mechanism for implementing
reliable applications (because
producers by definition are not, and
do not want to be, responsible for
end-to-end message delivery).
I'm no expert, but I think this is saying that the spec doesn't define a way for a consumer to confirm receipt to a producer, by design. I don't know of any implementations that do - and if they did, it looks like it'd be a big departure from the model the spec captures.

Resources