3 May 2011
Knowledge of J2EE containers, Adobe Flex, and Java will be helpful.
All
This article explains how to create a Flex application in Flash Builder 4.5 that invokes a method in a Java class on the server using BlazeDS Remoting service. After you set up the server environment required for the sample application, you’ll use Flash Builder 4.5 to generate ActionScript service classes and build a Flex application that displays the service call results.
The first step is to create a Java class to be invoked from the Flex application. The sample application for this article uses the SimpleCustomerService class. This class has a method named getAllCustomers(), which will be invoked from the Flex application:
public class SimpleCustomerService
{
public ArrayList<SimpleCustomer> getAllCustomers()
{
ArrayList<SimpleCustomer> customers = null;
//code to create ArrayList containing SimpleCustomer objects
return customers;
}
}
Start by compiling SimpleCustomerService.java and SimpleCustomer.java in the <SampleZipFile>/java_src folder (or you can use the precompiled class files from the <SampleZipFile>/java_classes folder).
To create a web application that uses the class files, follow these steps:
Before you can invoke the Java class from the Flex application you must expose the class as a Remoting service destination using BlazeDS. To set up BlazeDS for your web application you have to deploy BlazeDS JAR files in your web application source path. Follow these steps to set up BlazeDS:
This folder contains BlazeDS configuration files, which are used to configure Remoting, Messaging, and Proxy services.
Next, you need to add Servlet mapping for the BlazeDS Servlet named MessageBrokerServlet, so that BlazeDS is invoked when you send a request to a Remoting, Messaging, or Proxy destination using any of the channels supported.
If you are using your own web.xml file, then you’ll need to add the code below. Alternatively, you can copy it from blazeds/WEB-INF/web.xml.
<!-- Http Flex Session attribute and binding listener support -->
<listener>
<listener-class>flex.messaging.HttpFlexSession</listener-class>
</listener>
<!-- MessageBroker Servlet -->
<servlet>
<servlet-name>MessageBrokerServlet</servlet-name>
<display-name>MessageBrokerServlet</display-name>
<servlet-class>flex.messaging.MessageBrokerServlet</servlet-class>
<init-param>
<param-name>services.configuration.file</param-name>
<param-value>/WEB-INF/flex/services-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MessageBrokerServlet</servlet-name>
<url-pattern>/messagebroker/*</url-pattern>
</servlet-mapping>
Flash Builder 4.5 uses RDSDispatchServlet (bundled with BlazeDS and LiveCycle Data Services ES2) to get service destination details in a web application. If you’re using your own web.xml file, add Servlet mapping for RDSDisptachServlet in your web application by copying the following XML snippet into the samplewebapp/WEB-INF/web.xml file under the <web-app> node.
<servlet>
<servlet-name>RDSDispatchServlet</servlet-name>
<display-name>RDSDispatchServlet</display-name>
<servlet-class>flex.rds.server.servlet.FrontEndServlet</servlet-class>
<init-param>
<param-name>useAppserverSecurity</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>10</load-on-startup>
</servlet>
<servlet-mapping id="RDS_DISPATCH_MAPPING">
<servlet-name>RDSDispatchServlet</servlet-name>
<url-pattern>/CFIDE/main/ide.cfm</url-pattern>
</servlet-mapping>
To expose a Java class as a Remoting destination you need to add a <destination> node in the samplewebapp/WEB-INF/flex/remoting-config.xml file under the <service> node as shown below. (You can find the remoting-config.xml and services-config.xml files used for the sample application in the <SampleZipFile>/config folder.)
<destination id="SimpleCustomerServiceDestination">
<properties>
<source>com.adobe.services.SimpleCustomerService</source>
</properties>
</destination>
When you start your web server, BlazeDS will expose your Java class as a Remoting destination with SimpleCustomerServiceDestination as the id.
Now that the server is set up, you are ready to use Flash Builder 4 to generate code for consuming the Remoting service destination. The Flex application will invoke the getAllCustomers() method in the destination you created.
To generate ActionScript code that can consume a Remoting service destination, Flash Builder will send a request to the BlazeDS or Adobe LiveCycle Data Services ES2 enabled web application you configured in the project server settings and retrieve details of the exposed service destinations.
Follow these steps to create the new Flex project:
Follow these steps to create a new service in Flash Builder 4.5:
useAppSecurity parameter of RDSDispatchServlet is set to false in web.xml.)Flash Builder communicates with the server and displays list of exposed service destinations as shown in Figure 4.
Flash Builder 4.5 generates an ActionScript class that represents the Java class associated with the selected Remoting service destination. In this case, Flash Builder 4.5 generated a class named SimpleCustomerServiceDestination, which has functions for each public method exposed in SimpleCustomerService.java. To invoke a method of SimpleCustomerService.java on the server, you simply invoke the corresponding function in SimpleCustomerServiceDestination.as. You can see the service (SimpleCustomerServiceDestination.as) and its operations (representing functions in the service class) listed in the Data/Services view; you can also view the code using the Package Explorer.
If there are any custom data types used as a return type or as arguments in the Java class methods, Flash Builder 4.5 will generate an ActionScript class to represent the custom data type along with the service class files. In this sample getAllCustomers() returns an ArrayList containing SimpleCustomer object types. The ArrayList is a built-in data type and is converted to ArrayCollection on the client side by default. SimpleCustomer, however, is not a built-in data type so Flash Builder 4.5 generated SimpleCustomer.as with properties for each public property in SimpleCustomer.java to represent the SimpleCustomer objects returned from server. Note that the operation return types for the service are also properly configured in the Data/Services view based on the return type of the methods in the Java class.
Now that the code required to consume a Remoting service destination has been generated, follow the steps in the next section to display the response from the service call in a UI control.
Flash Builder 4.5 can also generate code to invoke a service call and bind the service call result to a UI control. In this sample you will bind the result of the getAllCustomers() operation to a DataGrid.
Because you selected New Service Call, Flash Builder 4 will create a new instance of the SimpleCustomerServiceDestination class and a CallResponder class in the current MXML file. If an instance of SimpleCustomerServiceDestination already exists only the CallResponder instance will be created. The CallResponder class helps you manage the results for asynchronous calls made to RPC-based services. You can find more details on CallResponder in the Flex Language Reference.
Note: If you already have a service call in the current MXML file and you want to bind its result to the UI control, then select Existing Call Result in the Bind To Data dialog box and choose the existing service call.
Save your application and run it. After the Flex application launches in a web browser, it will invoke the getAllCustomers() method in the SimpleCustomerService Java class on the server and displays the SimpleCustomer objects returned from the server in the DataGrid.
Now that you have used Flash Builder 4 to develop a Flex application that sends a request to a BlazeDS Remoting service destination and displays the response in a DataGrid, you may want to look for opportunities to use this approach in your own applications. You can expose any Java class as a BlazeDS or LiveCycle Data Services ES2 Remoting service destination and build a Flex application in Flash Builder 4 to consume that destination.
You can also use Flash Builder 4 to build Flex applications for HTTP services, web services, and the data management services in LiveCycle Data Services ES2. You can learn more in the article Data-centric development with Flash Builder 4.
If you are developing a data-centric application, you should also take some time to understand the data management and model-driven development capabilities of LiveCycle Data Services ES2. The LiveCycle Data Services ES2 Quick Starts is an excellent resource to help you hit the ground running.

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License