How to group gwt-rpc calls? How to group gwt-rpc calls? ajax ajax

How to group gwt-rpc calls?


Google's Ray Ryan did a presentation about Best Practices For Architecting Your GWT App, where he talked about using a command pattern. Sending asynchronous commands that happen to go over RPC is what you probably want. Once you're sending commands instead of RPCs, it's very easy to batch them.

See gwt-dispatch for a library that implements this pattern for you. I'm just starting to use it, so I don't know if it batches automatically, but it's all open source with a permissive licence, so you can fix it if it doesn't.


GWT doesn't provide a one-step solution for batching several arbitrary RPCs. However, keep in mind that GWT's automatic serialization makes it quite easy to write both serial and batched versions of each of your RPC methods. For example, suppose you've defined this RPC:

FooResponse callFoo(FooRequest request);

It's this easy to write a "batch" version of the same RPC yourself:

ArrayList<FooResponse> batchCallFoo(ArrayList<FooRequest> requests) {  ArrayList<FooResponse> responses = new ArrayList<FooResponse>();  for (FooRequest request : requests) {    responses.add(callFoo(request));  }}


It's a good question but I don't think there's an easy solution.

I believe you'll have to create a separate method that groups together your methods to achieve batching in a similiar way to DWR.

Ie if you have:

public int add(int x, int y);public int sub(int i, int j);

You'd create a new method to combine them:

public Map<String, Integer> addAndSub(Map methodsAndArguments) {    // Call add and sub methods with it's arguments}

You will still need to handle the whole response in the same callback method of course.

I realize it might not be the most elegant solution but due to the way GWTs RPC work I think it's the way to go. With GWT I think you should generally try to write your methods so that batching won't even be an issue you need to consider.