MDB Hello World With WildFly And ActiveMQ With RAR Deployment

Posted by {"name"=>"Palash Ray", "email"=>"paawak@gmail.com", "url"=>"https://www.linkedin.com/in/palash-ray/"} on February 13, 2020 · 6 mins read

Introduction

This is the 2nd increment of the MDB Series. In Part 1, we spoke about what is an MDB? We then, took a simple design problem: a servlet posts user data to a Message Queue. The Message Broker then sends a notification to a MDB. We used the Artemis MQ, which comes embedded in Wildfly, as our Message Broker.
In this increment, we are going to use Apache ActiveMQ, running as a separate service, as our Message Broker. We are going to use the RAR archive, or specifically, the activemq-rar archive, to communicate between Wildfly and ActiveMQ.

What is a RAR?

In simple terms, a RAR Archive or a Resource Adapter Archive, is a JEE Component that helps a JEE Server to communicate with an external Resource. The RAR implements the JEE Connector architecture specs. It can be either deployed within a EAR file, or independently. In our example, we are going to deploy the activemq.rar independently.

Implementation Details

The structure will be very similar to what we had in the 1st increment of the series, with subtle differences as highlighted below.

The pom.xml

The pom is a slight improvement, with minor version upgrades for the below dependencies:

    	
   		 javax.servlet
   		 javax.servlet-api
   		 3.1.0
   		 provided
   	 
   	 
   		 javax.jms
   		 javax.jms-api
   		 2.0.1
   		 provided
   	 
   	 
   		 javax.ejb
   		 javax.ejb-api
   		 3.2.2
   		 provided
   	 

We have removed the org.wildfly:wildfly-jms-client-bom dependency, as it is no longer needed.
Apart from the above changes, all other elements like the war-plugin etc., remains the same.

Defining the JMS Queue

The WEB-INF/embedded-artemis-jms.xml file is no longer needed, instead, the Queue is defined in the standalone.xml configuration, as described in a later section.

mdb.properties

There are slight changes to the JNDI names of the Queue and the ConnectionFactory:

ACTIVEMQ_QUEUE_LOOKUP=java:/queue/HELLOWORLDMDBQueue
ACTIVEMQ_JMS_CONNECTION_FACTORY=java:/ActiveMQConnectionFactory
WILDFLY_USER=user
WILDFLY_PASSWORD=user123

MdbConfig

This remains the same, except for the change in the property file keys.

AuthorRestController

This remains the same, without any changes.

Configuring the MDB to Receive JMS Messages

The AuthorRequestListenerBean remains the same structurally, with a few minor changes in the @MessageDriven annotation as shown below:

@MessageDriven(name = "HelloWorldQueueMDB",
    activationConfig = {
   	 @ActivationConfigProperty(propertyName = "destinationType",
   		 propertyValue = "javax.jms.Queue"),
   	 @ActivationConfigProperty(propertyName = "destination",
   		 propertyValue = "HELLOWORLDMDBQueue"),
   	 @ActivationConfigProperty(propertyName = "acknowledgeMode",
   		 propertyValue = "Auto-acknowledge") })
public class AuthorRequestListenerBean implements MessageListener {
...
}

The standalone.xml

As before, we would need to modify the default standalone.xml that comes bundled with WildFly. We would be taking Wildfly 18 for our reference. Copy the $WILDFLY_HOME/standalone/configuration/standalone.xml and rename it to standalone-with-external-activemq-rar-deployment.xml. You can see the differences here:
https://github.com/paawak/blog/commit/2013c222f8d847e7d41c8910c92ff68f67eef4a8?diff=unified
The differences are highlighted below.

Adding the ActiveMQ Messaging Extension

Under the extensions section, we would need to add the below extension:

Defining the ActiveMQ Messaging SubSystem

Like before, we would need to define the new subsystem for ActiveMQ Messaging:

    	
        	
            	
                	
            	
            	
            	
            	
                	
            	
            	
                	
            	
            	
            	
                	
                	
            	
            	
                	
            	
            	
            	
            	
            	
            	
        	
    	

Adding the MDB Resource Adapter

As before, under the subsystem ejb3 section, we would need to add the details of the Resource Adapter to be used to communicate with the ActiveMQ Messaging system.


   
   

Describing the ActiveMQ Resource Adapter

This is a new section that we would need to add under the resource-adapters to describe the ActiveMQ RAR. Note that we would need to specify the ActiveMQ broker details, as well as the Queue JNDI lookup details here.

    	
        	
            	
           		 activemq-rar-5.15.4.rar
                	XATransaction
                	false
                	admin
                	admin
                	tcp://localhost:61616?jms.rmIdFromConnectionId=true
                	
                    	
                        	
                            	1
                            	20
                            	false
                            	false
                        	
                    	
                	
                	
                    	
                        	HELLOWORLDMDBQueue
                    	
                	
            	
        	
    	

This is how the completed standalone-with-external-activemq-rar-deployment.xml looks like:
https://github.com/paawak/blog/blob/master/code/mdb-demo/wildfly/external-activemq/rar-archive-deployment/mdb-activemq-rar-demo-spring/src/main/wildfly/standalone-with-external-activemq-rar-deployment.xml

Source Code

The complete source can be found here:
https://github.com/paawak/blog/tree/master/code/mdb-demo/wildfly/external-activemq/rar-archive-deployment/mdb-activemq-rar-demo-spring

Running the Demo

Building the war

To build the war file, we do:

mvn clean package

Deploying the RAR

The ActiveMQ RAR is no longer included in the ActiveMQ Distribution, but, can be downloaded from here: https://repo1.maven.org/maven2/org/apache/activemq/activemq-rar/
Just copy the RAR file into the $WILDFLY_HOME/standalone/deployments directory.

Creating a Guest User in WildFly

Follow the steps as described in the previous increment.

Deploying the war

Copy the mdb-activemq-rar-demo-spring.war into the directory $WILDFLY_HOME/standalone/deployments.

Starting the ActiveMQ Locally

Download and unpack the ActiveMQ distribution for your platform. Then start it with

bin/activemq start

Starting WildFly with our custom configuration

Copy the src/main/wildfly/standalone-with-external-activemq-rar-deployment.xml into the $WILDFLY_HOME/standalone/configuration/ directory. Then start WildFly with the below command:

$WILDFLY_HOME/bin/standalone.sh -c standalone-with-external-activemq-rar-deployment.xml

After WildFly starts successfully, you can access the Author page here:
http://localhost:8080/mdb-activemq-rar-demo-spring/author.html
As before, you should be able to post a Message through the Author Page, and the same would appear on the Console from the MDB as a Json Message.

23:32:55,072 INFO [com.swayam.demo.mdb.rar.spring.listener.AuthorRequestListenerBean] (default-threads - 2) Text message received: {"authorId":1,"authorFirstName":"aaaa","authorLastName":"bbbb","genreShortName":"cccc","genreName":"dddd"}