Friday, August 29, 2008

Invoking Flash remoting requests using the Java AMF Library

In a follow up to my recent post about the new Java AMF client available in BlazeDS, I was recently asked how to send a 'typical' flash remoting request to ColdFusion.

Here is how you do that in Java:

Object[] args = ...
String sourceName = "my.cfc.path.Component";
RemotingMessage message = new RemotingMessage();
message.setMessageId(flex.messaging.util.UUIDUtils.createUUID());
message.setOperation("myCfcFunctionName");
message.setBody(args);
message.setSource(sourceName);
message.setDestination("ColdFusion");
Object returnValue = amfConnection.call(null, message);

and accessing the body of the response like this.

Object body = ((AcknowledgeMessage) returnValue).getBody();

Not that the "args" variable is an array of the arguments you are passing to the CFC function. It can also be a Java List, or a simple object. See the BlazeDS source for RemotingMessage.java for details. This sends the same kind of message that the mx:RemoteObject tag does in MXML, so in ColdFusion terms you are using the "Flash Remoting Update". If you use the 'raw' AMFConnection API, you would be using the "classic" Flash Remoting. Both will work, but I recommend using the RemotingMessage style.

What does this do for you? Well if you are exclusively using Flex as a client, not much. But if you would like to write Java code to invoke a CFC, this alows you to do that very easily.

2 comments:

Michael Dorloechter said...

Hi Tom,
may I utilise your posting for an on-topic question? I am trying to reconstruct a given AMF-message and messing up the params types badly. How can I explicitely define multiple param types in a n-dimensional amf-message? I hope the following will explain:

Original Request and my reconstruction:
http://i.imgur.com/ESSZv.png


Reconstruction code:
http://pastebin.com/pcJA4bJH

As you can see, the object types inside the messages differ, and heres where im stuck.

Tom said...

Hi Michael,

Looks like you are real close, with the only difference being Array vs a "Mixed Array". Try making a real Java array instead of an array list.

Hope that gets you on a the right track.