samedi 26 décembre 2009

Deep into GWT rpc request

By default, rpc calls doesn't haves timeout.
It is a problem when the network connection between the client and the server is interrupted, because the client rpc proxy waits indefinitely for a response that will never come... The management of the timeout depends on the version of GWT.


Management of the timeout with GWT version 1.x (tested with 1.6.4, 1.7.0 and 1.7.1) :


The first naive idea is to use the Timer class. This technique is not very elegant, because it monopolizes client side resources, and do not release http connection.
The problem is that RPC connection is a persistent http connection, and browser has a maximum number of http persistent connection per server.
So to solve this problem, we must be attentive to the interface of the asynchronous RPC service.


Typically service rpc methods are reflected in the asynchronous interface, with an additional parameter for callback and void return. But the proxy generator authorizes asynchronous  methods to return different kind of object : Void, Request and RequestBuilder.
In our case, we will use the object RequestBuilder, because we can defined a timeout with the methods "setTimeoutMillis". When an asynchronous  methods return a RequestBuilder object, for calling the remote method, we must explicitly call the "send" method of the RequestBuilder object. With this approach, when the timeout is reached, an RequestTimeoutException is throwed and the callback onFailure method is called.


Management of the timeout with GWT version 2.0 :


With GWT 2.0, it is possible to define its own RpcRequestBuilder class which define the timeout and will be used by the rpc proxy for asynchronous call of RPC service. Here's the code to implement :







Then, we must affect RequestBuilder to the rpc proxy, like this :




Aucun commentaire:

Enregistrer un commentaire