5.2 Using Remote Procedure Calls

5.2.1 Configuring Remote Services

A Flex application needs to know how to access remote services. The configuration of remote services is specified using the ‑services path_to_services-config.xml compiler directive. For more information about the services-config.xml file refer to the online Flex SDK documentation.

In Flex Builder this is specified as a compiler argument in the Flex Compiler options in the Flex project properties dialog box:

Flex Builder also needs to know server options for a Flex server application. To keep it simple, the Flex projects.

5.2.2 Services Configuration

The services-config.xml file should be placed in the WEB‑INF/flex directory where the Flex application is deployed.

<?xml version="1.0" encoding="UTF-8"?>
    <service class="flex.messaging.services.RemotingService" id="remoting-service" messageTypes="flex.messaging.messages.RemotingMessage">
        <adapter-definition class="flex.messaging.services.remoting.adapters.JavaAdapter" default="true" id="java-object"/>
      <destination id="Web2Internal">
          <channel ref="Web2InternalAMFChannel"/>
      <destination id="Web2Connect">
          <channel ref="Web2ConnectAMFChannel"/>        </channels>
    <channel-definition class="mx.messaging.channels.AMFChannel" id="Web2InternalAMFChannel">
      <endpoint class="flex.messaging.endpoints.AMFEndpoint" uri="http://manor-as08-v:8080/moweb2cn/amf/Web2InternalService"/>
    <channel-definition class="mx.messaging.channels.AMFChannel" id="Web2ConnectAMFChannel">
      <endpoint class="flex.messaging.endpoints.AMFEndpoint" uri="http://manor-as08-v:8080/moweb2cn/amf/Web2ConnectService"/>
    <factory class="org.codehaus.enunciate.modules.amf.SpringFactory" id="spring"/>
     <target class="flex.messaging.log.ConsoleTarget" level="Debug" />

5.2.3 Building and Deploying

A Web hosted Flex application needs to be deployed to a Web server. Flex Builder is able to copy the compiled project and associated assets to target output path. In Flex Builder configure the project’s Flex Build Path option for the Output Folder by specifying a path to a directory where Flex has write permissions:

5.2.4 Creating a Session using RPC

The supplied SWC archive file contains a library of functions and data types that saves developers from having to write their own library from scratch. The Web 2.0 Connect Flex library file can be download from the downloads section on the home page of the Web 2.0 Connect installation.

The following source code demonstrates creating a Web 2.0 Connect session using the RPC library:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
  height="256" width="830">
    import com.mosol.moweb2cn.Web2Connect;
    import com.mosol.moweb2cn.Web2Connect.CreateSessionResultEvent;
    private function createSession() : void
      var rpc : Web2Connect = new Web2Connect();
      rpc.addEventListener("createSession", sessionCreateHandler);
      rpc.addEventListener("fault", rpcFaultHandler);
      rpc.createSession("admin", "formula");
    private function sessionCreateHandler(event : CreateSessionResultEvent) : void
      responseText.text = event.result.value;
    private function rpcFaultHandler(event : Event) : void
      responseText.text = event.toString();
    text="Click button to create a session"
    fontSize="20" fontFamily="Courier New"
    x="10" y="10"
    width="811" height="200"
    label="Create Session"        
    x="711" y="226"

The RPC library provides a set of managed code to access the Web 2.0 Connect APIs. Unlike the examples in Section 3.0, Programming Interfaces, the RPC created session responds with an event of type CreateSessionResultEvent. If the session creation is successful, the value property of the event’s result object is the string OK.

And remember, all remote services are handled by applications using asynchronous requests.

5.2.5 Closing a Session using RPC

Although a session eventually times out as far as the Web server is concerned, the Operations Center Web client session remains until the session is terminated.

By adding the following ActionScript functions to the previous example and a new button to invoke the function, the Web 2.0 Connect session can be closed and the Managed Object Web client session terminated:

    private function closeSession() : void
      var rpc : Web2Connect = new Web2Connect();
      rpc.addEventListener("closeSession", closeSessionHandler);
      rpc.addEventListener("fault", rpcFaultHandler);
    private function closeSessionHandler(event : CloseSessionResultEvent) : void
      responseText.text = "Session closed\n" + event.result.value;

The closeSession() RPC method response event is of type CloseSessionResultEvent and the response result value is simply an OK string, if successful; otherwise, an error message is returned:

Crashed sessions leave orphaned Web client sessions on the Operations Center server, which might need to be manually terminated.

5.2.6 A Word About Security

Flex applications are compiled into Flash SWF format files and use the Flash Player as their runtime library. Flash applications are typically hosted within a Web page, but there is nothing to stop a user from downloading the SWF file to their desktop and open it directly from their local file system. Doing so, however, causes a security violation within the Flash runtime and any attempts to access remote services results in a Security sandbox violation error.

Flex applications targeted for the Web must be hosted on a Web server to access remote services without compromising client security. Offline Flex applications intended for desktop deployment must use the Adobe AIR framework that provides a secure offline sandbox for Flash.