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

No comments:

Post a Comment