Expand my Community achievements bar.

Setting up RemoteObjects

Avatar

Level 1
I have a question about how to set up a simple RemoteObject
call in Flex 2 (with CF 7.0.2). It seems like the examples inside
the documentation never do a good enough job of actually telling
you where your source files are supposed to be located in reference
to everything else.



So, my question is, does someone have some very quick and
dirty sample code of how I can set up a simple RemoteObject call in
Flex?



I need:



- Source code for your Flex app (with the
<mx:RemoteObject> call).

- Changes you had to make to your services-config.xml file

- Location of your files (MXML code and your CFCs that the
destination ties to)



The Flex help always give a source like this:

<source>samples.restaurant.RestaurantService</source>



But then never actually tells you where in the world that
object exists. Obviously there's some CFC called
"RestaurantService", but where does it exist (full path)?



The application that I built already uses Web services, but
I'd like to switch over to remote objects (since they are binary
and should be quicker than the doggy SOAP wrappers). Implementing
web service calls was a snap, but the Remote Objects seems quite a
bit more lengthy.



Any help is greatly appreciated. Thanks!







3 Replies

Avatar

Former Community Member
Well, I can give you an example of how we do it using JBoss
instead of ColdFusion. Maybe it would help...



Assuming you have FDS instead on your Java Application Server
and your Java class has a public method you are trying to access. A
simple one:



package com.simple.example;

public class AJavaClass {

public AJavaClass() {}

public String addExclamationPoint(String aString) { return
aString + "!"; }

}



You then to add configuration to your web.xml so that FDS can
access your Java Class. Let's hope the markup will show:







Avatar

Former Community Member
OK, let us continue this: the web.xml



<context-param>

<param-name>flex.class.path</param-name>


<param-value>/WEB-INF/flex/hotfixes,/WEB-INF/flex/jars</param-value>

</context-param>



<!-- 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>

<init-param>

<param-name>flex.write.path</param-name>

<param-value>/WEB-INF/flex</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>



<servlet>

<servlet-name>FlexMxmlServlet</servlet-name>

<display-name>MXML Processor</display-name>

<description>Servlet wrapper for the Mxml
Compiler</description>


<servlet-class>flex.bootstrap.BootstrapServlet</servlet-class>

<init-param>

<param-name>servlet.class</param-name>


<param-value>flex2.server.j2ee.MxmlServlet</param-value>

</init-param>

<init-param>


<param-name>webtier.configuration.file</param-name>


<param-value>/WEB-INF/flex/flex-webtier-config.xml</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>



<servlet>

<servlet-name>FlexSwfServlet</servlet-name>

<display-name>SWF Retriever</display-name>


<servlet-class>flex.bootstrap.BootstrapServlet</servlet-class>

<init-param>

<param-name>servlet.class</param-name>


<param-value>flex2.server.j2ee.SwfServlet</param-value>

</init-param>

<!-- SwfServlet must be initialized after MxmlServlet
-->

<load-on-startup>2</load-on-startup>

</servlet>



<servlet>


<servlet-name>FlexForbiddenServlet</servlet-name>

<display-name>Prevents access to *.as/*.swc
files</display-name>


<servlet-class>flex.bootstrap.BootstrapServlet</servlet-class>

<init-param>

<param-name>servlet.class</param-name>


<param-value>flex.server.j2ee.ForbiddenServlet</param-value>

</init-param>

</servlet>



<servlet>

<servlet-name>FlexInternalServlet</servlet-name>


<servlet-class>flex.bootstrap.BootstrapServlet</servlet-class>

<init-param>

<param-name>servlet.class</param-name>


<param-value>flex.server.j2ee.filemanager.FileManagerServlet</param-value>

</init-param>

<load-on-startup>10</load-on-startup>

</servlet>



<servlet-mapping>


<servlet-name>MessageBrokerServlet</servlet-name>

<url-pattern>/messagebroker/*</url-pattern>

</servlet-mapping>



<servlet-mapping>

<servlet-name>FlexMxmlServlet</servlet-name>

<url-pattern>*.mxml</url-pattern>

</servlet-mapping>



<servlet-mapping>

<servlet-name>FlexSwfServlet</servlet-name>

<url-pattern>*.swf</url-pattern>

</servlet-mapping>



<servlet-mapping>


<servlet-name>FlexForbiddenServlet</servlet-name>

<url-pattern>*.as</url-pattern>

</servlet-mapping>



<servlet-mapping>


<servlet-name>FlexForbiddenServlet</servlet-name>

<url-pattern>*.swc</url-pattern>

</servlet-mapping>



<servlet-mapping>

<servlet-name>FlexInternalServlet</servlet-name>

<url-pattern>/flex-internal/*</url-pattern>

</servlet-mapping>



you'll also need to place the flex folder in the WEB-INF of
your WAR. Instead the flex folder is your remote-config and
services config file. The remote-config you put the java class you
are to access and the protocol you'll use (we are using AMF here):



<default-channels>

<channel ref="my-amf"/>

<!-- <channel ref="my-http"/> -->

</default-channels>

<destination adapter="java-object" id="RO">

<properties>

<source>com.simple.example.AJavaClass</source>

<scope>session</scope>

</properties>

</destination>



You'll need to adjust your service-config to point to your
application server...



<channel-definition id="my-amf"
class="mx.messaging.channels.AMFChannel">

<endpoint uri="
http://jboss.example.simple.com:8080/prefix/messagebroker/amf"
class="flex.messaging.endpoints.AMFEndpoint"/>

<properties>

<polling-enabled>true</polling-enabled>

</properties>

</channel-definition>



You may need to adjust the security on services-config
accordingly. The mxml can then look like this:



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

<mx:Application xmlns:mx="
http://www.adobe.com/2006/mxml">



<mx:Script>

<![CDATA[



private function runRO(event:Event):void

{

RO.addExclamationPoint(text1.text);

}



private function roLoaded(evt:ResultEvent):void

{

text1.text = evt.result as String;

}



]]>

</mx:Script>



<mx:VBox x="0" y="0" percentWidth="100"
percentHeight="100">

<mx:HDividedBox

horizontalAlign="left"

percentWidth="100"

percentHeight="100"

backgroundColor="#FFFFFF">

<mx:TextInput id="text1" />

<mx:Button label="go" click="runRO" />

</mx:HDividedBox>

</mx:VBox>



<mx:RemoteObject id="RO" destination="RO" >

<mx:method

name="addExclamationPoint"

result="roLoaded(event)"

fault="mx.controls.Alert.show(event.fault.message, 'RO
Error')"/>

</mx:RemoteObject>

</mx:Application>



We then precompile the flex and place the swf in the root
directory of the war. You then just wrap it all up in the WAR and
place it on the application server. Then things just magically
work.



Unfortunately, there is no steadfast way to do this because
of the multiple platforms this needs to be able to run on. But this
is esstentially how we got ours to work. As for using it with CF if
you google "Flex 2 RemoteObject CF" you'll get plenty of examples.





Avatar

Level 1
Ok, so I figured it out.



[1]

I don't need to touch the "services-config.xml". I am using
ColdFusion 7.0.2 which means that I can communicate with CFCs
without any further modification.



[2]

This was the code that I was using to invoke my remote
object:



<mx:RemoteObject id="dashboardRPC"

destination="ColdFusion"

source="cfc.dashboard">

</mx:RemoteObject>



However, I came to find out that the "source" directory is
actually off the webroot of the ColdFusion installation (for us,
that's d:\cfusionmx7\wwwroot). However, our IIS server uses a
different root for all of the actual web files (d:\web\wwwroot).
So, here's where I thought the CFC should go:



d:\web\wwwroot\cfc\dashboard.cfc



However, according the current setup, it's supposed to go
here:



d:\cfusionmx7\wwwroot\cfc\dashboard.cfc



I can, however, keep the cfc in the IIS directory as long as
I use a CF Mapping and then change services-config.xml to allow CF
to use mappings.



I now have it working. Thank goodness.