BlazeDS & Flex & In the News & LiveCycle DS & Neat Apps & Tutorials 16 May 2008 10:09 am

BlazeDS Tutorial – Remote Object Service

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

Need to bring your Flex project up to speed? Zee Yang is a freelance Flex developer with deep understanding of architecture and user experience. You can reach him at zee.yang@gmail.com.

28 Responses to “BlazeDS Tutorial – Remote Object Service”

  1. on 20 May 2008 at 5:49 am 1.FlexLive.net » BlazeDS Tutorials said …

    [...] BlazeDS Remote Object Service – This is the bread n’ butter of BlazeDS. It enables you to make RPC calls to your custom Java objects. It’s also a necessity if your application is heavy on data [...]

  2. on 04 Jul 2008 at 7:42 am 2.John said …

    I’m new to Java and noticed that an error was occurring when trying to access the class via a remoteObject in flex 3.

    After much experimentation, I noticed that once the destination set within the java class path, (within the remote-config.xml) was set correctly, then the remote object connection was established.. lol the obvious.

    However (and this may help some people). When the path is changed the Java server must be restarted. Even if the path is re entered correctly again. So in other words

    myPackage.myClass = works, hurray!

    myClass = after running in flex, the path is now bust, must restart tomcat server

    Try to fix path by entering myPackage.myClass = still bust must still restart server

    Now I’m not 100% sure about this. However seems to be the case using tomcat on my development machine. And was also the one thing that took the longest to get my head around.

    JC

  3. on 18 Jul 2008 at 1:30 am 3.jonas said …

    This is cool, is there a way to specify the remote connection as a http service to enable a swf on a different server to make a remote call?

    Jonas

  4. on 03 Dec 2008 at 2:52 am 4.david said …

    sample custom java class is ok, but when i have a List property in the custom java class, call the RemoteObject will get error: TypeError: Error #1009: can not visit referenced property or method by null object…
    if i initialize the List property like this: public var userVO: Array = new Array; , it can’t get the value from java end-back. and now org.userVO.length = 0.

  5. on 12 Dec 2008 at 6:08 pm 5.Zanpher said …

    Hello,

    Thanks for the post, this is literally the only one on the net that seems to contain complete code.

    Unfortunately, this is still incomplete. The first part (client side) is missing the rest of the code needed to run ….

    Then you totally switch gears on the bottom half as a whole new sample with missing code as well ..

    Is there any chance you could provide a complete and working sample?

  6. on 19 Jan 2009 at 11:21 pm 6.Adam said …

    Thanks for the tutorial. I have gone through this several times but am still getting an error. I am running this on Tomcat 6.0.16 with the latest BlazeDS jars (3.2.0.3978). Has anyone seen this. I have seen others post a similar problem, but mine doesn’t have an HTTP code.

    RPC Fault faultString=”Send failed” faultCode=”Client.Error.MessageSend” faultDetail=”Channel.Connect.Failed error NetConnection.Call.Failed: HTTP: Failed: url: ‘http://localhost:8080/WebContent/messagebroker/amf’”]
    at mx.rpc::AbstractInvoker/http://www.adobe.com/2006/flex/mx/internal::faultHandler()[C:\autobuild\3.2.0\frameworks\projects\rpc\src\mx\rpc\AbstractInvoker.as:220]
    at mx.rpc::Responder/fault()[C:\autobuild\3.2.0\frameworks\projects\rpc\src\mx\rpc\Responder.as:53]
    at mx.rpc::AsyncRequest/fault()[C:\autobuild\3.2.0\frameworks\projects\rpc\src\mx\rpc\AsyncRequest.as:103]
    at mx.messaging::ChannelSet/faultPendingSends()[C:\autobuild\3.2.0\frameworks\projects\rpc\src\mx\messaging\ChannelSet.as:1482]
    at mx.messaging::ChannelSet/channelFaultHandler()[C:\autobuild\3.2.0\frameworks\projects\rpc\src\mx\messaging\ChannelSet.as:975]
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at mx.messaging::Channel/connectFailed()[C:\autobuild\3.2.0\frameworks\projects\rpc\src\mx\messaging\Channel.as:997]
    at mx.messaging.channels::PollingChannel/connectFailed()[C:\autobuild\3.2.0\frameworks\projects\rpc\src\mx\messaging\channels\PollingChannel.as:354]
    at mx.messaging.channels::AMFChannel/statusHandler()[C:\autobuild\3.2.0\frameworks\projects\rpc\src\mx\messaging\channels\AMFChannel.as:390]

  7. on 20 Jan 2009 at 10:53 am 7.Adam said …

    Nevermind, I got it. I didn’t set my context in the Flex project properties -> Server Properties

  8. on 29 Jan 2009 at 8:21 pm 8.Ramesh Kumar B said …

    i have followed all the steps mentioned inthe above tutorial but i am getting the error when I clicked the button. Here i am posting the error please see this and solve me .

    [RPC Fault faultString="[MessagingError message='Destination 'myJavaClass' either does not exist or the destination has no channels defined (and the application does not define any default channels.)']” faultCode=”InvokeFailed” faultDetail=”Couldn’t establish a connection to ‘myJavaClass’”]
    at mx.rpc::AbstractInvoker/http://www.adobe.com/2006/flex/mx/internal::invoke()[C:\autobuild\3.2.0\frameworks\projects\rpc\src\mx\rpc\AbstractInvoker.as:263]
    at mx.rpc.remoting.mxml::Operation/http://www.adobe.com/2006/flex/mx/internal::invoke()[C:\autobuild\3.2.0\frameworks\projects\rpc\src\mx\rpc\remoting\mxml\Operation.as:197]
    at mx.rpc.remoting::Operation/send()[C:\autobuild\3.2.0\frameworks\projects\rpc\src\mx\rpc\remoting\Operation.as:113]
    at Function/http://adobe.com/AS3/2006/builtin::apply()
    at mx.rpc.remoting.mxml::Operation/send()[C:\autobuild\3.2.0\frameworks\projects\rpc\src\mx\rpc\remoting\mxml\Operation.as:170]
    at Function/http://adobe.com/AS3/2006/builtin::apply()
    at mx.rpc::AbstractService/http://www.adobe.com/2006/actionscript/flash/proxy::callProperty()[C:\autobuild\3.2.0\frameworks\projects\rpc\src\mx\rpc\AbstractService.as:290]
    at eee/___eee_Button1_click()[C:\Users\ramesh\Documents\Flex Builder 3\Test\src\eee.mxml:25]

    Please help

  9. on 03 Feb 2009 at 1:15 am 9.raj said …

    hello,
    i find this tutorial very good for me as beginners…. this really help me to do my 1st flex java integration… i really appriciate that…. but m thinking how can i send my parameters to java …if anyone knows then it will be highly appriciated..

    thanks in advance

  10. on 27 Feb 2009 at 9:49 am 10.raj said …

    I have read the tutorial, its very nice thanks a lot.But I m getting this error Parse error at ”.can you please help me?

  11. on 01 Mar 2009 at 5:04 pm 11.donkeybandit said …

    Zee, I’ve read a number of your articles now and they are excellent. I have a feeling I will be coming back regularly as I use more and more complex AMF projects.

    Your bare bones article was excellent and prompted me write my own specific one for a tomcat turnkey solution. If you’re interested, you can read it at http://flexbandit.com/archives/55

  12. on 04 Mar 2009 at 1:05 am 12.Pavithra said …

    I have a Prob. i need to invoke a method in a class which is extends a base class… when i give the source as extended class., blaze is not able recog this class!! Cant we use a extended class as source in remote-config.xml??!!

  13. on 15 Mar 2009 at 6:30 am 13.Basarat said …

    Thanks … session helped a lot :)

  14. on 26 Mar 2009 at 8:15 pm 14.Sean said …

    I’m getting a parse error like raj on the RemoteObject tag. Is there a workaround or fix for this? I’ve seen that it’s been a bug in the framework reported some time ago:

    http://bugs.adobe.com/jira/browse/SDK-16099

  15. on 14 Apr 2009 at 6:18 am 15.babass said …

    I everybody,

    I have a same question of Jonas :

    “jonas said …

    This is cool, is there a way to specify the remote connection as a http service to enable a swf on a different server to make a remote call? ”

    Babass

  16. on 07 May 2009 at 1:03 pm 16.Doug said …

    I’m not sure AMF is that much better than SOAP. You cited performance, and that certainly is true. However, when you’re developing in FlexBuilder the only way to validate that you’re calling the appropriate method is to verify at “runtime”.

    FlexBuilder does no introspection for “type aheads” on the target Java destination objects. You get that with with WSDLs. You get this with EJB interfaces. You get this with CORBA IDLs.

    From a coder perspective RemoteObject is a “code and pray” approach. If your builds take a long time then finding typos, etc, can be all the more frustrating. You have no guarantees that your “sayHello()” method exists in the remote object.

  17. on 04 Sep 2009 at 4:01 am 17.Tanzeem Akhtar Bhatti said …

    Thanks a lot for your sample

  18. on 11 Sep 2009 at 5:28 pm 18.Ashwin suresh said …

    I was wondering guys, i have a flex client setup to get data through a webservice. I want to modify my client to use the remoteobject so that i can move data through the AMF protocol. My BlazeDS is deployed in the same oc4j server as my webservice. Webservice is basically an archive(web archive, but still an archive) behind the scenes. Once deployed, the class file should be accessible via classloader of the application server’s java instance. Can i just create a destination in ‘remoting-config’ for my webservice’s class file. In this way, i can use the webservice so that other resources can access the webservice as well as allow my flex client to transfer data through AMF protocol.

    If my question is not clear, i can explain it in detail.

  19. on 17 Sep 2009 at 10:31 am 19.jorge said …

    thanks a lot for your samples, again more especificationes

  20. on 25 Sep 2009 at 6:15 am 20.Jelle said …

    For the entry #8 where Ramesh reported the problems where his destinations were not recognized. You might want to check the setup of your Flex project. You have to be careful when you select and set the root context. If you fail then you hit Ramesh’s problem.

    Please check my image in ..

    http://www.otembo.de/blog/blog3.php/development/

  21. on 15 Oct 2009 at 7:54 pm 21.doez said …

    thx

  22. on 22 Feb 2010 at 8:35 pm 22.kjv007 said …

    Thanks for the tutotorial, but I’m having this error, any advice?

    [RPC Fault faultString="[MessagingError message='Destination 'myJavaClass' either does not exist or the destination has no channels defined (and the application does not define any default channels.)']” faultCode=”InvokeFailed” faultDetail=”Couldn’t establish a connection to ‘myJavaClass’”]
    at mx.rpc::AbstractInvoker/http://www.adobe.com/2006/flex/mx/internal::invoke()[E:\dev\3.1.0\frameworks\projects\rpc\src\mx\rpc\AbstractInvoker.as:259]
    at mx.rpc.remoting.mxml::Operation/http://www.adobe.com/2006/flex/mx/internal::invoke()[E:\dev\3.1.0\frameworks\projects\rpc\src\mx\rpc\remoting\mxml\Operation.as:197]
    at mx.rpc.remoting::Operation/send()[E:\dev\3.1.0\frameworks\projects\rpc\src\mx\rpc\remoting\Operation.as:113]
    at Function/http://adobe.com/AS3/2006/builtin::apply()
    at mx.rpc.remoting.mxml::Operation/send()[E:\dev\3.1.0\frameworks\projects\rpc\src\mx\rpc\remoting\mxml\Operation.as:170]
    at Function/http://adobe.com/AS3/2006/builtin::apply()
    at mx.rpc::AbstractService/http://www.adobe.com/2006/actionscript/flash/proxy::callProperty()[E:\dev\3.1.0\frameworks\projects\rpc\src\mx\rpc\AbstractService.as:290]
    at StartHere/handleRemoteObj()[E:\Workspace\ProofOfConceptWS\FSubmetering\flex_src\StartHere.mxml:127]
    at StartHere/___StartHere_Button1_click()[E:\Workspace\ProofOfConceptWS\FSubmetering\flex_src\StartHere.mxml:151]

  23. on 08 Mar 2010 at 5:15 am 23.Hidayath said …

    kjv007… Your problem can be solved by compiling the flex code with -services compiler option

  24. on 08 Mar 2010 at 5:17 am 24.Hidayath said …

    kjv007 … -services takes an argument. The path to the services-config.xml file

  25. on 13 Mar 2010 at 10:37 am 25.Praveen said …

    Thanks a lot for the sample… It is really good.
    I have a problem when returning custom object from server. A string value set a server side is not reflected at client, meaning the value is null.

    But things work fine for primitive like String. I have created a corresponding Action Script class at client. Any idea how this could be resolved?

  26. on 27 Mar 2010 at 10:52 am 26.Fareed Ahmad Shaik said …

    Hi guys,

    May I know how can I call my method using webservice.
    from Flex to Java.

  27. on 15 Jun 2010 at 10:01 pm 27.Use BlazeDS to create exceptional rich applications with Flex and Java | Flex Certification said …

    [...] BlazeDS Tutorial – Remote Object Service [...]

  28. on 31 Jul 2010 at 6:05 am 28.M_R@V said …

    Hi guys ,

    While executing the given code,the following error occurred ,

    Error :

    “Destination ‘myJavaClass’ either does not exist or the destination has no channels defined (and the application does not define any default channels.)’”

    Please help me to get out from this error.

Trackback This Post | Subscribe to the comments through RSS Feed

Leave a Reply