Codenameone:如何在发生网络异常时停止XFClient findAndWait请求,以便调用线程可以继续进行? [英] Codenameone: How to stop XFClient findAndWait request when network exception occurs so the calling thread can progress?

查看:54
本文介绍了Codenameone:如何在发生网络异常时停止XFClient findAndWait请求,以便调用线程可以继续进行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的CN1应用程序使用XFClient与运行Xataface的服务器上的mySQL数据库进行通信.计时器线程调用XFClient的findAndWait方法,然后在重复之前睡眠1秒钟.它可以在99%以上的时间中正常运行,但是有时应用程序遇到网络问题(错误的网关,ConnectionExceptions,SocketTimeOutException等)并停止与服务器通信,因为尽管有异常,但findAndWait仍在等待.发生异常时,findAndWait是否可以停止等待?

My CN1 app uses XFClient to communicate with mySQL database on a server running Xataface. The timer thread calls XFClient's findAndWait method and then sleeps 1 second before repeating. It works well 99+% of the time but sometimes the app encounters a network problem (Bad Gateway, ConnectionExceptions, SocketTimeOutException, etc.) and stops communicating with the server because findAndWait keeps waiting in spite of the exception. Is there a way for findAndWait to stop waiting when an exception occurs?

这是到目前为止我尝试过的事情:

Here’s what I’ve tried so far:

  1. 使用该错误.这样可以避免向用户显示错误消息但是findAndWait一直在等待
  2. Request.setTimeout(5000)在5秒后开始播放(基于在Android设备上构建的CodeNameOne中的SocketTimeoutException ).没有效果.findAndWait一直在等待.在NetworkManager和请求级别对此进行了尝试.
  3. 在NetworkManager的handleException中,在下面添加了代码(基于 https://github.com/codenameone/CodenameOne/issues/733 ).它编译并运行,但是findAndWait一直在等待.

  1. comsume the error. This suppresses the error message to the user but the findAndWait keeps waiting
  2. Request.setTimeout(5000) to kick out after 5 secs (based on SocketTimeoutException in CodeNameOne build on Android device) . No effect. The findAndWait keeps waiting. Tried this at the NetworkManager and at the request level.
  3. In handleException for NetworkManager, added code below (based on https://github.com/codenameone/CodenameOne/issues/733). It compiled and ran but findAndWait kept waiting.

    NetworkManager.getInstance().addErrorListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent evt) {
            System.out.println("got a network error.  will kill the connect request and consume it.");
            if (theXFClient.getcRMP() != null) {
            NetworkManager.getInstance().killAndWait(theXFClient.getcRMP());
            }
            evt.consume();
        }
    });

我正在模拟器上运行此程序,启动应用程序,然后通过在模拟器菜单中禁用网络连接来触发问题.我希望当我重新打开网络连接时,计时器线程能够继续运行,但是它仍然停滞.

I’m running this on the simulator, launching the app and then triggering the problem by disabling the network connection in the simulator menu. I was hoping the timer thread would carry on when I turn back on the network connection but it is still stalled.

顺便说一句-当应用程序通过模拟器菜单(和实际设备)暂停并恢复时,计时器从上次停止的地方开始计时.希望我们能找到一种方法,在发生网络故障时也能做到这一点.

BTW - When the app is paused and resumed via the simulator menu (and real devices), the timer picks up right where it left off. Hoping we can figure out a way that it can do that when network glitches happen as well.

Thx,迈克

推荐答案

我刚刚添加了可以在XFClient类和XFQuery类上设置的超时和读取超时支持.如果特定查询的XFClient和XFQuery都指定了超时,则XFQuery超时将优先.

I have just added timeout and read timeout support that can be set on both the XFClient class and the XFQuery class. If both XFClient and XFQuery for a particular query specify a timeout, then the XFQuery timeout will take precedent.

此更新即将在Codename One设置中进行,但是您现在可以直接从此处

This update will be in Codename One settings soon, but you can download this new build directly right now from here

这篇关于Codenameone:如何在发生网络异常时停止XFClient findAndWait请求,以便调用线程可以继续进行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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