如何强制 Vaadin 客户端引擎重试向服务器发送请求? [英] How can I force Vaadin client engine to retry sending requests to the server?

查看:19
本文介绍了如何强制 Vaadin 客户端引擎重试向服务器发送请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在试验 Vaadin Java 框架,我注意到客户端引擎不会重试向服务器发送请求.当移动互联网网络较弱或不一致时,最好继续重试发送请求,而不是放弃.

I'm experimenting with the Vaadin Java framework at the moment and I've noticed that the client engine does not retry sending requests to the server. When mobile internet network is weak or inconsistent it would be good to keep retrying sending of requests rather than giving up.

有人知道如何在 Vaadin 中实现这一点吗?

Does any one know how to achieve this in Vaadin?

推荐答案

扩展 ApplicationConnection 和覆盖 doAjaxRequest 应该足以实现您想要做的事情.像这样:

Extending ApplicationConnection and overriding doAjaxRequest should be enough to achieve what you're trying to do. Something like this:

public class MyApplicationConnection extends ApplicationConnection {

    private final Logger logger = Logger
            .getLogger(MyApplicationConnection.class.getName());

    @Override
    protected void doAjaxRequest(final String uri, final JSONObject payload,
            final RequestCallback requestCallback) throws RequestException {
        // wrap the original request callback handle the retries
        RequestCallback myRequestCallback = new RequestCallback() {

            private int retries = 3;

            @Override
            public void onResponseReceived(Request request, Response response) {
                int status = response.getStatusCode();
                if (status / 100 == 2) { // 2xx Successful
                    requestCallback.onResponseReceived(request, response);
                } else {
                    handleError(request, response, null);
                }
            }

            @Override
            public void onError(Request request, Throwable exception) {
                handleError(request, null, exception);
            }

            private void handleError(Request request, Response response,
                    Throwable exception) {
                if (retries == 0) {
                    logger.info("Ajax request failed.");
                    if (response == null) {
                        requestCallback.onError(request, exception);
                    } else {
                        requestCallback.onResponseReceived(request, response);
                    }
                } else {
                    try {
                        logger.info("Ajax request failed, retrying " + retries
                                + " more times.");
                        retries--;
                        MyApplicationConnection.super.doAjaxRequest(uri,
                                payload, this);
                    } catch (RequestException e) {
                        // something went wrong in the ajax send() call, so no
                        // point in retrying
                        logger.warning("Sending Ajax request failed. Cause: "
                                + e.getMessage());
                        requestCallback.onError(request, exception);
                    }
                }
            }
        };
        super.doAjaxRequest(uri, payload, myRequestCallback);
    }
}

在你的 *.gwt.xml 文件中:

And in your *.gwt.xml file:

<replace-with class="com.example.client.MyApplicationConnection">
    <when-type-is class="com.vaadin.client.ApplicationConnection"/>
</replace-with>

您可能还想在 handleError 方法中添加一个 Timer 或其他东西,这样当网络关闭时,请求会等待一段时间才能恢复.不过应该很简单.

You might also want to add a Timer or something to the handleError method, so that when the network is down, the request will wait for a while for it to come back up. It should be fairly trivial though.

这篇关于如何强制 Vaadin 客户端引擎重试向服务器发送请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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