http service not working for parallel requests

464 views Asked by At

I am using an http service object to make servlet requests inside a method in flex. The method is being invoked simultaneously in parallel by two events. I could see that both requests have reached the servlet, but only one returns to the result event. Also this behaviours is not consistent . is it possible that parallel invocation of the httpservice result in loss of some requests? I am sure that both requests have reached the servlet and data is returned from it. Its just that the result event is not triggered in certain cases. Thanks in advance.

Including code to describe the issue better.

Please find the method below. The below method "callServlet" is being invoked by two separate events

private var httpObj:HTTPService=new HTTPService();

private function callServlet(text:String):void{

        Alert.show(text);
        httpObj = new HTTPService();
        httpObj.url=<servlet URL>;
        httpObj.method="POST";
        httpObj.resultFormat="xml";
        httpObj.contentType="application/xml";
        var requestString:String=text;
        httpObj.request=requestString;
        httpObj.addEventListener(ResultEvent.RESULT,onResultMethods);
        httpObj.addEventListener(FaultEvent.FAULT,onFaultMethod);
        httpObj.send();

    }

Each time i call the method, i pass a different "text" variable. I can see that the alert displays the two different texts send to it. And as explained earlier, both requests does reach the servlet and a response is sent from servlet. But the result event "onResultMethod" is invoked just once.It doesnt invoke the "faultonFaultMethod" either.

2

There are 2 answers

1
Zeus On

Yes, I have seen this problem before, if you are making multiple requests from flex, some of them will be lost, that was back in 3.0 times. Browsers has a way of stopping the number of http calls, they can allow maximum of 2 calls at a time(depends on the browser), may be chain your requests one after the other or use a singleton which manages your calls.

0
Chinta On

Thanks all for help. I think i ve got the issue though i cannot guarantee the answer to be right. The above method is called twice by two events. The httpOject variable is a private var global to the method callServlet. The listeners created in this method are being removed in the result and fault handler methods(this is not shown in the code above). So i believe when multiple events call the method simultaneously there is a chance that the global variable httpObj is modified by both the events and hence both events result in servlet call using the same httpservice object. When the first call returns to the resulthandler i am removing the listener for resulthandler due to which the second result does not reach the resulthandler method.

This is my assumption and as of now i dont have any better solution. Do let me know if anyone comes up with a better explanation.