JMS:
JMS (Java Message Service).
Q: What is JMS?
JMS is all about sending and receiving messages between two or more clients.
A messaging System allows and promotes the loose coupling of components (JMS helps in building the communication between two or more applications in a loosely coupled manner).
Allows components to post messages for other components.
Asynchronous rather than synchronous.
Also known as MOM (Message-Oriented Middleware)
History:
Java Message Service is developed by Sun Microsystems as a part of Java Platform Enterprise Edition. The first version of JMS i.e., JMS 1.0.2b was released in June 26, 2001. The stable version of JMS is JMS 2.0 released in May 21, 2013.
Q: What does JMS do?
A Java API that allows applications to:
Create
Send
Receive
Read messages
In principle it is kind of like a news system but doesn’t involve email.
Q: Why do we need JMS?
Generally, the user sends message to application. But, if we want to send message from one application to another, we need to use JMS API.
Consider a scenario, one application A is running in INDIA and another application B is running in USA. To send message from A application to B, we need to use JMS.
JMS is also useful when we are writing any event-based application like a Chat server where it needs a publish event mechanism to send messages between the server and the clients.
Note that JMS is different from RMI so there is no need for the destination object to be available online while sending a message. The publisher publishes the message and forgets it, whenever the receiver comes online, it will fetch the message. It’s a very powerful solution for very common problems in today’s world.
Benefits of JMS: 1) Asynchronous: JMS is asynchronous by default. To receive the message, client is not required to send request. Message will arrive automatically to the client.
2) Reliable: It provides assurance that message is delivered.
3) It allows exchanging and using information between other Java Platform languages such as Scala and Groovy.
Disadvantages of JMS:
In other applications, we can use many languages and frameworks. But in JMS, APIs are specified, the message format is not. This is a limitation of JMS. (We can say no Portability just Java technology only)
JMS Messaging Domains:
Two basic models:
point – to – point:
Point-to-point means if we have two clients. Client A and client B. and client A wants to send the message to the client B. and this message will be consume by client B.
It’s like a One-to-One relationship.
To send all the messages we require Queue here. So, Queue will collect all your messages and give to the client B.
So, basically Queue will not send the message. But client B who will consume the Message. It's more like a pool type of messages.
It means Queue will not push. This client will pull.
In point-to-point we use Queue.
Publish/subscribe:
In Publish/Subscribe domain, we have one sender and multiple receivers. So, it’s like One-to-Many relationship
Client 1 will publish.
The concept of Sending and Publishing almost same just in Point-to-point we have Queue and in Publish/Subscribe model we have Topic.
In this, we have to just Subscribes. So, we can receive those messages.
So, if you go with One-to-One Relationship, we use a Queue and if we go with One-to-Many Relationship, we use a Topic.
JMS API:
The Java standard API used to implement the MOM (Message Oriented Middleware).
The Java Message Service API provides a set of interfaces for applications to create, send, receive, and read messages.
It also exchanges information between different systems.
JMS application contains following elements −
JMS clients − JMS clients use JMS API to send and receive messages.
Messages − It includes the data which will be exchanged between JMS clients.
JMS provider − It is a message-oriented middleware software, that provides administrative and control features to the clients.
JMS Sender − It is commonly known as JMS Producer or Publisher, which is used to send messages to destination system.
JMS Receiver − It is generally known as JMS Consumer or Subscriber, which is used to receive messages to the destination system.
JMS Programming Model:
To achieve the programming model in JMS, we require these things.
1) Administered Objects:
Two types of Administered Objects. - Connection Factory and – Destination.
1) Connection Factory: it will have a ConnectionFactory object that will be use to create the connection between JMS client and Service provider. (So simply means once you get the object of Connection Factory, you will get the object of the Connections.)
2) Destination: The client uses an object known as a destination which is used to specify the target of messages it produces and the source of message who consumes it.
The JMS application uses two types of destinations:
1) Queue
2) Topic
2) Connections: it will have a ConnectionFactory object that will use to create the connection between JMS client and Service provider.
3) Sessions: It is a lightweight JMS object, used for producing and consuming messages.
4) Message Producer: The message producer is generated by a session, which is responsible to create the messages.
5) Message Consumer: The message consumer is generated by a session, which is responsible to receive the messages.
6) Messages: It includes the data which will be exchanged between JMS clients. JMS messages have a basic format that is simple but highly flexible.
A JMS message can have three parts: a header, properties, and a body. Only the header is required.
Q: Message Consumption in JMS:
Two ways to consume the messages:
1. Synchronously:
2. Asynchronously:
Comments