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.
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.
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 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.
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.
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
This remains the same, except for the change in the property file keys.
This remains the same, without any changes.
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 { ... }
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.
Under the extensions section, we would need to add the below extension:
Like before, we would need to define the new subsystem for ActiveMQ Messaging:
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.
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
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
To build the war file, we do:
mvn clean package
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.
Follow the steps as described in the previous increment.
Copy the mdb-activemq-rar-demo-spring.war into the directory $WILDFLY_HOME/standalone/deployments.
Download and unpack the ActiveMQ distribution for your platform. Then start it with
bin/activemq start
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"}