DEV Community

Cover image for Java Message Service (JMS)
Sandeep Kumar Seeram
Sandeep Kumar Seeram

Posted on

Java Message Service (JMS)

Java Message Service (JMS) is a messaging API that provides a standard way for Java applications to send and receive messages. JMS is a loosely coupled messaging system, which means that the sender and receiver of a message do not need to be running at the same time. JMS is also a reliable messaging system, which means that messages are not lost or corrupted.

JMS is used in a variety of applications, including:

• Enterprise application integration (EAI)
• Business-to-business (B2B) integration
• Web services
• Cloud computing

JMS provides two messaging domains:

• Point-to-point messaging
• Publish-subscribe messaging

In point-to-point messaging, there is a one-to-one relationship between the sender and receiver of a message. In publish-subscribe messaging, there is a one-to-many relationship between the sender and receiver of a message.

JMS provides a number of features that make it a powerful messaging API, including:

• Message persistence
• Message acknowledgment
• Message delivery guarantees
• Message filtering
• Message expiration
• Message transformation

JMS is a mature and widely used messaging API. It is supported by a variety of messaging vendors, including IBM, Oracle, and Red Hat.

Here are some of the benefits of using JMS:

• Loosely coupled messaging: The sender and receiver of a message do not need to be running at the same time.
• Reliable messaging: Messages are not lost or corrupted.
• Standardized API: JMS is a standard API, which means that Java applications can communicate with any JMS provider.
• Vendor-neutral: JMS is a vendor-neutral API, which means that Java applications can communicate with any JMS provider without being locked into a particular vendor.
• Mature and widely used: JMS is a mature and widely used messaging API, which means that there is a large body of knowledge and expertise available.

Java program that sends messages via Java JMS:

import javax.jms.*;
import javax.naming.*;

public class JmsMessageSender {
    public static void main(String[] args) throws NamingException, JMSException {
        // Set up the JNDI context to access the JMS provider
        Context context = new InitialContext();
        ConnectionFactory connectionFactory = (ConnectionFactory) context.lookup("ConnectionFactory");
        Destination destination = (Destination) context.lookup("queue/MyQueue");

        // Create a JMS connection and session
        Connection connection = connectionFactory.createConnection();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        // Create a JMS message producer
        MessageProducer producer = session.createProducer(destination);

        // Create a text message and send it
        TextMessage message = session.createTextMessage("Hello, world!");
        producer.send(message);

        // Clean up resources
        producer.close();
        session.close();
        connection.close();
    }
}

Enter fullscreen mode Exit fullscreen mode

Java program that receives messages from the JMS queue named “MyQueue”:

import javax.jms.*;
import javax.naming.*;

public class JmsMessageReceiver {
    public static void main(String[] args) throws NamingException, JMSException {
        // Set up the JNDI context to access the JMS provider
        Context context = new InitialContext();
        ConnectionFactory connectionFactory = (ConnectionFactory) context.lookup("ConnectionFactory");
        Destination destination = (Destination) context.lookup("queue/MyQueue");

        // Create a JMS connection and session
        Connection connection = connectionFactory.createConnection();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        // Create a JMS message consumer
        MessageConsumer consumer = session.createConsumer(destination);

        // Start the connection
        connection.start();

        // Receive messages until there are no more
        while (true) {
            Message message = consumer.receive();
            if (message instanceof TextMessage) {
                TextMessage textMessage = (TextMessage) message;
                System.out.println("Received message: " + textMessage.getText());
            } else {
                System.out.println("Received message of unsupported type: " + message.getClass().getName());
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more