Wednesday, July 22, 2009

Integrating Apache ActiveMQ with WSO2 ESB

When going through some of the recent posts in the WSO2 ESB user forum, I got the impression that some folks are having trouble getting Apache ActiveMQ to work with WSO2 ESB. So today I'm going to explain how to integrate Apache ActiveMQ with WSO2 ESB and run one of the JMS sample scenarios.

I will be using WSO2 ESB 2.1 and Apache ActiveMQ 5.2. First download WSO2 ESB 2.1 and Apache ActiveMQ 5.2 from the respective websites if you already haven't done so. Apache ActiveMQ has different distributions for Windows platform and Unix/Linux platform. So remember to download the distribution that matches your target platform. To run the sample scenario described here you will also require Apache ANT. Once you have extracted the downloaded archives to some location in your local disk. Let's refer to WSO2 ESB installation directory as ESB_HOME and ActiveMQ installation directory as AMQ_HOME.

Now we are all set to go.....

First go to AMQ_HOME/bin directory and start the ActiveMQ broker. On Linux you simply have to execute the script named 'activemq'.

Then you need to copy the following 3 jar files into ESB_HOME/repository/components/lib directory. (With WSO2 ESB 2.1 this is the location where most of the 3rd party dependencies should be deployed)
  • activemq-core-5.2.0.jar
  • geronimo-j2ee-management_1.0_spec-1.0.jar
  • geronimo-jms_1.1_spec-1.1.1.jar
You will find all these jars in AMQ_HOME/lib directory.

Then open up ESB_HOME/conf/axis2.xml file in your favorite text editor and uncomment the JMS listener configuration. JMS listener configuration should look something like this.

<transportReceiver name="jms" class="org.apache.axis2.transport.jms.JMSListener">
<parameter name="myTopicConnectionFactory" locked="false">
<parameter name="java.naming.factory.initial" locked="false">
org.apache.activemq.jndi.ActiveMQInitialContextFactory
</parameter>
<parameter name="java.naming.provider.url" locked="false">
tcp://localhost:61616
</parameter>
<parameter name="transport.jms.ConnectionFactoryJNDIName" locked="false">
TopicConnectionFactory
</parameter>
<parameter name="transport.jms.ConnectionFactoryType" locked="false">topic</parameter>
</parameter>

<parameter name="myQueueConnectionFactory" locked="false">
<parameter name="java.naming.factory.initial" locked="false">
org.apache.activemq.jndi.ActiveMQInitialContextFactory
</parameter>
<parameter name="java.naming.provider.url" locked="false">
tcp://localhost:61616
</parameter>
<parameter name="transport.jms.ConnectionFactoryJNDIName" locked="false">
QueueConnectionFactory
</parameter>
<parameter name="transport.jms.ConnectionFactoryType" locked="false">queue</parameter>
</parameter>

<parameter name="default" locked="false">
<parameter name="java.naming.factory.initial" locked="false">
org.apache.activemq.jndi.ActiveMQInitialContextFactory
</parameter>
<parameter name="java.naming.provider.url" locked="false">
tcp://localhost:61616
</parameter>
<parameter name="transport.jms.ConnectionFactoryJNDIName" locked="false">
QueueConnectionFactory
</parameter>
<parameter name="transport.jms.ConnectionFactoryType" locked="false">queue</parameter>
</parameter>
</transportReceiver>
Now go to ESB_HOME/bin and start sample 250 which demonstrates how to receive messages over JMS and forward them over HTTP. To start the sample go to ESB_HOME/bin and run the command ./wso2esb-samples.sh -sn 250 if you are on Unix/Linux or run the command ./wso2esb-samples.bat -sn 250 if you are on Windows. This will bring up WSO2 ESB with sample 250 configuration.

The ESB startup may take some time depending on your system. However note the following lines on the console when it starts up.
INFO - JMSConnectionFactory JMS ConnectionFactory : myTopicConnectionFactory initialized
INFO - JMSConnectionFactory JMS ConnectionFactory : myQueueConnectionFactory initialized
INFO - JMSConnectionFactory JMS ConnectionFactory : default initialized
INFO - JMSListener JMS Transport Receiver/Listener initialized...
This confirms that the JMS transport has successfully started by connecting to the ActiveMQ broker. When the ESB has fully started up you should get a message similar to the following line on the console.
INFO - StartupFinalizerServiceComponent WSO2 Carbon started in 33 sec
Now we are ready to run the sample. Go to ESB_HOME/samples/axis2Server/src/SimpleStockQuoteService and run the command ant to build the sample service and deploy in the sample Axis2 server that comes with WSO2 ESB (This is where you need Apache ANT. So make sure you have properly installed ANT before trying to build the service and proceed any further.). Then switch back to ESB_HOME/samples/axis2Server and start the sample server by running axis2server.sh or axis2server.bat startup script.

Finally to run the sample client head over to ESB_HOME/samples/axis2Client directory and run the following command.
ant jmsclient -Djms_type=pox -Djms_dest=dynamicQueues/StockQuoteProxy -Djms_payload=MSFT
This will send a message to a JMS queue named StockQuoteProxy. The ESB will receive the message from the queue and forward it to the sample Axis2 server over HTTP. When the Axis2 server is done with processing the message it will print the following line on the console.
samples.services.SimpleStockQuoteService  :: Accepted order for : 9351 stocks of MSFT at $ 158.6569470528259
Congratulations! You just got Apache ActiveMQ integrated and working with WSO2 ESB. Now that wasn't too hard, was it? The key thing you have to remember is the way 3rd party dependencies are deployed into WSO2 ESB. In older versions of WSO2 ESB you had to copy the jar files into ESB_HOME/lib. But that does not work with the latest version of WSO2 ESB. The deployment model of WSO2 ESB has slightly changed with the introduction of WSO2 Carbon and Equinox P2 into the ESB.

So that's all about getting ActiveMQ to work with WSO2 ESB. With this knowledge now you may try other JMS related samples that come with WSO2 ESB.

4 comments:

Srideep said...

nice guide...
can u post a similar guide for how to integrate banzai (quickfixj) and wso2 esb?

george said...

In this case , does the client gets the response. As you said only axisserver and esb console show the processing. How to get the response on the client side

Hiranya Jayathilaka said...

Hi George,

The sample described here is a one-way scenario. Meaning the server doesn't send a response back. But WSO2 ESB does support two way messaging with JMS. A sample is available at http://wso2.org/project/esb/java/3.0.0/docs/samples/transport_samples.html#Sample264

Anonymous said...

Excellent stuff.