Wednesday, July 12, 2006

Flex: The Second Simplest Example of RemoteObject

(Update: I wrote this some time ago, and I don't have access to the source code any more. Therefore, I can't send it to you. And, I don't know about newer versions of Flex.)

In a previous post, we developed what I claim to be the world's simplest example of using the RemoteObject facility in Flex 2 to invoke a method on a Java object using the RPC services. In that example, we just had a button on a Flash application that called a method on the server. All the method did was print a message. Although we passed a message into the server method, that message was hard-coded in the ActionScript, and no results were passed back.

In this post, we will build on that to pass some user data into the server and get a response back. In theory, this ought to be very simple: the user data just comes from an input box, and the result will just be displayed in a text box.

Java Code

Below is a simple function that you can add to the SimpleRemoteObject from the previous example:

public String echoMessage(String msg)
System.out.println("SimpleRemoteObject.echo: " + msg);
return "The RemoteObject says: " + msg;

As you can see, the method writes the message to the console (like before), and returns the message with a little text prepended to it. Pretty simple.

Flash Client

The user interface is pretty simple, and I just added it to the form in the previous example. We have a TextInput to enter data, a Button to invoke the method, and TextArea to display the result from the server.

<mx:FormItem label="Input Message">
<mx:TextInput id="input"/>
<mx:Button label="Echo Message" click="echoMessage()"/>
<mx:FormItem label="Output Message">
<mx:TextArea id="output"/>

So far, so good. The button calls an ActionScript method (echoMessage) to call the server - just like the previous example:

public function echoMessage():void {

We just get the text from the TextInput object (input) and call the method on the server. (Note that although I named the ActionScript method the same as the Java method - echoMessage - this isn't necessary; I'm just not creative enough to think of anything else.) But wait, something is missing: what about the message coming back from the server? Based on my experience with Java (and dozens of other languages), I would expect to see code that looks like:

output.text = remoteObj.echoMessage(input.text);

But, that's not how it works in Flash. Flash makes RPC calls asynchronously. This means that Flash doesn't wait for the result to come back from the server, and that's actually "a good thing." Without asynchronous server calls, the Flash client would hang during a (potentially long) server call, and that would be a bad thing.

OK, so how do we get the result back from the server? This introduces some additional complexity, but it's still tractable. It begins in the MXML to describe the server method:

<mx:method name="echoMessage" result="displayResponse(event)"/>

This says that when the echoMessage method on the Java server object is called, and the result arrives at some later time, the displayResponse ActionScript method will be called.

This method is pretty simple, too:

public function displayResponse(event:ResultEvent):void { = event.result;

That's all there is. Note that we don't even have to update the remoting-config.xml file on the server. This is because we are not creating a new destination; we're just using the same Java class - SimpleRemoteObject.

When you run the application in the browser, enter a message in the input area, click the button, and the response will eventually appear in the output area.

If you need the complete code, let me know - post a comment. At the moment, I'm too busy to put together a zip file and post it somewhere.



Naresh Bojja said...

I am also new to flex. which web server you are using?

thank you.

Charles Anderson said...

That was just the stock server that comes with the Flex SDK - JRun.


Karthik said...
This comment has been removed by the author.
Mahmood said...

It is nice help but there is problem on my system. I am using Flex 3.
Channel.Connect.Failed error NetConnection.Call.Failed: HTTP: Status 404:
Why this happens? What could be the problem and solution?

Charles Anderson said...

Sorry Mahmood, but I can't help you. I haven't worked on Flex for a year and a half. I haven't been tracking the newer releases.


Bharath Enaganti said...

i need complete code for remote object using java......will u plz give......send to this

thank you

Unknown said...

Hello Charles....please can u mail me the full file will e-mail is :

Charles Anderson said...

Since this question keeps coming up: no, I can't send anyone the source code for this example. It was a long time ago (in Internet time), and I'd be hard pressed to even find the machine where I did the work, let alone find the source code.


raj said...

Could you please send me the complete code to Thanks a lot.