Dispose - Http IOException

Just trying to iron out the last few errors from my binding.
My Bridge handler takes care of all polling (external api) and updates the Thing handler channels by using httpUtil in a separate class Http and calling:

Http http = new Http();
String response = http.httpClient("features", data, "application/json", "");
public String httpClient(@Nullable String type, @Nullable InputStream data, @Nullable String other,
            @Nullable String groupId) {
        String response = "";
        try {
            response = HttpUtil.executeUrl(method(type), url(type, groupId), getHeader(type), data, other, 100000);
        } catch (IOException e) {
            logger.warn("Http Util threw an error, its possible you just restarted the binding");
        }
        return response;
    }

This all runs in a separate thread:

Runnable runnable = new Runnable() {
            @Override
            public void run() {
                polling();
            }
        };
        refreshTask = scheduler.scheduleWithFixedDelay(runnable,10, pollingInterval, TimeUnit.MILLISECONDS);
    }

If I remove the binding on the disposal of the Bridge handler httpUtil throws an IOException (custom error message)even though I cancel the refresh task as the first part of the dispose:

2020-03-10 09:35:52.562 [DEBUG] [rf.internal.handler.LWAccountHandler] - Running dispose()

2020-03-10 09:35:52.564 [WARN ] [ab.binding.lightwaverf.internal.Http] - Http Util threw an error, its possible you just restarted the binding

2020-03-10 09:35:52.564 [DEBUG] [rf.internal.handler.LWAccountHandler] - JSON Is Normal, Response is: 

How do i stop this from happening?

A few things:

  1. Don’t use logger.warn here - you only need debug
  2. I’d definitely log the error as well as you are not providing any information beyond a guess (ie possibly restarted). If this was running on someones system - it could have thrown for any number of reasons and you should be providing that reason.
  3. You can see from your timestamps that the polling method was running at the same time as dispose. When you run cancel - all it does is signal to the thread that it should stop running. The thread doesn’t stop running unless you check for it (the scheduler will check and not run another instance - but that does nothing for currently running threads). I’d change your runnable to show something like if (!Thread.currentThread().isInterrupted()) { polling() } and if you really want to disable the message (I personally wouldn’t do this) - in the catch if (!Thread.currentThread().isInterrupted()) { logger.... }

Personally - I’d leave the catch statement alone and only put the interrupted in the polling runnable (or in the polling method itself depending on how complicated it is)…

2 Likes

Thanks @tmrobert8,
Ill have a look when I get a second regarding the thread.
The logging levels were only for me testing, and i have since added in the error message to the log.
This is all new to me so a very big learning curve.

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.