ActiveMQ
September 11, 2019

References

What is JMS?

Java Message Service (JMS) is an application program interface (API) from Sun Microsystems that supports the messaging communication between computers / applications / software components in a network.

The messages involved exchange crucial data between computers – rather than only between users – and contain information such as event notification and service requests.

This API allows Java based applications to create, send, receive and read messages without exposing any client architecture information. JMS architecture is very loosely coupled unlike RMI where the remote method name is needed in order to communicate.

Two basic architecture is used for communication:

i. QUEUE – This is a kind of pipeline connection between two clients. For example, One-To-One chat where one client sends a message and the other one receives that.

ii. TOPIC – This can be considered as a common space where all the clients connected to the particular topics can communicate with each other. For example, Group chat where one client sends a message and all the connected clients receives that.

(Reference: https://docs.oracle.com/javaee/6/tutorial/doc/bncdq.html)

JMS Topic vs Queues

JMS Topic vs Queues

What is Apache ActiveMQ?

ActiveMQ is an open-source, messaging software which serve as the backbone for an architecture of distributed applications built upon messaging. ActiveMQ completely implements Java Messaging Service 1.1 (JMS). In order to run a JMS application we need ActiveMQ instance running on a machine which will be treated as a JMS server.

(Reference: http://activemq.apache.org)

Apache ActiveMQ installation

To install ActiveMQ

1
brew install activemq

To have launchd start activemq now and restart at login:

1
brew services start activemq

Or, if you don’t want/need a background service you can just run:

1
activemq start

To run ActiveMQ Console

1
activemq console

The ActiveMQ broker runs a web-console when it boots that is accessible from http://127.0.0.1:8161. Username is admin and password is admin.

Deploy the ActiveMQ RAR to GlassFish Server

To copy activemq-rar from maven repository to current directory

1
wget https://repo1.maven.org/maven2/org/apache/activemq/activemq-rar/5.15.10/activemq-rar-5.15.10.rar -O activemq-rar-5.15.10.rar

To deploy rar file to GlassFish Server

1
asadmin deploy --type rar activemq-rar-5.15.10.rar

Once it has been deploy as a Java EE connector we need to configure the connector to connect to our default broker.

Configuring the ActiveMQ Connector

First 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;

| ServerURL | tcp://127.0.0.1:61616 | | UserName | admin | | Password | admin |

The resource adapter configuration, JMS connection pool, JMS resource (to create JNDI mapping for JMS Connection pool), JMS mapping to Queue can be created using glassfish-resources.xml or asadmin;

Using asadmin administrator tool

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#Resource adapter configuration
asadmin create-resource-adapter-config  --property ServerUrl=tcp://127.0.0.1:61616:UserName='admin':Password='admin' activemq-rar-5.15.10

#JMS connection pool
asadmin create-connector-connection-pool  --raname activemq-rar-5.15.10 --connectiondefinition javax.jms.ConnectionFactory --ping true --isconnectvalidatereq true jms/myConnectionPool

#JNDI mapping (connection factory)
asadmin create-connector-resource --poolname jms/myConnectionPool jms/myConnectionFactory

#JNDI mapping (JMS mapping to Queue)
asadmin create-admin-object --raname activemq-rar-5.15.10 --restype javax.jms.Queue --property PhysicalName=TESTQ jms/TESTQ

Using glassfish-resources.xml

1
2
3
4
5
6
7
8
<resource-adapter-config name="activemq-rar-5.15.10" 
                          thread-pool-ids="thread-pool-1" 
                          resource-adapter-name="activemq-rar-5.15.10">
    <property name="ServerUrl" value="tcp://localhost:61616"/>
    <property name="UserName" value="admin"/>
    <property name="Password" value="admin"/>
    <property name="UseInboundSession" value="false"/>
</resource-adapter-config>
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
<connector-connection-pool associate-with-thread="false" 
                            connection-creation-retry-attempts="0" 
                            connection-creation-retry-interval-in-seconds="10" 
                            connection-definition-name="javax.jms.ConnectionFactory" 
                            connection-leak-reclaim="false" 
                            connection-leak-timeout-in-seconds="0" 
                            fail-all-connections="false" 
                            idle-timeout-in-seconds="300" 
                            is-connection-validation-required="true" 
                            lazy-connection-association="false" 
                            lazy-connection-enlistment="false" 
                            match-connections="true" 
                            max-connection-usage-count="0" 
                            max-pool-size="32" 
                            max-wait-time-in-millis="60000" 
                            name="jms/myConnectionPool" 
                            ping="true" 
                            pool-resize-quantity="2" 
                            pooling="true" 
                            resource-adapter-name="activemq-rar-5.15.10" 
                            steady-pool-size="8" 
                            validate-atmost-once-period-in-seconds="0"/>
1
2
3
4
5
6
7
<connector-resource enabled="true" 
                    jndi-name="jms/myConnectionFactory" 
                    object-type="user" 
                    pool-name="jms/myConnectionPool">
    <description>My Connection Factory</description>
    <property name="Name" value="myFactory"/>
</connector-resource>
1
2
3
4
5
6
7
8
9
<admin-object-resource enabled="true" 
                        jndi-name="jms/myAMQ" 
                        object-type="user" 
                        res-adapter="activemq-rar-5.15.10" 
                        res-type="javax.jms.Queue">
    <description>My JMS Queue</description>
    <property name="Name" value="myAMQ"/>
    <property name="PhysicalName" value="myAMQ"/>     
</admin-object-resource>