In a previous blog we described how to disable the OpenMQ message broker in Payara Server. In this blog we'll describe how to use Active MQ and send and receive messages from Payara Server.
Installing ActiveMQWe'll assume a default installation of ActiveMQ downloaded installed from http://activemq.apache.org/download.html. In this blog we are using the 5.15.11 release. Which we have downloaded and unzipped and run the default installation using;
./activemq consolefrom within the bin directory of ActiveMQ. This will run the default ActiveMQ installation in the foreground so that we can see any log messages.
We will also create a test queue within the ActiveMQ broker. The ActiveMQ broker runs a web-console when it boots that is accessible from http://127.0.0.1:8161
Access the web console and login with the default credentials admin admin. The create a queue called TESTQ
Deploy the ActiveMQ RAR to Payara ServerNow we need to set up the connectivity between Payara Server and ActiveMQ. To do this we use as a standard JavaEE resource adapter. Thankfully the ActiveMQ team provide us one which can be downloaded from Maven Central at
wget http://search.maven.org/remotecontent?filepath=org/apache/activemq/activemq-rar/5.15.11/activemq-rar-5.15.11.rar -O activemq-rar-5.15.11.rar
The rar then needs to be deployed to Payara Server.
Once it has been deployed as a Java EE connector we need to configure the connector to connect to our default broker. You can also use asadmin to deploy the rar file.
asadmin deploy --type rar activemq-rar-5.12.0.rar
Configuring the ActiveMQ ConnectorFirst we need to create a Configuration for the resource adapter to refer to our ActiveMQ broker.
The key settings are ServerURL, UserName and Password. For an out of the box ActiveMQ configuration they should be set to;
The resource adapter configuration can also be created using asadmin via;
asadmin create-resource-adapter-config --property ServerUrl=tcp://127.0.0.1:61616:UserName='admin':Password='admin' activemq-rar-5.12.0
Now we need to create a JMS connection pool as shown in the screen shots below.
This can also be created using asadmin;
asadmin create-connector-connection-pool --raname activemq-rar-5.12.0 --connectiondefinition javax.jms.ConnectionFactory --ping true --isconnectvalidatereq true jms/myConnectionPoolFinally we create a JNDI mapping for our JMS Connection pool by creating a JMS resource.
Again we can also create the JNDI mapping via asadmin;
asadmin create-connector-resource --poolname jms/myConnectionPool jms/myConnectionFactory
Now we have the connection factory configured we can also create a JMS mapping to our Queue
asadmin create-admin-object --raname activemq-rar-5.12.0 --restype javax.jms.Queue --property PhysicalName=TESTQ jms/TESTQWe are now set to write our MDB.
Writing our MDB
Below shows the code for an MDB that listens on the queue configured in this blog within ActiveMQ. You can see that both the resource adapter, the physical name of the queue and the JNDI name are set as Activation Properties of the MDB. Note the Activation Property resourceAdapter is only available from Payara Server 188.8.131.52 onwards. If you have a version of Glassish or Payara Server prior to that you will need to specify the resource adapter in your glassfish-ejb.xml deployment descriptor.
If you build and deploy this MDB you will see in the Active MQ console that we have a single consumer on our queue.
Once you have sent the message you can check the server log as our code just printed out the received message.
That's it. You have successfully connected Active MQ to Payara Server. Give it a try: