views:

43

answers:

2

while trying to solve my problems in serializing the execution of cairngorm commands, I tried to bypass completely the event dispatching and simply instantiated the command I wanted to execute, then called it's execute method. In this method there's a call to a delegate that calls ServiceUtils that performs the HTTPService.send thing...

Now, those commands should be run in the exact order I call them. And, since the server (RAILS) is only one, all requests should return in the same order. This isn't so.. the order varies upon different executions.. why?!?

+1  A: 

Just because you send requests in a certain order doesn't mean the responses will return in that order. HTTPService calls are asynchronous. For example, assume the following three requests are sent at the same time:

Request 1 (takes 4 seconds on the server to process)
Request 2 (takes 0.5 seconds to process)
Request 3 (takes 2 seconds to process)

Assuming network speed is constant (and a lot of other environment issues being constant), you will get the response for Request 2 back first, then Request 3, then Request 1.

If you need to call them in serial, you should do something like this:

protected function doWork():void {
    request1.send();
}

protected function onRequest1Complete(e:ResultEvent):void {
    request2.send();
}

protected function onRequest2Complete(e:ResultEvent):void {
    request3.send();
}

protected function onRequest3Complete(e:ResultEvent):void {
    // you are done at this point
}

...

<mx:HTTPService id="request1" url="http://example.com/service1" result="onRequest1Complete(event)" />
<mx:HTTPService id="request2" url="http://example.com/service2" result="onRequest2Complete(event)" />
<mx:HTTPService id="request3" url="http://example.com/service3" result="onRequest3Complete(event)" />

Hope that helps.

RJ Regenold
I'm not quite convinced.. the web server is only one, and it can process only one request at a time.. therefore I still don't understand why it returns in different order.. but I solved the problem making the GUI 'return order proof'thanks
luca
For the same reason you can run multiple applications on your computer, a server can process multiple requests. For example, on your computer you probably have a web browser open, can listen to music, and can have an IDE open. Imagine each of those as requests. Your computer doesn't stop playing music because you're in your IDE, right? It's the same concept on the server. Imagine the physical infrastructure that would be required if a server could only process on request at a time! If someone did something process intensive on your site, it would prevent anyone else from using it. HTH.
RJ Regenold
A: 

RJ's answer covers it very well. Just to add to it:

Your commands will create asynchronous requests via the services you use. If you want to "simulate" synchronous execution of commands, the subsequent command will have to be executed in the resultHandler of the previous commands request.

Although this may not always be the cleanest way of doing things, it may be suitable for your scenario. I'll need more information about the nature of service calls and the app in general to make a call whether this is the best method for you or not.

HTH, Sri

Srirangan