This article is applicable to LiveCycle DS, LiveCycle DS Express, and BlazeDS.

Remote Object Service, one of the keys services in LiveCycle DS and BlazeDS, enables Flex applications make remote procedure calls to the Java server via the AMF3 protocol.

AMF3 is architected similar to SOAP, but magnitudes faster because it’s a pure binary protocol. If your Flex application loads large amounts of data, and speed/bandwidth is a priority, you should consider leveraging the Remote Object Service.

The conceptual diagram below demonstrates the architecture of a Flex app coupled with the remoting service,

flex remoting service

Server Side Coding and Configuration

With the Remote Object Service, Flex applications could invoke methods from Java classes hosted on the server.There are a few rules you have to follow when creating a remote object class in Java,

  • The Java class must be public
  • Only public methods are visible to the Flex client
  • Method names should not begin with an underscore character
  • Certain method names are reserved and should not be used, such as addHeader(), addProperty(), valueOf(), etc. Refer to the manual for the complete list
  • Enterprise Java Beans (JNDI hosted classes) are not supported

Here is an example of a simple Java class,

public class MyRPC
{
public String sayHello()
{
return “Hello from Remot Object!”;
}

public int add(int a, int b)
{
return a+b;
}
}

To host the classes in Remote Object Service,

  • Place compiled class files under WEB-INF/classes. Be sure to preserve the folder structure if you are using Java packaging.
  • It’s recommended to compile your classes into a jar. In that case, place the jar under WEB-INF/lib.

Finally, you must add the class destination in WEB-INF/flex/remoting-config.xml,

<destination id=“myJavaClass” >
<properties>
<source>MyRPC</source>
<scope>application</scope>
</properties>
</destination>

  • The destination id attribute is referenced when you invoke the class from Flex.
  • The <source> node points to the fully qualified name of the Java class. i.e. packageName.className
  • The <scope> node determines when the Java class will be instantiated,
    • Application means the class will be instantiated once during servlet initialization
    • Session means the class is instantiated for each user session.
    • Request resets the class for each invokation

Client Side Coding

In the Flex client code, instantiate a RemoteObject class and set the destination attribute to match the destination id in the remoting-config file.

<mx:RemoteObject id=“myService” destination=“myJavaClass”>

To invoke a method in the class, simply call the method name after the RemoteObject id,

<mx:Button click=“myService.sayHello()” />

You could add a result event handler to the RemoteObject to catch return messages, but a even quicker method is to bind to the lastResult,

<mx:Label text=“{myService.sayHello.lastResult}” />

Data Exchange between Flex and Java

Flex RemoteObject implicitly maps strongly typed objects between ActionScript and Java. A Date object in Flex, for example, is mapped to a Date object is Java automatically.

For scenarios where you have to pass multiple pieces of data back and forth, you need to explicitly declare a custom data type. Here is a simple example that uses a custom Java class to hold user registration info,

package sample;

public class UserInfo
{
private String userName;

public String getUserName()
{
return userName;
}

public void setUserName(String value)
{
userName = value;
}
}

For the RemoteObject to correctly parse the custom data type, you need to create a similar class in ActionScript,

[Bindable]
[RemoteClass(alias="sample.UserInfo")]
public class UserInfo
{
public var userName:String=”";
}

Notice the two attributes attached to the ActionScript class,

  • Bindable attribute is necessary for binding the class property values
  • RemoteClass attribute points to the corresponding Java class

Click here for the sample code