| Blog Home | About | Entries By Date | Search |
|
Title
Flex 3, WebService, ASyncToken and IResponder
Entry
I’ve built many web service based applications in Flash AS 2.0; and it’s always been a nightmare to track the Asyncronous calls to a web service, pending result responses and faults. But I was recently introduces to Flex Builder 3, running the 3.2 framework, and I’m feeling MUCH better about web services. I’m having issue when attempting to use the Flexbuilder GUI under Data> Importing Web Services (WSDL). So I’ve had to create my own classes and methods to support my applications; Then I researched ASyncToken, and IResponder and I was exstatic. let me show you some code:
<mx:Script>
 <![CDATA[
  import mx.rpc.AsyncToken;
  import mx.rpc.Responder;
  import mx.rpc.events.FaultEvent;
  import mx.rpc.events.ResultEvent;
  import mx.rpc.soap.mxml.WebService;
  private var ws:WebService�
  private var token:AsyncToken
  private var responder:mx.rpc.Responder
  public function init():void{
   var ws:WebService = new WebService();
   ws.wsdl = "http://myservice.com/?wsdl"
  Â
  }
  public function callMyTestMethod(params:*):void
  {
   token:ASyncToken = ws.myTestMethod();
   responder = new Responder(catchResult, catchFault)
   token.faultMessage = "myTestMethod Failed"
   token.otherParams = "other parameters"
   token.addResponder(responder);
  }
 Â
  private function catchResult(e:ResultEvent):void{
   myResultObject = e.results
   //Do something with the results. And by the way,�
   //access your Token object in this method through
   //the ResultEvent.token object;
   token = e.token;  Â
  }
 Â
  private function catchFault(e:FaultEvent):void{
   //Don't forget the FaultEvent.token object...�
   token = e.token;
   mx.controls.Alert.show(e.fault.faultString, token.faultMessage)�
   //I've created a log method to track all errors as well.
   log(e.fault.getStackTrace());
  }
  ...
Using this AsyncToken, and Responder interface, I’ve been able to successfully re-use a single WebService component, and track responses and faults from those services without serious concern. Now that I can track objects and pass parameters in the token, I’ve been able to daisy-chain services that are co-dependant to update data in a database, without the fear of the Race Condition. The best part about it; no more generic addEventListeners to the WebService. I can have a different listener per method. –Lenny