views:

1083

answers:

4

With DWR it is possible to group together several service calls into one single HTTP request :
dwr batch feature

This feature is very useful to reduce the latency of an ajax application. Is there a way to do something similar with GWT / GWT-RPC ?
Thanks for your help

A: 

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));
  }
}
dmazzoni
Your solution doesn't work because the RPCs in GWT are asynchronouse; they return immediately after the call; the AsyncCallback handler is responsible for handling the value.
Miguel Ping
Unless his batchCallFoo is the server-side implementation, in which case, it would work, but as a client-side call, his batch implementation does nothing of the sort - it calls callFoo repeatedly, which is the antithesis of "batch" calls.
Chris Kaminski
A: 

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.

stian
+5  A: 

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.

Steve Armstrong
A: 

You can also use GWTEventService if your application fits into the domain of Comet (server-side-push):

GWTEventService is an event-based client-server communication framework. It uses GWT-RPC and the Comet / server-push technique. The client side offers a high-level API with opportunities to register listeners to the server like to a GUI component. Events can be added to a context/domain on the server side and the listeners on the client side get informed about the incoming events. The server side is completely independent of the client implementation and is highly configurable.

Because one of the advantages offered by this event model is:

Events are bundled to reduce server calls

Amir Moghimi