Search code examples
javaajaxgwtgwt-rpc

Fake a GWT Synchronous RPC call


First of all, I know that doing a synchronous call is "wrong", and know that "is not possible".

But, in a situation a lot complex (i dont know how to explain), i need to wait the response from server, I'am using the GWT-Platform command implementation for the GWT RPC calls.

I was looking for some kind of "hack" for doing this.

Thanks in advance.


Solution

  • There is a solution but it is not easy (e.g. you cannot flip a single parameter to make it work). GWT is using normal JS XMLHttpRequest under the hood. In GWT there is an overlay type for it called com.google.gwt.xhr.client.XMLHttpRequest. This class is used to send requests to the server over HTTP. Each JS XMLHttpRequest is first initialized by calling method open. This method has few parameters, but the third parameter specifies if the request should be asynchronous. If you change it to false, request will be synchronous.

    But GWT-RPC doesn't use this class directly, it using it via RpcRequestBuilder, and this class is not using XMLHttpRequest directly as well, it is using RequestBuilder.

    So what you'll need to do is to create customized version of RpcRequestBuilder and RequestBuilder (which will use XMLHttpRequest initialized to be synchronous).

    The you can set RPCRequest builder to your GWT-RPC service instance, by casting it to the ServiceDefTarget.

    Do you still want to have synchronous GWT-RPC requests?