5

如果失败(除 HTTP 200 之外的任何响应代码),我需要重试发送 GWT RPC 请求。原因很复杂,我就不细说了。到目前为止,我将所有请求响应都放在同一个地方,如下所示:

    // We override the RpcRequestBuilder.doSetCallback method and force your service to use it
    // With this we can read the response headers if we need to.
    ((ServiceDefTarget)serviceRPC).setRpcRequestBuilder(new RpcRequestBuilder() {

        @Override
        protected void doSetCallback(RequestBuilder rb, final RequestCallback callback) {
            super.doSetCallback(rb, new RequestCallback() {

                @Override
                public void onResponseReceived(Request request,
                        Response response) {
                    httpResponseOkHandler(callback, request, response);
                }

                @Override
                public void onError(Request request, Throwable exception) {
                    httpResponseErrorHandler(callback, request, exception);
                }
            });
        }
    });

因此,使用httpResponseOkHandler方法,我可以捕获 HTTP 故障。但是,有没有办法“重新抛出”请求,即再试一次?我不想存储 RPC 请求的高级参数,我更愿意使用已经流式传输并准备重新发送的请求内容。

有任何想法吗?

4

1 回答 1

4

好吧,我自己找到了答案。所以它毕竟很整洁。在负载重的医院环境中工作,网络往往不可靠。所以这就是为什么我需要在放弃之前重新发送几次 rpc 请求。这是解决方案:

1-为您设置特殊请求构建器以捕获所有请求响应但保留请求构建器。

    ((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-现在使用请求生成器发送请求任意多次。一件好事是已经设置了请求构建器并且数据被序列化,这避免了存储 POJO 未序列化的数据。

    // 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);         
    }

这对我来说很好,使用它需要您自担风险!

于 2012-06-17T22:16:12.807 回答