How To Run ActiveMQ in the Cloud - Archived

Messaging is one of the most important components required for building enterprise applications. Apache ActiveMQ is a popular and open-source messaging broker for remote communication between  applications using the JMS specification. ActiveMQ is written in Java, but provides client libraries for many languages. This allows different applications written in different languages to communicate among each other. Apart from language support, ActiveMQ also exposes a REST API which producers and consumers can use to interact with each other. In this blog post, we will first setup ActiveMQ on OpenShift and then I will demo how you can use the ActiveMQ REST API to publish and consume messages.

Step 1: Sign up for an OpenShift Account

If you don’t already have an OpenShift account, head on over to the website and sign up. It is completely free and Red Hat gives every user three free Gears on which to run your applications. At the time of this writing, the combined resources allocated for each user is 1.5 GB of memory and 3 GB of disk space.

Step 2: Install the rhc client tools on your machine

The OpenShift client tools are written in a very popular programming language called Ruby. With OSX 10.6 or later and most Linux distributions, ruby is installed by default so installing the client tools is a snap. Simply issue the following command on your terminal application:

sudo gem install rhc

Step 3 : Setting up OpenShift

The rhc client tool makes it very easy to setup your openshift instance with ssh keys, git and your applications namespace. The namespace is a unique name per user which becomes part of your application url. For example, if your namespace is t20 and application name is activemq then url of the application will be https://activemq-t20.rhcloud.com/. The command is shown below.

rhc setup -l openshift_login

Step 4 – ActiveMQ on OpenShift in two minutes

Note : GitHub repository for blog post quickstart

You can get the code from github using the following URL:

https://github.com/shekhargulati/activemq-openshift-quickstart

To install ActiveMQ on OpenShift, we will need to create a diy application type. ActiveMQ comes bundled with a jetty instance so we will be using that to expose REST services.

rhc app create activemq diy-0.1 --from-code=git://github.com/shekhargulati/activemq-openshift-quickstart.git

Wait for a minutes to let activemq start. Then, you can see ActiveMQ running at http://activemq-domainname.rhcloud.com. You can view the admin page at http://activemq-{yourNamespace}.rhcloud.com/admin/.

Examining ActiveMQ in the Cloud

Now that you have an ActiveMQ instance running in cloud, lets look at what have we have done.

  • We pulled code from a github repository. As you can see in the diy directory, under activemq folder their exists a folder called activemq. This is the latest ActiveMQ version i.e. 5.6.0. This is the extracted tarball which you can get from ActiveMQ web site. There are only two files which are updated in the activemq folder — conf/activemq.xml and conf/jetty.xml. The activemq.xml contains all the configuration related to which transport connectors, persistence provider, network connector to use etc. In our case we use http transport connector as shown below.
<transportConnectors>
        <transportConnector name="http" uri="http://OPENSHIFT_INTERNAL_IP:15005"/>
</transportConnectors>
  • The jetty.xml file contains configuration items related to setting up the jetty server. We changed the configuration to make jetty run on port 8080 as shown below.
<bean id="Server" class="org.eclipse.jetty.server.Server" init-method="start"
        destroy-method="stop">
 
        <property name="connectors">
            <list>
                <bean id="Connector" class="org.eclipse.jetty.server.nio.SelectChannelConnector">
                    <property name="port" value="8080" />
                    <property name="host" value="OPENSHIFT_INTERNAL_IP"/>
                </bean>
            </list>
        </property>
 
        <property name="handler">
            <bean id="handlers" class="org.eclipse.jetty.server.handler.HandlerCollection">
                <property name="handlers">
                    <list>
                        <ref bean="contexts" />
                        <ref bean="securityHandler" />
                    </list>
                </property>
            </bean>
        </property>
 
    </bean>
  • The next important change exists in start.sh script which sets the ACTIVEMQ_DATA environment variable to OPENSHIFT_DATA_DIR. Edit the activemq.xml and jetty.xml file and add the IP address and finally start the activemq server . The start.sh is shown below.
#!/bin/bash
# The logic to start up your application should be put in this
# script. The application will work only if it binds to
# $OPENSHIFT_INTERNAL_IP:8080
set -x
export ACTIVEMQ_DATA=$OPENSHIFT_DATA_DIR
cd $OPENSHIFT_RUNTIME_DIR/repo/diy/activemq
sed -ig 's/OPENSHIFT_INTERNAL_IP/'$OPENSHIFT_INTERNAL_IP'/g' conf/activemq.xml
sed -ig 's/OPENSHIFT_INTERNAL_IP/'$OPENSHIFT_INTERNAL_IP'/g' conf/jetty.xml
bin/activemq start

Step 5 – Lets use it — Sending messages over the REST API

Now that we have ActiveMQ running on OpenShift, lets use it to send messages. We will use the GNU wget program as a consumer and curl as a message producer. The curl producer would make a post request to a topic and the wget consumer will be listening to the topic. The example shown below is taken from “ActiveMQ in Action” book.

To start the Wget consumer type the following command:

wget -O message.txt --save-cookies cookies.txt --load-cookies cookies.txt --keep-session-cookies http://activemq-domainname.rhcloud.com/demo/message/OPENSHIFT/DEMO?type=topic

The command shown above instructs wget to receive the next available messsage from the OPENSHIFT.DEMO topic and save it to message.txt file.

The curl producer can send the message to OPENSHIFT.DEMO topic as shown below.

curl -d "body=message" http://activemq-domainname.rhcloud.com/demo/message/OPENSHIFT/DEMO?type=topic

That’s it for this blog. Happy Messaging !!!!

What’s Next?

Categories
News, OpenShift Online
Tags
, ,
Comments are closed.