开发者

Waiting for more than one event (using GWT)

I want to fetch 开发者_如何学JAVAtwo XML documents from the server and resume processing when both have arrived. Can I fetch them in parallel, or do I have to refrain from issuing the second request until the first has completed?


You can fetch them in parallel, but keep in mind that browsers have a limit on the number of parallel requests, see http://www.browserscope.org/?category=network (choose "Major Versions" in the dropdown on the top left to see more versions). Note especially, that IE < 8 has a limit of 2 connections per hostname!

If you still want to do this, then note that the responses can arrive in any order. So you'll have to implement something that will keep track of the requests/responses (a counter or something more sophisticated), so that you'll know when all responses you need have arrived.

The best solution is often to send just one request that asks for both XML documents, and the server returns them both at once in one response.


Make both requests, then check when either one completes whether the other is done, and continue if it is.

private String responseOne;
private String responseTwo;

public startRequests() {
  makeAsyncRequestOne(new AsyncCallback<String>() {
    onSuccess(String response) {
      this.responseOne = response;
      if (responseTwo != null) {
        proceed();
      }
    }
  });

  makeAsyncRequestTwo(new AsyncCallback<String>() {
    onSuccess(String response) {
      this.responseTwo = response;
      if (responseOne != null) {
        proceed();
      }
    }
  });
}

As Chris points out, this may hit a ceiling on maximum concurrent requests to the same hostname, so if you have lots of requests to send at once, you could keep a queue of requests and call the next one in proceed() until the queue is exhausted.

But if you plan on having a lot of concurrent requests, you probably need to redesign your service anyway, to batch operations together.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜