Messaging with JMS using Payara with embedded OpenMQ broker

Last Updated:  February 26, 2020 | Published: April 25, 2019

.Messaging is a key concept for distributed enterprise applications. There are a lot of use cases, where you don't want or need a sync response you get with e.g. blocking REST calls. The counterpart, async messaging, can make sense for the following: IoT (sensor data), event streaming, data duplication, etc. With Kafka and other highly-distributed messaging solutions, you may forget that there is already a proven messaging standard. This is part of Java/Jakarta EE and called JMS (Java Message Service). Most of the major application servers (like Payara) also provide an embedded broker.

For this Java EE standard, I'm providing a simple introduction for sending and receiving JSON messages with this blog post. The technology setup is the following: Java 8, Java EE 8, Payara 5.191 and H2 for storing the messages.

JMS prerequisites

As Payara already comes with OpenMQ, which implements the Java Message Service (JMS) standard, you don't have to set up an external JMS broker (e.g ActiveMQ, RabbitMQ ..) for this example and can use the embedded version (think twice if you use this in production).

The connection pool for the embedded OpenMQ is preconfigured and we can directly make use of it via its JNDI name jms/__defaultConnectionFactory. If you want to connect to an external broker you would have to set up the connection manually (take a look at this excellent example for ActiveMQ from Steve Millidge itself).

With JMS you can make use of two different concepts for delivering our messages: Topics (publish & subscribe) and Queues (point-to-point). In this example, I'm using a javax.jms.Queue but the code would look quite similar for using a Topic.

The JMS Queue or Topic has to be first configured within Payara as a JMS Destination Resource and can be configured either via the Payara admin panel (Resources – JMS Resources – Destination Resources) or using asadmin:

You first have to specify the resource type (Topic or Queue). Next, the physical name of the resource within the broker (will be created if it doesn't exist). Last but not least the JNDI name of the resource.

Let's start coding

The Maven project for this showcase is a simple and thin Java EE project:

In this showcase I'm sending random stock information (using JSON with JSONP) every two seconds with a simple EJB timer:

For sending messages, you first have to create a connection via the JMS ConnectionFactory, then create a Session and finally a message producer for the concrete topic or queue. You can access the required resources via their JNDI names as configured before.

Receiving messages is achieved with so-called Message-Driven Beans (MDB) which implement the MessageListener interface and are configured (which topic or queue to listen) using the @MessageDriven annotation. In this example, I'm sending and receiving the message within the same application (for simplicity). Furthermore, I'm storing the message payload in the embedded H2 database of Payara:

Once deployed to Payara, the console output looks like the following:

The JPA entity StockHistory looks like the following:

For the sake of completeness, this is the persistence.xml for this small application:

You can find the full code on GitHub with a step-by-step guide to run this example locally on your machine with Docker. If you are looking for a simple JMS quickstart with Open Liberty, have a look at one of my previous posts.

Keep sending/receiving messages,

Phil

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
>