You probably know that data services in Flex are asynchronous. That is, when you make a data service request, the result does not come back immediately. You set up handlers for the results when they are returned.
As Flex applications become more complex, there is more reliance on communication with servers via the Flex data services.
One of those services, RemoteObject, is especially useful if your server-side code is running on the same application server as the Flex engine. RemoteObject calls are faster that WebServices and HTTPServices because the information is exchanged in binary.
But RemoteObject has a secret. Well, it has a not-so-well advertised behavior. While it is true that requests made with RemoteObject data services are asynchronous, they are not parallel.
That is, if you make 3 requests using HTTPService you get the results returned when each request completes. For example, if one request takes 2 seconds, another takes 10 seconds, and another takes 30 seconds, you get the first results back in 2 seconds, the next comes about 8 seconds later, and last comes some 20 seconds after that.
If make 3 requests using RemoteObject, you get the results when they ALL complete. That is, your results come back in 40 seconds.
The reason for this seemingly odd behavior is that all of the RemoteObject requests go to a single point in the server: a servlet known as amfgateway.
Amfgateway takes in all of the requests and queues them. It processes each in turn and when all have been run, returns the results back.
This is by design. Most applications are fine with this behavior. It is only when you are making several or more requests simultaneously AND those requests take an extraordinary amount of time to complete, do you have a problem.
However, there is a work-around should you need it. If you read the RemoteObject tag documentation, you’ll see there is an attribute called endpoint. This names the URL to handle the remote request. The default is the amfgateway servlet.
It turns out, that if you explicity give the endpoint and you make each URL unique by adding a bogus attribute, a new connection will be opened to service the request. Thus your simultaneous requests get executed in parallel and your results come back when each is finished.
Here is an example:
<mx:RemoteObject endpoint=”/flex/amfgateway?a=1″ … >
<mx:RemoteObject endpoint=”/flex/amfgateway?a=2″ … >
and so forth.
In the example “/flex” is the web root of your Flex installation.