Tuesday, May 12, 2009

Developing Flex Applications with Java Middle-Tier

In my job we are using Flex with Java to develop web applications that support the activity of the agency I'm working for. It's my first experience with Flex and I'd say, I'm duly impressed. I had experience of developing using GWT and I must say, that Flex is an equally good tool.

A Simple Way
Just follow this tutorial.

Tools Used
We use the following toolset:

  • eclipse with FlexBuilder plugin (plugin is not free)
  • jBoss
  • Java for back-end
  • LCDS to communicate between Flex and Java (not free, but there are free alternatives: BlazeDS and the LCDS Express is also free, I think).
  • Cairngorm pattern for Flex client structure

Tips and Gotchas

  • A great intro to Cairngorm (and Flex itself) is one by David Tucker.
  • He is using ColdFusion for the back-end, so switch to this or this for how to do it with Java.
The Cairngorm pattern for calling a remote service is a bit complex. Here's how it goes:
  • In the main mxml file (the one where you define mx:Application): instantiate the ServiceLocator: . The id "services" is irrelevant, it seems. And instantiate the FrontController.
  • Let say, the command LOGIN is to be handled by the back-end. Dispatch the command by initiating the LoginEvent and calling dispatch() on it.
  • Each event is mapped onto a command in the FrontController. Let say, in this case, onto a LoginCommand.
  • In loginCommand, instantiate a delegate, say LoginDelegate and call the login(event)
  • In LoginDelegate, obtain the service proxy using:
    and call the login(args) on it.
  • Now, two additional files need to be setup: Services.mxml and remoting-config.xml
  • In the first one, Services.mxml, define a mx:RemoteObject with id="myRemoteObject" and the destination="myRemoteClass".
  • In the second, the remoting-config.xml, define a destination with id="myRemoteClass" and the Java class package+classname in the source tag.
  • In the Flex Server properties (access through project properties), set the Root Context as the /YourProjectName.
If you need to pass an object between client and the server, create two corresponding value objects (usually named *VO, like LoginVO), one in ActionScript in flex branch and one in Java branch:

  • The ActionScript version, needs to have an annotation above the class definition: [RemoteClass(alias=org.sjb.LoginVO)].
  • The Java version needs to be public and have public setters.
If you want to debug not only Flex (which is done the usual "eclipse" way), stop the JBoss server and start it in Debug mode. That's all that is needed.

Altogether, using Flex with Java is very simple. Enjoy it!

No comments: