In this post, we are going to discuss Messaging Models supported by Java Messaging System (JMS). Before reading this post, please go through my previous post about JMS to understand some JMS Basic concepts.
JMS Messaging Models
JMS API supports two kinds of Messaging models (Programming models) to support Asynchronous Messaging between Heterogeneous Systems.
- Point-To-Point Model(P2P Model)
- Publish-Subscribe Model(Pub/Sub Model)
Point-to-Point Messaging Model
Point-to-Point Messaging Model is also known as P2P Model. Below diagram shows typical Point-To-Point Messaging model in any Messaging system.
How a P2P Model works?
Point-To-Point model follows these concepts:
- P2P Model uses “Queue” as JMS Destination
- In P2P Model, a JMS Sender or JMS Producer creates and sends messages to a Queue.
- JMS Queue is an administered object, which is created in a JMS Provider by Administrator.
- In P2P Model, a JMS Receiver or JMS Consumer receives and reads messages from a Queue.
- In P2P Model, a JMS Message is delivered to one and only one JMS Consumer.
- We can configure any number of JMS Senders and JMS Receivers to a particular queue. However, any message should be delivered to one and only one Receiver.
- There is no timing dependency between JMS Sender and JMS receiver. That means the JMS Receiver can consume the messages whether it is alive or not when the JMS Sender sent that message.
- In this model, Destination stores messages till its consumed by Receiver.
Possible P2P Model Messaging systems
In this section, we will discuss “What are the possible ways to implement P2P Messaging Model”.
Approach 1: In this P2P Approach, we have configured one sender and one receiver. It is a very simple and straightforward approach as there is only a one-to-one connection.
One sender sends messages to queue and receiver receives those messages.
Approach 2: In this P2P Model, we have configured one sender and multiple receivers. However, communication will happen between one sender to one receiver only. If the sender sends a message to Receiver-1, then only that Receiver (i.e. Receiver-1) receives that message and sends ACK (Acknowledgement) back to the Sender.
Approach 3: In this P2P Model, we have configured multiple Senders and only one Receiver. However, communication will happen one sender to one receiver only.
Approach 4: In this P2P Model, we have configured multiple senders and multiple receivers. However, communication will happen between one sender to one receiver only. If Sender-2 sends a message to Receiver-2, then only that Receiver (i.e. Receiver-2) receives that message and sends ACK (Acknowledgement) back to the Sender-2.
- A P2P model may have more than one sender and more than one receiver, but each message is consumed by one and only one receiver.
Even though this queue has more than one receiver, but each message is consumed by only one Receiver. For example, “Msg1” is only consumed by “JMS Receiver 3”.
- Sometimes, same JMS client may act as a JMS Sender or a JMS Receiver.
Publish/Subscribe Messaging Model
Pub/Sub Messaging model is again divided into two categories.
- Durable Messaging Model: Durable Model is also known as Persistent Messaging Model. In this model, Messages are stored in some kind of store in JMS Server until they are delivered to the destination properly.
- Non-Durable Messaging Model: Non-Durable Model is also known as Non-Persistent Messaging Model. In this model, Messages are not stored in the JMS Server.
The following diagram shows a typical Publish/Subscribe messaging system.
How Pub/Sub Model Works
- Pub/Sub model uses Topic as JMS Destination.
- JMS Administrator uses JMS Provider Admin Console and configures all required ConnectionFactory and Topics Objects in JMS Provider.
- JMS Publisher creates and publishes messages to Topics.
Subscriber subscribes to interested Topics and consumes all messages.
- Pub/Sub Messaging model has timing dependency. That means JMS Subscriber can consume messages which are published to the Topic only after it subscribes to that Topic. Any messages posted before its subscription or any messages posted when it is inactive, cannot be delivered to that Consumer.
- Unlike P2P Model, in this model Destination does not store messages.
Just like P2P Model, Pub/Sub model also contains many possible architectures:
- One Publisher and many Subscribers
- Many Publishers and one Subscriber
- Many Publishers and many Subscribers
Differences between P2P and Pub/Sub Messaging Model
The following table explains the major differences between the two JMS Messaging Models.
|S.No.||Point-To-Point Messaging Model||Publish/Subscribe Messaging Model|
|1.||Each message is delivered to one and only one JMS Receiver||Each message is delivered to multiple Consumers.|
|2.||P2P Model has no timing dependency.||Pub/Sub model has some timing dependency.|
|3.||JMS Receiver sends acknowledgements to JMS Sender once it receives messages.||Acknowledgement is not required.|