问题
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:
- comsume the error. This suppresses the error message to the user but the findAndWait keeps waiting
- 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.
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, Mike
回答1:
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.
This update will be in Codename One settings soon, but you can download this new build directly right now from here
来源:https://stackoverflow.com/questions/61647990/codenameone-how-to-stop-xfclient-findandwait-request-when-network-exception-occ