1

我们有一个带有 ServletContextListener 的 Web 应用程序,它启动多个 ScheduledExecutorService 以通过远程位置的 SNMP 更新一些中继板,而不是运行 Web 应用程序的服务器。

但是,由于远程位置的 Internet 连接丢失,远程中继板可能不可用。

发生这种情况时,更新板的线程需要很长时间才能执行。如何监控 ScheduledExecutorService 的执行和运行?

现在我使用以下代码启动线程:

schedulerLichtsturing = Executors.newSingleThreadScheduledExecutor();
schedulerLichtsturing.scheduleAtFixedRate(new LichtsturingDaemon(), 0, 15, TimeUnit.SECONDS);

但是我已经看到线程在一段时间后不再运行,我不知道它为什么停止。由于未捕获的异常或其他原因?我怎样才能监控这个?

我还想监视线程的运行状态以及上次更新的时间。

我应该如何解决这个问题?

4

1 回答 1

0

你可以使用这样的东西:

private void runThrowException() throws Exception {
  throw new Exception("This allways throw an exception.");
}

Runnable runnable = new Runnable() {
  public void run() {
    try {
      runThrowException();
    }catch (Exception exception) {
      System.out.println("Catch exception: " + exception.toString());
    }
  }
};

ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
service.scheduleAtFixedRate(runnable, 1, 1, TimeUnit.SECONDS);

如果子函数返回异常,则调度的执行器继续运行。

我知道,它不会影响监控问题...

于 2021-07-06T12:40:40.217 回答