By using AWS re:Post, you agree to the AWS re:Post Terms of Use

Simplifying Messaging with JMS 2.0 in Amazon MQ for ActiveMQ 5.18+

3 minute read
Content level: Intermediate
1

This article explores how leveraging the simplified JMS 2.0 APIs in Amazon MQ for ActiveMQ version 5.18 can help developers build more readable and maintainable messaging applications through reduced boilerplate code and enhanced resource management

Amazon MQ for ActiveMQ version 5.18 supports the use of simplified JMS 2.0 APIs, which enhances messaging applications through reduced boilerplate code and improved resource management.

One of the major improvements in JMS 2.0 is the introduction of the JMSContext interface, which implements AutoCloseable, allowing it to be used within try-with-resources blocks. This simplifies code by automatically managing the lifecycle of resources, ensuring that connections are closed cleanly at the end of the block without requiring explicit finally blocks. This streamlined approach not only makes the code more concise and readable but also reduces the likelihood of resource leaks, particularly in complex messaging scenarios. Developers can now build messaging applications that are more maintainable and less prone to errors thanks to the improvements in JMS 2.0.

For example, the following sends a message to an ActiveMQ queue using JMS1.1 API

JMS 1.1

import jakarta.jms.Connection;
import jakarta.jms.Destination;
import jakarta.jms.JMSException;
import jakarta.jms.MessageProducer;
import jakarta.jms.Session;
import jakarta.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;

public class JMS11Example {
    public static void main(String[] args) {
        String brokerUrl = SecretsManagerUtil.getSecret("REPOST_BROKER_URL");
        String username = SecretsManagerUtil.getSecret("REPOST_BROKER_USERNAME");
        String password = SecretsManagerUtil.getSecret("REPOST_BROKER_PASSWORD");


        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(brokerUrl);
        Connection connection = null;
        try{
            connection = connectionFactory.createConnection(username, password);
            connection.start();

            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            Destination repostqueue = session.createQueue("repostqueue");

            MessageProducer messageProducer = session.createProducer(repostqueue);
            TextMessage textMessage = session.createTextMessage("Hello from JMS 1.1!");

            messageProducer.send(textMessage);

        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

The code for similar functionality in JMS 2.0 is below:

JMS 2.0

import jakarta.jms.JMSContext;
import jakarta.jms.JMSProducer;
import jakarta.jms.Queue;
import org.apache.activemq.ActiveMQConnectionFactory;

public class JMS2Example {
    public static void main(String[] args) {
        String brokerUrl = SecretsManagerUtil.getSecret("REPOST_BROKER_URL");
        String username = SecretsManagerUtil.getSecret("REPOST_BROKER_USERNAME");
        String password = SecretsManagerUtil.getSecret("REPOST_BROKER_PASSWORD");
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(brokerUrl);
        try(JMSContext context = connectionFactory.createContext(username, password)){
            Queue repostqueue = context.createQueue("repostqueue");
            JMSProducer producer = context.createProducer();

            // Send message
            producer.send(repostqueue, "Hello from JMS 2.0!");
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Conclusion

The code in JMS2.0 is much simpler with the use of fewer interfaces. If you are using Amazon MQ for ActiveMQ 5.18 or above, you can consider updating your client APIs to JMS2.0 for better readability, maintainability and less error prone code. When developing production applications, follow secure coding and testing best practices. The example code retrieves the Broker URL, Username, and Password from AWS Secrets Manager. For a working example, refer to the code provided in this article.

profile pictureAWS
EXPERT
published a month ago73 views