How to resend a GWT RPC request if it fails (or how to create persistent RPC request)?

前端 未结 1 907
耶瑟儿~
耶瑟儿~ 2021-01-06 02:32

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

相关标签:
1条回答
  • 2021-01-06 03:07

    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!

    0 讨论(0)
提交回复
热议问题