Java Messaging Services – Part 3 – Setup Pega to ActiveMQ Connection
In this blog article, we will see two things – configure the deployment descriptor or the conf files to use the JNDI resources and Import the ActiveMQ external jar file
Important note: This post is specific to Apache tomcat server. If you use J2EE compliant application servers like Websphere, WebLogic then your implementation will vary.
It is recommended to visit the previous article on JMS series Part 1 & Part 2. Do not miss the Introduction post!
https://myknowtech.com/tag/jms
What is JMS?
It is a Java Message oriented middleware (MOM) API that supports sending messages between two or more systems.
How to setup JNDI resources for ActiveMQ connection in Tomcat?
Pega allows two ways to setup the JNDI resources
- Using JNDI server.
- Using direct resource reference in deployment descriptor or server configuration files.
For more information on JNDI, use the link – https://stackoverflow.com/questions/4365621/what-is-jndi-what-is-its-basic-use-when-is-it-used
1. Using JNDI server –
You can use this option, if the JMS provider supports/provides JNDI server, so that we can connect to the server from a Pega application using JNDI server instance. By this way, the JNDI lookup will give us the available connection factory and destination names.
But sadly, ActiveMQ does not provide a full JNDI server.
I am not going to talk more about JNDI server in this post. Let’s concentrate on the second option
2. Using direct resource reference
There are three ways (or more ways) to setup the connections using JNDI direct resource reference.
The following files can be used for the configuration
a. Server.xml – This helps in configuring the Tomcat server in XML format. You will find the configuration file in the directory – <Tomcat Home>/conf/server.xml
Here you can specify global JNDI resources for the Tomcat server. By specifying the resources globally you can use it in all the applications that are hosted on the Tomcat server.
2. Context.xml – It defines certain behaviour, JNDI resources,and settings for the Tomcat server or for the web application.
You will find the context.xml in the same <Tomcat home>/conf/context.xml for the tomcat server.
For the web application, you will find or you can create one in <Tomcat home>/webapps/prweb/META-INF folder
3. Web.xml – It is a deployment descriptor file that describes how to deploy a web application in a servlet. Every web application in the Tomcat server holds the web.xml file for the configuration.
You can always check in Google, the different ways to set the JNDI resources for the Web application.
I am going to follow the below steps to setup the JNDI resources.
- Declare the resource globally in the server.xml.
- Specify the resource link in webapp context.xml
- Specify the resource in the web.xml
Important tip – Make sure you have a backup copy for all the configuration files, so that if you ** it up, you can bring it back 🙂
1. Declare the resource globally in the server.xml
Note: It is not always mandatory to declare all the resource globally. To showcase the different option, I chose updating all 3 files 🙂
Open the server.xml file
<tomcat home>/conf/server.xml
In the GlobalNamingResources tag – add the two tags. 1 for connection factory and the other for Queue destination.
<Resource name=”jms/ConnectionFactory” auth=”Container” type=”org.apache.activemq.ActiveMQConnectionFactory” description=”JMS Connection Factory” factory=”org.apache.activemq.jndi.JNDIReferenceFactory” brokerURL=”tcp://localhost:61616″ brokerName=”ActiveMQBroker” useEmbeddedBroker=”false”/>
<Resource name=”jms/queue/FirstQ” auth=”Container” type=”org.apache.activemq.command.ActiveMQQueue” factory=”org.apache.activemq.jndi.JNDIReferenceFactory” physicalName=”MyKnowPega.FirstQ”/>
Save the file.
Note: Make sure you give the right type name (java class)
2. Specify the resource link in webapp context.xml
What we are going to do here is refer the global JNDI resource we defined in the server level to the application level using resource-link tag.
Step 1: Switch to prweb/META-INF directory.
Step 2: Add a new context.xml file.
Make sure you start with and end with context tag.
Refer the two resource link for the connection factory and Queue.
In the attribute global, make sure you specify the same name you used in the server.xml global declaration.
Tip: You can save as server context.xml file and remove unwanted lines.
ResourceLink tag –
<ResourceLink global=”jms/ConnectionFactory” name=”jms/ConnectionFactory” type=”javax.jms.ConnectionFactory”/>
<ResourceLink global=”jms/queue/FirstQ” name=”jms/queue/FirstQ” type=”javax.jms.Queue”/>
3. Specify the resource in the web.xml
What we are going to do here is refer the resource inside the deployment descriptor file – web.xml
Step 1: Open the prweb/WEB-INF/web.xml
Step 2: Add the two resource-ref for connection factory and Queue destination.
Resource-ref tag –
<resource-ref id=”ResourceRef_11″>
<description>Connection factory</description>
<res-ref-name>jms/ConnectionFactory</res-ref-name>
<res-type>org.apache.activemq.ActiveMQConnectionFactory</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
<resource-ref id=”ResourceRef_12″>
<description>queue</description>
<res-ref-name>jms/queue/FirstQ</res-ref-name>
<res-type>org.apache.activemq.command.ActiveMQQueue</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
Our Pega is ready with the configuration. Restart the server.
The second part of the post is about making the ActiveMQ Java classes ready in the Pega engine base.
We know by default, Pega engine comes with lot of Java classes to support different functionality, but there will be always situation where you may need to install external jar.
In our case with ActiveMQ as Third party JMS provider, we need to install a jar – activemq-all-<version>
I already tried to create a new Connect-JMS rule without the activemq jar and I ended up with classnotfound error!
Step 1: download the right jar version for your activeMQ connection.
Version of my ActiveMQ is 5.15.12. Use the same version jar.
Follow the link – https://mvnrepository.com/artifact/org.apache.activemq/activemq-all
Download the jar.
Step 2: Manually import the jar using Import wizard.
Important note: When you do it in real time project, make sure you try the new third party Jar in sandbox or playground environment. Else take right back up, so that you can fall back in case of issues
Complete the wizard.
Step 3: Restart the server.
That’s it
Just to quickly verify, Login to Pega, and create a test Connect-JMS rule.
Specify resource reference option and see if the picklist is populating value for Connection factory and destination field.
All good!!
Pega is fully ready to make use of ActiveMQ JMS Provider. In the next article, we will see how to setup a JMS connector and send a message to the Queue destination we created before in ActiveMQ. Finally, something to do in Pega designer studio.