如果GWT RPC请求失败(或如何创建持久RPC请求),如何重新发送? [英] How to resend a GWT RPC request if it fails (or how to create persistent RPC request)?

查看:98
本文介绍了如果GWT RPC请求失败(或如何创建持久RPC请求),如何重新发送?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要重试发送一个GWT RPC请求,如果它失败(HTTP 200以外的任何响应代码)。理由很复杂,所以我不会详细说明。到目前为止,我在同一地点处理所有请求响应,如下所示:

  //我们覆盖RpcRequestBuilder.doSetCallback方法并强制你的服务使用它
//如果需要的话,我们可以读取响应头文件。
((ServiceDefTarget)serviceRPC).setRpcRequestBuilder(new RpcRequestBuilder(){
$ b @Override
保护无效doSetCallback(RequestBuilder rb,最终RequestCallback回调){
super。 doSetCallback(rb,new RequestCallback(){
$ b $ @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请求的高级参数,我宁愿使用已经流式传输并准备重新发送的请求内容。



任何想法?

解决方案

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

设置您的特殊请求生成器来捕获所有请求响应,但保留请求生成器。

<($)> $ $ $ $ $ $ $ $保护无效doSetCallback(RequestBuilder rb,最终的RequestCallback回调函数){
final RequestBuilder requestBuilder = rb;
super.doSetCallback(rb,new RequestCallback(){
$ b @Override
public void onResponseReceived(Request request,
响应响应){
httpResponseOkHandler(requestBuilder,callback,request,response);
}
$ b $ @Override
public void onError(Request request,Throwable exception) {
httpResponseErrorHandler(requestBuilder,callback,request,exception);
}
});
}
});

2-现在使用请求构建器根据需要多次发送请求。一件好事就是请求生成器已经设置好了,数据被序列化了,这样就避免了存储POJO未序列化的数据。

  // We有一些服务器HTTP错误响应(我们只使用RPC时服务器端代码为200)
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(无法启动异步服务调用 - 检查网络连接,null);
callback.onError(request,iex);
}
} else {
callback.onResponseReceived(request,response);
}

这对我来说很好,在你自己的冒险中使用它! p>

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 treat all request responses in the same place like this:

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

So, using httpResponseOkHandler method, I can catch HTTP failures. But, is there a way to "rethrow" the Request, i.e. try again? I don't want to store the high level parameters of the RPC request, I would prefer to use the request content that was already streamed and ready to resend.

Any ideas?

解决方案

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!

这篇关于如果GWT RPC请求失败(或如何创建持久RPC请求),如何重新发送?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆