Integrating Flex with JBoss Seam - Remote Calls

Remote calls are the most straight forward type of communication between Flex and a server. The basic idea is that the server exposes a few methods and then Flex calls them directly. In the example I posted before, the remote calls are done using AMF as implemented by Exadel Flamingo (they developed their own AMF-Serializer).

Here are the steps needed to take in oder to set up the communication:

  1. On the server side (JBoss Seam) we need to expose methods for remote access. To do this, we just have to mark the methods with the @WebRemote annotation on the interface
    (src/action/com/flexdevil/silvafug/DoSomething.java)

    @WebRemote public String doSomething();
  2. Configure the Flex application to let it know which Seam component to access (set the destination to the @Name of the Seam component in services-config.xml)
    <destination id="doSomething"/>
  3. Instantiate a remote object that will handle the communications between Flex and Seam (src/silvafug.mxml)
    <mx:RemoteObject destination="doSomething" id="service"
     result="{showWhatTheServerSaid()}"/>
  4. Talk to the server (src/silvafug.mxml)
    <mx:Script>
       ...
       function showWhatTheServerSaid()
       {
          Alert.show(service.doSomething.lastResult);
       }
       ...
    </mx:Script>
    ...
    <mx:Button label="Do Something" click="service.doSomething()"/>

Because the communication with the server is asynchronous, we need to monitor the lastResult in order to get the return value from the server. We can either use binding to keep an object set to the latest result (<mx:Text text="{service.doSomething.lastResult}"/>) or we can set the result property of the RemoteObject to a method that gets called once the lastResult is available.

Remote calls can be used to easily accomplish all the backend communications. They are useful to access database data (save/retrieve), run complex calculations based on user input, or extract interesting stats from the server and represent them in a meaningful way to the user. Although Flamingo offers a few custom components that are pretty powerful and that simplify the server communication even more for specific situations, Remote Objects represent the core of backend communications.

Leave a Comment

Name (required)

Mail (will not be published) (required)

Website

Comment