Friday, January 20, 2012

ActiveMQ: Configuring Multiple Instances of the Web Console

When configuring multiple instances of ActiveMQ brokers in master slave configurations it may be useful to configure the web console for each broker in a single Jetty instance.   This will allow you to use a single URL to access each of the brokers by simply changing the URL context.

We will configure the ActiveMQ brokers using two master slave pairs as follows:

AMQ1M - AMQ1 Master
AMQ1S - AMQ1 Slave

The web console to service these brokers will be configured as: http://localhost:8080/amq1

In the event that AMQ1M should fail, this url will then failover to AMQ1S.

AMQ2M - AMQ2 Master
AMQ2S - AMQ2 Slave

The web console to service these brokers will be configured as: http://localhost:8080/amq2

In the event that AMQ2M should fail, this url will then failover to AMQ2S.

This configuration allows the URL to be descriptive by identifying the brokers it is servicing while keeping port configuration to a minimum by utilizing a single Jetty instance.

So with the concept out of the way lets look at the configuration that will make this happen.

Note: In this example I will be using the FuseSource distribution of ActiveMQ.  Below is a list of binaries used to setup this example.  However, the instructions provided should work with both the Apache and FuseSource distributions.

  • apache-activemq-5.5.1-fuse-01-13
  • activemq-web-console-5.5.1-fuse-01-13.war
  • jetty-distribution-7.5.4.v20111024.zip

Step 1.
Grab a Jetty distribution from the Eclipse Jetty download site.  Once downloaded, extract the zip or tar.

Step 2.
Add the activemq-all-x.x.x-fuse-xx-xx.jar to the Jetty /lib directory.

Step 3.
Down the ActiveMQ web console war.  This can be obtained via the FuseSource repo: activemq-web-console.  Explode the activemq-web-console-x.x.x-fuse-xx-xx.war to Jetty's web apps directory in a web context named amq1.  Repeat this, and create web context amq2.  Once the war is exploded we will need to add some JNDI configuration to each webapp's web descriptor (WEB-INF/web.xml).  At this point you should have a directory structure that resembles the following:

webapps
     |_amq1
     |_amq2

Step 5.
For each of the web application's (amq1 and amq2) web descriptor files (WEB-INF/web.xml) we are going to add the following JNDI configuration:

 <!-- ========================================================== -->  
 <!-- JNDI Config -->  
 <!-- ========================================================== -->  
 <resource-ref>  
     <res-ref-name>jms/connectionFactory</res-ref-name>  
     <res-type>javax.jms.ConnectionFactory</res-type>  
     <res-auth>Container</res-auth>  
 </resource-ref>  
 <resource-ref>  
      <res-ref-name>jmx/url</res-ref-name>  
      <res-type>java.lang.String</res-type>  
 </resource-ref>  
 <resource-ref>  
      <res-ref-name>jmx/user</res-ref-name>  
      <res-type>java.lang.String</res-type>  
 </resource-ref>  
 <resource-ref>  
      <res-ref-name>jmx/password</res-ref-name>  
      <res-type>java.lang.String</res-type>  
 </resource-ref>  

Step 6.
Then create jetty-env.xml files for each of the web applications.  The jetty-env.xml is used to configure the JNDI resources specific to the application and the file will reside in the WEB-INF/ directory.  In each file, the ConnectionFactory and JMX URL will be specified.

For amq1 the jetty-env.xml will be configured as follows:

 <?xml version="1.0"?>  
 <!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">  
 <Configure id="amq1" class="org.eclipse.jetty.webapp.WebAppContext">  
   <New id="connectionFactory" class="org.eclipse.jetty.plus.jndi.Resource">  
     <Arg><Ref id="amq1"/></Arg>  
     <Arg>jms/connectionFactory</Arg>  
     <Arg>  
       <New class="org.apache.activemq.ActiveMQConnectionFactory">  
         <Arg>admin</Arg>  
         <Arg>admin</Arg>  
         <Arg>failover:(tcp://localhost:61616,tcp://localhost:62616)</Arg>  
       </New>  
     </Arg>  
   </New>  
   <New id="url" class="org.eclipse.jetty.plus.jndi.Resource">  
     <Arg><Ref id="amq1"/></Arg>  
     <Arg>jmx/url</Arg>  
     <Arg>  
       <New class="java.lang.String">  
         <Arg>service:jmx:rmi:///jndi/rmi://localhost:1199/jmxrmi,service:jmx:rmi:///jndi/rmi://localhost:1299/jmxrmi</Arg>  
       </New>  
     </Arg>  
   </New>  
   <New id="jmxuser" class="org.eclipse.jetty.plus.jndi.Resource">  
     <Arg><Ref id="amq1"/></Arg>  
     <Arg>jmx/user</Arg>  
     <Arg>  
       <New class="java.lang.String">  
          <Arg>admin</Arg>  
       </New>  
     </Arg>  
   </New>  
   <New id="jmxpassword" class="org.eclipse.jetty.plus.jndi.Resource">  
     <Arg><Ref id="amq1"/></Arg>  
     <Arg>jmx/password</Arg>  
     <Arg>  
       <New class="java.lang.String">  
          <Arg>admin</Arg>  
       </New>  
     </Arg>  
   </New>
 </Configure>  

For amq2 the jetty-env.xml will be configured as follows:

 <?xml version="1.0"?>  
 <!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">  
 <Configure id="amq2" class="org.eclipse.jetty.webapp.WebAppContext">  
   <New id="connectionFactory" class="org.eclipse.jetty.plus.jndi.Resource">  
     <Arg><Ref id="amq2"/></Arg>  
     <Arg>jms/connectionFactory</Arg>  
     <Arg>  
       <New class="org.apache.activemq.ActiveMQConnectionFactory">
         <Arg>admin</Arg>  
         <Arg>admin</Arg>   
         <Arg>failover:(tcp://localhost:63616,tcp://localhost:64616)</Arg>  
       </New>  
     </Arg>  
   </New>  
   <New id="url" class="org.eclipse.jetty.plus.jndi.Resource">  
     <Arg><Ref id="amq2"/></Arg>  
     <Arg>jmx/url</Arg>  
     <Arg>  
       <New class="java.lang.String">  
         <Arg>service:jmx:rmi:///jndi/rmi://localhost:1399/jmxrmi,service:jmx:rmi:///jndi/rmi://localhost:1499/jmxrmi</Arg>  
       </New>  
     </Arg>  
   </New>  
   <New id="jmxuser" class="org.eclipse.jetty.plus.jndi.Resource">  
     <Arg><Ref id="amq2"/></Arg>  
     <Arg>jmx/user</Arg>  
     <Arg>  
       <New class="java.lang.String">  
          <Arg>admin</Arg>  
       </New>  
     </Arg>  
   </New>  
   <New id="jmxpassword" class="org.eclipse.jetty.plus.jndi.Resource">  
     <Arg><Ref id="amq2"/></Arg>  
     <Arg>jmx/password</Arg>  
     <Arg>  
       <New class="java.lang.String">  
          <Arg>admin</Arg>  
       </New>  
     </Arg>  
   </New>  
 </Configure>  
Step 7.
Before starting the Jetty instance we need to make sure it is enabled to use JNDI. This can be done by editing the [jetty]/start.ini file. The following items need to be added to the options list: plus and annotations. This ensures these jars are loaded. You also need to add etc/jetty-plus.xml to configurations files near the bottom of the file.  The results of these changes should look as follows in the start.ini file:

 #===========================================================  
 # Start classpath OPTIONS.  
 # These control what classes are on the classpath  
 # for a full listing do  
 #  java -jar start.jar --list-options  
 #-----------------------------------------------------------  
 OPTIONS=Server,jsp,jmx,resources,websocket,ext,plus,annotations  
 #-----------------------------------------------------------  
 #===========================================================  
 # Configuration files.  
 # For a full list of available configuration files do  
 #  java -jar start.jar --help  
 #-----------------------------------------------------------  
 #etc/jetty-jmx.xml  
 etc/jetty.xml  
 etc/jetty-plus.xml  
 # etc/jetty-ssl.xml  
 # etc/jetty-requestlog.xml  
 etc/jetty-deploy.xml  
 #etc/jetty-overlay.xml  
 etc/jetty-webapps.xml  
 etc/jetty-contexts.xml  
 etc/jetty-testrealm.xml  
 #===========================================================  


Step 8.
Now we can start jetty using the following command:
java -Dwebconsole.type=jndi -jar start.jar

This command informs the brokers' web consoles to use the JNDI properties to provision the ConnectionFactory and the JMX connection.

Step 9.
In addition to the master/slave configuration required for the ActiveMQ brokers, which I will cover in my next post, we also need to disable the Jetty configuration.  The default configuration file for ActiveMQ is activemq.xml located in the /conf directory of the broker.  At the bottom of this file you will find an import tag that is used to import the resource: jetty.xml.  This should be commented out as follows:


<!--<import resource="jetty.xml"/>-->


Step 10.
Now we are ready to start our master/slave configuration of brokers.  For each broker run the following command:


./bin/activemq console


The Result of This Configuration
Now lets take a look at how the web console and Jetty behave with this configuration.  In your browser navigate to http://localhost:8080/amq1.  You should see that this connects you to broker amq1M which is the master in the amq1 pair.

ActiveMQ - amq1M (Master)

Now if we go to the terminal where the broker amq1M was started and issue a 'control c' command to kill it, when the browser is refreshed the connection will failover to broker amq1S which is the slave in the amq1 pair.

ActiveMQ - amq1S (Slave)

To check on the amq2 broker pairs, navigate to http://localhost:8080/amq2.  You should see this connects you to amq2M which is the master in the amq2 pair of brokers.

ActiveMQ - amq2M (Master)

Again, if we go to the terminal where the broker amq2M was started and issue a 'control c' command to kill it, when the browser is refreshed the connection will failover to broker amq2S which is the slave in the amq2 pair.

ActiveMQ - amq2S (Slave)
Summary
Using this configuration to set up multiple instances of ActiveMQ broker pairs in a single Jetty instance you can see how it could simplify managing and accessing the web consoles for all brokers as the environment scales to support many instances of ActiveMQ master/slave pairs.  This eliminates the need for each individual broker to have separate URLs and ports to access the web console.  In a followup to this post I will show how the brokers were configured to support a master/slave configuration.

2 comments:

  1. nice article!! A couple of questions. 1) Is there a way to put in the username/password for JMX authentication to the brokers? 2)How would you edit the jetty.sh to use your start up parameters of:
    java -Dwebconsole.type=jndi -jar start.jar?
    so that jetty could be run as a linux service.

    ReplyDelete
    Replies
    1. Hi Paul,

      I have updated the blog to include configuration that can be used when brokers are secured using the authentication plugin. Note the addition of the username and password to the connection factory and the addition of the jmx/user and jmx/password JNDI properties. I have also had a look at the jetty.sh script and you could add the -Dwebconsole.type=jndi Java system property to the JAVA_OPTIONS variable that is being used for several other Java system properties in that file.

      Delete