Monday, February 15, 2010

JBoss Message Clustering by Example

It's amazing how the JBoss Team put together an easy way to do JMS Clustering. I'll start with an easy example, creating a Queue named "MyClusteredQueue". In this example I'm using JBoss AS 5.1. and two computers connected on the same network, with these IP's:

- Computer A: 192.168.0.143
- Computer B: 192.168.0.210

So, here are the steps:

1) Install the JBoss on both computers. We are going to use the "all" configuration for both computers.

2) We create our Queue on both servers.

Go to $JBOSS_HOME/server/all/deploy/messaging/  and edit the destinations-service.xml file. Add the MyClusteredQueue before the last server tag. It looks like this:










This is how you add a Queue to the JBoss, and the people how are familiar with this, the only new thing is to add the attribute "Clustered".  This step must be on both computers. At the end of the article you can find the files.

3) Crate the MDB that will consume the messages, and deploy it into the two servers.
(The code is on the Resources, see at the end of the page).

 



4) Start the jboss with the following options:

Computer A:
$ cd $JBOSS_HOME/bin
$ ./run.sh -c all -b 192.168.0.143 -Djboss.messaging.ServerPeerID=1

Computer B:
$ cd $JBOSS_HOME/bin
$ ./run.sh -c all -b 192.168.0.210 -Djboss.messaging.ServerPeerID=2


It is necesary to give an ID to each server and this is accomplished with this directive:
-Djboss.messaging.ServerPeerID

when you start the jboss on computer A, you should see these on the logs:

 
and once you start the jboss on computer B, you should see the following logs:

And the computer A, will be shown the new change with these logs:

5) Now it's time to send a Message to the Queue. To accomplish this it's necessary to change the connection factory to "ClusteredConnectionFactory". Also on the jndi.properties file it's necessary to add the two computers ip's separated by comma to the java.naming.provider.url property.

java.naming.provider.url=192.168.0.143:1099,192.168.0.210:1099


The client that I wrote (you can find it on the Resources) is a web application. You need to deploy it in any computer, in my case I deployed it on the computer A. And you can access it through this URL: http://192.168.0.143:8080/JMSWeb/ (just modify the IP where the JMSWeb.war was deployed).

You will see this:

In order to use it, just put any message and (if you want) you can increase the times that the message will be sent. If you put 10 times, and the message "Hello JMS Clustering", you will see that the message is clustered and the two servers process the Queue.




No comments:

Post a Comment