How to group gwt-rpc calls?
Asked Answered
C

4

7

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

Careerist answered 25/10, 2008 at 18:12 Comment(0)
C
8

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.

Coben answered 9/10, 2009 at 15:13 Comment(0)
B
1

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));
  }
}
Brink answered 25/10, 2008 at 19:15 Comment(2)
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.Engrail
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.Solana
I
1

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.

Insecure answered 30/12, 2008 at 11:49 Comment(0)
A
1

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

Affer answered 11/6, 2010 at 6:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.