I require retrying to send a GWT RPC request if it fails (any response code other then HTTP 200). Reasons are complex so I won\'t elaborate on that. What I have so far is I
Well, found the answer myself. So it's pretty neat after all. Working in heavily loaded hospital environments, network tend to be unreliable. So that is why I needed to resend rpc requests a few times before giving up. Here is the solution :
1- Set you special request builder to catch all requests responses but keep the request builder.
((ServiceDefTarget)serviceRPC).setRpcRequestBuilder(new RpcRequestBuilder() {
@Override
protected void doSetCallback(RequestBuilder rb, final RequestCallback callback) {
final RequestBuilder requestBuilder = rb;
super.doSetCallback(rb, new RequestCallback() {
@Override
public void onResponseReceived(Request request,
Response response) {
httpResponseOkHandler(requestBuilder, callback, request, response);
}
@Override
public void onError(Request request, Throwable exception) {
httpResponseErrorHandler(requestBuilder, callback, request, exception);
}
});
}
});
2- Now use the request builder to send the request as many times as you want. One great thing is the request builder was already set and data was serialized which avoids having to store POJO unserialized data.
// We had some server HTTP error response (we only expect code 200 from server when using RPC)
if (response.getStatusCode() != Response.SC_OK) {
Integer requestTry = requestValidation.get(requestBuilder.getRequestData());
if (requestTry == null) {
requestValidation.put(requestBuilder.getRequestData(), 1);
sendRequest(requestBuilder, callback, request);
}
else if (requestTry < MAX_RESEND_RETRY) {
requestTry += 1;
requestValidation.put(requestBuilder.getRequestData(), requestTry);
sendRequest(requestBuilder, callback, request);
} else {
InvocationException iex = new InvocationException("Unable to initiate the asynchronous service invocation -- check the network connection", null);
callback.onError(request, iex);
}
} else {
callback.onResponseReceived(request, response);
}
This is working fine for me, use it at your own risK!