Calling multiple methods from one RemoteObject in Flex and Actionscript using BlazeDS

BlazeDS allows us to use RPC to access our Java objects from an Adobe Flex or AIR app. This is a simple example that allows you to have a remote java object which you will make calls upon from Flex (or Actionscript). There are plenty of BlazeDS remoting tutorials out there. This HOW-TO shows how to create an actionscript class to handle your remote objects and handlers as well as linking more than one method to a single remote object.

This HOW-TO assumes you have the knowledge to create a small flex app and a server-side java project using BlazeDS. If you don’t know how to do this, there are tons of tutorials out there like this one.

Here’s the java object that we’ll be calling methods on from Flex:

public class MyRemoteService {
	public MyRemoteService() { }
 
	public String getSessionId() {
		String sessionId = FlexContext.getFlexSession().getId();
		System.out.println("returning sessionId "+sessionId);
		return sessionId;
	}
 
	public String[] getMyData() {
		return stuff from database or whatever
	}
}

Our RemoteObject will be called remoteService and will call the two methods getSessionId() and getMyData(). The following is the mxml for remoteService that has the two methods. Each method has a name attribute and attributes to set handlers for a fault and result.

<mx:RemoteObject id="remoteService" destination="remoteServiceDestination">
	<mx:method name="getSessionId" fault="handler_getSessionIdFault(event)" result = "handler_getSessionIdSuccess(event)" />
	<mx:method name="getMyData"    fault="handler_getMyDataFault(event)"    result = "handler_getMyDataSuccess(event)" />
</mx:RemoteObject>

This next code block does the same thing as our above code, except it’s an actionscript class. It handles the same two methods.

public class ConnectionManager
{
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent; 
	import mx.rpc.AsyncToken;
	import mx.rpc.Responder;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.mxml.RemoteObject;
	import mx.messaging.Channel; 
	import mx.messaging.ChannelSet; 
	import mx.messaging.channels.AMFChannel;  
 
	private var blazeUrl:String = "http://localhost:8080/BlazeServer/messagebroker/";
	private var remoteService:RemoteObject = new RemoteObject;
 
	public function ConnectionManager()
	{
		var cs:ChannelSet = new ChannelSet(); 
		var myAMF:Channel = new AMFChannel("my-amf", blazeUrl + "amf"); 
		cs.addChannel(myAMF); 
		remoteService.channelSet = cs; 
 
		remoteService.destination = "remoteServiceDestination";
		remoteService.showBusyCursor = true;
		var rpcCall:AsyncToken;
 
		rpcCall = remoteService.getSessionId();
		rpcCall.addResponder(new Responder(handler_getSessionIdSuccess, handler_getSessionIdFault) );
 
		rpcCall = remoteService.getMyData();
		rpcCall.addResponder(new Responder(handler_getMyDataSuccess, handler_getMyDataFault) );
	}
 
	private function handler_getSessionIdSuccess(event:ResultEvent):void {
		trace(event);
		Alert.show("sessionId: "+event.message.body.toString(), "handler_getSessionIdSuccess");
	}
 
	private function handler_getSessionIdFault(fault:FaultEvent):void {
		trace(fault);
		Alert.show(fault.toString(), "handler_getSessionIdFault");
	}
 
	private function handler_getMyDataSuccess(event:ResultEvent):void {
		trace(event);
		Alert.show(event.message.body.toString(), "handler_getMyDataSuccess");
	}
 
	private function handler_getMyDataFault(fault:FaultEvent):void {
		trace(fault);
		Alert.show(fault.toString(), "handler_getMyDataFault");
	}
}

For a good book about Flex check out Flex 3 in Action


3 Comments

  1. r says:

    Very helpful!!! Thanks! Great job!

  2. jaison d.thomas says:

    When i debug the above program, i got the fault message, “Cannot invoke method ‘method name’ “, please clarify me.

  3. jaison d.thomas says:

    When i debug a similar program, i got the fault message, ” cannot invoke method ‘ methodName’ “,
    please clarify me.

Leave a Reply