Flex Supports Spring – Are You Ready to Skip Web 2.0?

We’ve a lot of experiences now with Web 2.0 technologies based on AJAX and JSF. Although, we hold pretty modern, almost standards-based stuff in our hands the maintenance hell for the markup-based presentation layer is pretty lively. The costs are still tremendous in comparison to similar desktop developments.

To be a successful successor for AJAX/JSF development the Web 3.0 frameworks have to be

  • as ergonomic as current desktop applications when using their user interface components
  • compatible with the Web browsers already in use and based on widely-accepted plugins
  • faster in development through easier coding with lesser lines of code
  • easier to maintain and less complex in deployment
  • seamless integrated with Spring to support the new backend standard

Flex now supports all this, even with Open Source releases.

BlazeDS for Communication

Flex and Java are different in their programming model. So, we need an adapter to establish an object-to-object communication. In the past Adobe already offered their pretty expensive LiveCycle Data Services for communication with J2EE environments. BlazeDS now delivers the important parts of these services as Open Source.

There are other Open Source projects, like GraniteDS, Cinnamon or WebORB. So, we have real choice in connecting Flex with Spring. Although, the alternatives are more flexible to use, GraniteDS e.g. also supports Acegi, the Adobe committment to BlazeDS delivers better planning reliability for enterprise projects. Getting this directly from “the source” helps to persuade management for a first prototype ;-).

Different Ways to Communicate

BlazeDS allows to communication via

  • Messaging Services
  • RPC Services
    • HTTPService
    • WebService
    • RemoteObject

From the BlazeDS developer guide:

Messaging Services – Provides messaging services for collaborative and real-time applications. Lets you create applications that can send messages to and receive messages from other applications, including Flex applications and Java Message Service (JMS) applications.

RPC Services – Provides a call and response model for accessing external data. Lets you create applications that make asynchronous requests to remote services that process the requests, and then return data to your Flex application [Fig. 1].

BlazeDS - RPC Services [Source: BlazeDS Developer Guide]
Fig. 1: BlazeDS – RPC Services
[Source: BlazeDS Developer Guide]

We will use the RemoteObject implementation based on the Action Message Format (AMF) protocol in the following example.

Development for the Web Container

Christophe Coenraets from Adobe has published a Flex 3 adoption of his former Flex-to-Spring code project we will have a deeper look at. You may download the BlazeDS turnkey server and have a look at the sources in parallel.

The example is based on a Web container project (well-suited for the good old Tomcat), that delivers the configuration for a Spring project plus a BlazeDS factory implementation. The factory allows to give up the control of the remote object lifecycle to Spring, although BlazeDS normally manages the objects itself. So, all advantages of the Spring Dependency Injection can be used for remote objects then.

Extending Christophe’s Example

The example is pretty simple and allows to deliver Integer values to the client. How about a list of POJOs?

The Flex MXML code to create the client:

<?xml version="1.0" encoding="utf-8"?>
<mx:application xmlns:mx="http://www.adobe.com/2006/mxml">

    <mx:remoteobject id="spring"
        destination="pojoService"/>

    <mx:button label="Get List of Crew Members"
        click="spring.pojoList()"/>
    <mx:hbox width="300" height="200">
        <mx:list id="crewList"
            dataProvider="{spring.pojoList.lastResult}"
            width="100" labelField="name"/>
    </mx:hbox>
</mx:application>

We have a remote object “spring” here that references a “pojoService”. The remote object allows to call the “pojoList” method. This can be done through a button click. After a successful remote call we can reference the result list through “{spring.pojoList.lastResult}”. The Flex list shows the name attributes from the result list.

The pojoService is configured in {web_root}/WEB-INF/flex/remoting-config.xml:

<destination id="pojoService">
    <properties>
        <factory>spring</factory>
        <source>flexBean</source>
    </properties>
</destination>

Here we define the Spring bean “flexBean” to be used by the “pojoService”. The bean is managed by the BlazeDS factory “spring”.

BTW: You can configure the location of the BlazeDS configuration via the “services.configuration.file” parameter of the “MessageBrokerServlet” in web.xml. The example references a “/WEB-INF/flex/services-config.xml”. In this file you can find e.g. a reference to the “remoting-config.xml” that resides in the same folder.

To complete our example extension we have a look at the Spring application context and the corresponding bean implementation:

<beans>
    <bean id="flexBean"
        class="com.company.FlexList"/>
</beans>

We have a simple Spring bean here, that will implement the “pojoList” method for us:

[java]
package com.company;

import java.util.ArrayList;

public class FlexList {

public ArrayList<object> pojoList() {

ArrayList pojoList = new ArrayList();

pojoList.add(new Pojo("Jean-Luc Picard"));
pojoList.add(new Pojo("Deanna Troi"));
pojoList.add(new Pojo("Data"));

return pojoList;
}
}
[/java]

So, here we have the method that creates an ArrayList of Pojos. The Pojo class defines the “name” attribute that is referenced in the Flex list:

[java]
package com.company;

public class Pojo {

private String name;

public Pojo(String name) {
this.name = name;
}

public void setName(String name) {
this.name = name;
}

public String getName() {
return name;
}
}
[/java]

Next Steps

Some things we left out:

1 Comment so far

Comments are closed.