简体   繁体   English

Java:使用 ScheduledExecutorService 忽略 RuntimeException

[英]Java: Ignored RuntimeException with ScheduledExecutorService

Introduction介绍

I'm currently working on a project that logs data from a website to a database every x hours.我目前正在从事一个项目,该项目每 x 小时将数据从网站记录到数据库。

But when the database connection properties are not good, the program does not throw a RuntimeException pretends everything is fine.但是当数据库连接属性不好时,程序不会抛出RuntimeException假装一切正常。

My code:我的代码:

    private static ScheduledExecutorService executeWithPeriod(Runnable runnable, int period) {
        ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
        executor.schedule(runnable, Math.max(1, period), TimeUnit.HOURS);
        return executor;
    }

The runnable takes care of getting data and saving it to the database. runnable负责获取数据并将其保存到数据库中。

The possible throwed RuntimeException :可能抛出的RuntimeException

  • DockerSecretVariableNotFoundException DockerSecretVariableNotFoundException
  • EnvironmentVariableNotFoundException EnvironmentVariableNotFoundException

All these exceptions extends RuntimeException所有这些异常都扩展RuntimeException

Someone have the solution to throws the RuntimeException to the main thread?有人有将RuntimeException抛出到主线程的解决方案吗?

Well, since the task is just to tnrow a RuntimeException, how about this?好吧,既然任务只是处理一个 RuntimeException,那么这个怎么样?

class MyRunnable implements Runnable {
    public void run() {
        throw new RuntimeException("mark my words");
    }
}

Rest assured this code throws an exception. Rest 确保此代码引发异常。 You can try using this code:您可以尝试使用以下代码:

public static void main(String[] args) {
    new MyRunnable().run();
}

However usually Runnables are run in their own thread:但是通常 Runnables 在它们自己的线程中运行:

new Thread(new MyRunnable()).start()

So now the exception is thrown as well.所以现在也抛出了异常。 However it terminates the thread - and that's it.然而,它终止了线程 - 就是这样。

I am coming back to my question from the comment: What are you trying to achieve?我从评论中回到我的问题:你想要实现什么?

You haven't demonstrated a need for multiple threads, so you can eliminate the complexity of signaling between concurrent threads.您还没有证明需要多个线程,因此您可以消除并发线程之间信号传输的复杂性。 Use a single thread instead:改为使用单个线程:

final class YourTask {

    public static void main(String... args) throws InterruptedException {
        YourTask task = new YourTask();
        while (true) {
            try {
                task.doYourThing();
            } catch (Exception ex) {
                ex.printStackTrace();
                break;
            }
            TimeUnit.HOURS.sleep(1L);
        }
    }

    private void doYourThing() throws Exception {
        System.out.println("I'm saving data from a website to a database!");
        throw new RuntimeException("Oh no! I can't read my configuration!");
    }

}

Note that this solution "tells the user about the problem" and "stops the whole program," as specified.请注意,此解决方案按照指定“告诉用户问题”和“停止整个程序”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM