About nested RPC call

259 views
Skip to first unread message

tong123123

unread,
Jun 15, 2012, 5:09:08 AM6/15/12
to google-we...@googlegroups.com
just for example of nested server call, assume I need the following three server call in sequence
1) call server to select the count of records satisfy the criteria
2) if call1 success, call server to select the content of records satisfy the criteria
3) if call1 and call2 success, call server to put the criteria in session

as RPC is Async., so I need to write something like the following?
AsyncCallback callback1 = new AsyncCallback(){

  public void onSuccess(){
      AsyncCallback callback2 = new AsyncCallback(){
        public void onSuccess(){
            AsyncCallback callback3 = new AsyncCallback(){
                public void onSuccess(){

                }
                @Override
                public void onFailure(Throwable caught){
                 
                }
            }
        }
        @Override
        public void onFailure(Throwable caught){
         
        }
      }
  }
  @Override
  public void onFailure(Throwable caught){
 
  }
}

As shown, it is very ugly and difficult to trace...
any better method?

Jens

unread,
Jun 15, 2012, 6:22:10 AM6/15/12
to google-we...@googlegroups.com
First create methods for the different requests, and call the next method when the previous request is done. That way you avoid that chain of nested callback classes and its easier to read.

Second, it seems like you could do all three steps at once on the server with just a single request. In your example you would send the criteria information to your server, which then fetches the contents based on the criteria information and puts the criteria into the session. Then the server sends back the fetched records. Now you have the fetched records and their count (fetchedRecords.size()) on the client and the session contains the criteria. Same final situation but with a single request. If an error occurs on server side, well then your search has failed and you need to handle that on the client.
In general you can say, if there is no additional client logic involved between chained requests, its very likely that you can merge these chained requests into a single one. And a single request is always faster than three chained requests.

-- J.

RAlfoeldi

unread,
Jun 15, 2012, 4:09:26 PM6/15/12
to google-we...@googlegroups.com
"First create methods for the different requests, and call the next method when the previous request is done. That way you avoid that chain of nested callback classes and its easier to read. "

But you will never get around the nested call backs. Whatever you do, it might LOOK better, but it won't BE better. It's just the nature of async...
Reply all
Reply to author
Forward
0 new messages