What Is the Java Message Service?
Want to learn more about the Java Message Service (JMS)?
Join the DZone community and get the full member experience.Join For Free
In this article, we attempt to learn more about the Java Message Service (JMS). JMS stands for Java Message Service. As we proceed, we address what it is, why we need it, whether it is asynchronous or synchronous, and more.
Why Do We Need JMS?
Enterprise editions have started to focus on distributed application development. As a result, we need communication between distributed applications. It was difficult to combine communication in an existing application. As a result, JMS was introduced. JMS allows communication between the web component, application client, and JMS application. Besides, asynchronous messaging allows communication between two applications without being hard-wired to each other.
With that in mind, JMS is asynchronous. Now, this brings us to our next question:
Why Is JMS Asynchronous?
First, let's establish the difference between asynchronous and synchronous method invocation.
In the asynchronous method invocation, a response is sent back to the user before method execution completes. This is very useful in long-running or background process as they take a longer time. As the response is sent back to the user, the user can perform another task. On the other hand, the method can complete its execution.
You can consider the asynchronous method as a printing mechanism. In the process of printing, the printer will continue to print and the user can perform another task on PC.
In the synchronous method, the response is sent back to the user on complete completion of the method.
The sender will not like to stop their work until the receiver receives a message. With the help of the asynchronous message, the sender sends a message and continue his/her work.
Operation by JMS
We can perform the following task in JMS
1) Create a message
2) Send message
3) Receive message
4) Read message
Type of JMS
1) Point-to-Point (One-to-One)
In the point-to-point message bean, we have one sender and one receiver. It is like personal chat in mobile, but this messaging is between components. When the sender sends a message, if there is more than one message, then it has to wait in the queue. Once it reaches to a receiver, a receiver can consume it and acknowledge it.
2) Publish/Subscriber (One-to-Many)
Publish/subscriber is like Netflix. With Netflix, we have one provider (sender) and many consumers (receiver). Many users can subscribe to Netflix and watch a T.V. show or movie uploaded by Netflix. This is the same way as with the publish/ subscriber; the first component needs to subscribe. After subscribing, the component can consume and acknowledge messages. If there is more than one message, then it has to wait on a topic.
Programming Model of JMS
For JMS, we need to create an object of the connection factory. After that, we can create an object of connection that allows us to create an object of a session. Once these three objects are created, we can create a message sender and message producer.
Hope this helps. Happy messaging!
Opinions expressed by DZone contributors are their own.