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.




Saturday, February 6, 2010

Flex - BlazeDS with Grails, sending messages from Grails to Flex - BlazeDS

I started several weeks ago to see what we can do with Grails and its Enterprise side, and because where I'm working right now it's needed to do some messaging from the back-end to the clients (Flex clients), and well, I did some modifications to the flex/grails plug-in (developed by Marcel Overdijk) in order to be able to send messages from grails to Flex (consumer).

Assuming that we already have our domain, controllers and maybe some views,  the only thing we need to do is install the flex plug-in, do some small modifications to the source (flex plug-in), create a helper class (to send the message), create a service (this will defined our destination), add the messaging service tag to the flex configuration (services-config.xml) and test it on your Flex application. For these steps I'm using Grails IDE on Eclipse.

So here are the steps:

1) Install the Flex plug-in:
(Using the Grails IDE on eclipse, you can bring up the grails command prompt with Ctrl+Alt+G for Windows or Cmd+Alt+G for Mac)






This will install the flex plug-in and it will give the following structure








2) Modify the Flex plug-in source code:

The files to modify will be GrailsBootstrapService.java and FlexUtils.java

GrailsBootstrapService.java, the bootstrap service will help to create dynamically the destination(s). 

(This is the final version).































I added the lines 45 to 47.






FlexUtils.java

Here, I added several lines, and I know there is a better way to implement this (maybe put a Factory Pattern) to create the correct service.


a) Two new constants:






b) And these methods:
























3) Create a Helper Class:

This class will send any message to any destination (provided by the service created).
Just create a new Groovy class into the src/groovy folder.


































In this class, this method can be overloaded so we can pass more information, like Headers, IDs, etc. Take a look into the AsyncMessage.java (from BlazeDS API) for more information.


4) Create the Service:

Create the service using the grails command. And put the property identifier (static expose = ['flex-messaging'])







Final UpdateMessageService.groovy










The Bootstrap will create the updateMessageService destination

5) Add the Messaging service tag to the Flex configuration:
Its necessary to add new tags to the services-config.xml located on the web-app/WEB-INF/flex





















This tag will be added to the existing services tag







And this is the complete file: services-config.xml

 




















6) Test it in Flex
To Test the Flex client we need first to create a consumer tag and from any controller (from Grails) use the helper class to send a message to the updateMessageService destination.
















On Grails (in any controller or any other source where the message is necessary), just use the Helper class to send the message:





Resources: Files

Notes:
1st Draft version: 0.1