Monday, February 4, 2008

Using Remote Objects

Using remote object can be tedious and confusing if you want to do it from the scratch and want to understand everything at once. A better approach is to have something that works and build on it, understand it bit by bit. I used this approach for Tomcat but this should work well for any J2EE container like Websphere or JBoss. I am not focussing on Remote Object basics or RMI here, just how you can make it work for yourself for Java.


Download Adobe LiveCycle Data Services ES from:

http://www.adobe.com/products/livecycle/dataservices/

Install it for J2EE and not for integrated JRun. We will be using the express version. Alternatively you could use BlazeDS. To see the differences between the two visit this link:

Differences between Flex Data Services Express and BlazeDS

Please remember that using either means that you will have to buy it at some stage for commercial applications. See the respective licence for details. That being said these services are extremely useful and without them Flex looses a lot of utility.


  • The installation folder contains many things that you will be using as you understand flex more. For time being our interest lies in a file by name flex.war
  • Take flex.war and unzip the contents into a folder. Name it apporpriately. This folder will be your template for a blank flex application that you may want to develop on your server.
  • Take this skeleton folder and copy in the webapps directory of your Tomcat.
  • Browse to WEB-INF/flex folder. Open a file named license.properties and add license keys (ie if you have one) for flex builder, flex charting and flex data services.
  • In the same folder open file services-config.xml and see the security tag. Uncomment the line for tomcat server and comment the rest.
  • Open Flex builder. Go to File->New->Flex Project. Choose Flex Data Services for data access and compile application on the server when the page is viewed. Click Next. Browse to the folder in tomcat webapps where you are hosting the skeleton app. Validate location and name the root URL and context root appropriately. Click Next. Give an appropriate name to your project and click Finish.
  • You have set up the stage to use Remote Object.

    Now, copy the java class you intend to use in the appropriate directory under WEB-INF/classes. Lets say we have a class file from your compiled java file called MyRemoteObj.class made from this file:

    package app;

    public class MyRemoteObj{

    public String getMessage(){ return "hello"; }

    }

    Browse to WEB-INF/flex and open remoting-config.xml. Make sure it looks something like this:

    <?xml version="1.0" encoding="UTF-8"?>

    <service id="remoting-service" class="flex.messaging.services.RemotingService">

    <adapters> <adapter-definition id="java-object" class="flex.messaging.services.remoting.adapters.JavaAdapter" default="true"/>
    </adapters>

    <default-channels>
    <channel ref="my-amf"/>
    </default-channels>

    <destination id="responder">
    <properties>
    <source>app.MyRemoteObj</source>
    <scope>application</scope>
    </properties>
    </destination>

    </service>

    Remember this xml file is just one of the files uploaded by services-config.xml. It just helps in breaking our config xml into usable parts. <adapters> define the backend object. <channels> are defined in service-config. Here we just specify the channel we intend to use. destination is the part that helps flex use the remote object. id is ised to refer to it. source is its location under the WEB-INF/classes directory.

    Declare your remoter object in the MXML as

    <mx:RemoteObject id="remoteObjRef" destination="responder" fault="handleFault(event)" result="handelResult(event)"/>

    Use the id in AS to call the methods directly on the object: remoteObjRef.getMessage();

    No comments: