GWT RPC miss timeout

3,662 views
Skip to first unread message

And

unread,
Sep 10, 2009, 11:00:54 AM9/10/09
to Google Web Toolkit
Hi,
I couldn't find a way to set a timeout in GWT RPC call. Am I missing
something or this is done deliberately?

This feature is essential if your client use the system in low quality
network. Responses from the server might be lost sometimes and we
don't want obviously to loose one of two active connections to the
server.

I see there is nice implementation of timeout (with abort on
XmlHttpRequest) in RequestBuilder class, so any http request in GWT
supports timeout. Why I cannot use timeout with RPC? It should be easy
to implement because RPC uses RequestBuilder anyway (if I understand
GWT code correctly).

Did anybody cope with this issue?

Any help would be appreciated.

Many thanks,
Andrzej Bednarz

Sri

unread,
Sep 11, 2009, 1:15:44 AM9/11/09
to Google Web Toolkit
RPC Services do have timeouts, its just that by default they are set
to 0, which effectively means no timeout.

Here's what you can do to set timeouts on your services -

a) Create a custom RpcRequestBuilder, and set the desired timeout

public class MyRpcRequestBuilder extends RpcRequestBuilder {
@Override
protected RequestBuilder doCreate(String serviceEntryPoint)
{
RequestBuilder builder = new RequestBuilder
(RequestBuilder.POST, serviceEntryPoint);
builder.setTimeoutMillis(RPC_TIMEOUT);
return builder;
}
}

b) Set the custom RpcRequestBuilder immediately after your GWT.create
(..) call

//Can be a global object -- you don't have to create a new one
everytime
RpcRequestBuilder theBuilder = new MyRpcRequestBuilder();

MyServiceAsync service = GWT.create(MyService.class);
((ServiceDefTarget) service).setRpcRequestBuilder(theBuilder);


c) Use the service object to invoke methods as you normally do. In
your AsyncCallbacks onFailure(Throwable t), you can check

if (t instanceof RequestTimeoutException) {
/// handle timeouts..
}

thats it.

Thomas Broyer

unread,
Sep 11, 2009, 4:34:20 AM9/11/09
to Google Web Toolkit
Because RpcRequestBuilder (suggested by Sri) is a GWT 2.0 thing; if
you're using a GWT "official release" (hoping it is the latest, 1.7),
you can just declare your method in your xxxAsync to return a
RequestBuilder instead of void; then you can set the RequestBuilder's
timeout before calling send() to actually make the call.

And

unread,
Sep 11, 2009, 7:00:25 AM9/11/09
to Google Web Toolkit
Hi, thanks for the tip, it works like a charm!
Here is some more detailed info for GWT beginners (like me).
Like Thomas wrote, in order to set timeout for RPC request, you need
to change your functions in xxxAsync interface. For instance instead
of:

void getServerInfo(String input, AsyncCallback<String> callback);

you should have:

RequestBuilder getServerInfo(String input, AsyncCallback<String>
callback);

And when you call the service, you must explicitly run send()
function. Example below:

RequestBuilder rb = serviceAsync.getServerInfo(input, new
AsyncCallback<String>() {
@Override
public void onSuccess(String result) {
}
@Override
public void onFailure(Throwable caught) {
}
});
rb.setTimeoutMillis(6000);
try {
rb.send();
} catch (RequestException e) {
}

Many thanks for both answers.
Cheers,
Andrzej
Reply all
Reply to author
Forward
0 new messages