gpt4 book ai didi

java - 我需要在 finally block 中关闭 Quartz 调度程序吗?

转载 作者:行者123 更新时间:2023-11-29 09:16:01 39 4
gpt4 key购买 nike

我正在使用来自 StdSchedulerFactory 的 Quartz 2.0.1 调度程序。

我在我的代码中捕获了 SchedulerException

我应该在 finally block 中关闭调度程序吗:

} finally {
scheduler.shutdown();
}

还是应该在 try block 中关闭?

shutdown 方法会抛出 SchedulerException,所以 shutdown 似乎不应该在 finally block 中。

最佳答案

在任何情况下,您都不必在 finally block 中执行此操作,因为如果调度程序成功启动,它不会抛出 SchedulerException,因此如果您到达 SchedulerException 这意味着调度程序从未启动。因此,您不应关闭从未启动的调度程序。

这是项目 homepage 中的示例程序.

public class QuartzTest {

public static void main(String[] args) {

try {
// Grab the Scheduler instance from the Factory
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

// and start it off
scheduler.start();

scheduler.shutdown();

} catch (SchedulerException se) {
se.printStackTrace();
}
}
}

此外,从上面的链接,

Once you obtain a scheduler using StdSchedulerFactory.getDefaultScheduler(), your application will not terminate until you call scheduler.shutdown(), because there will be active threads.

关于java - 我需要在 finally block 中关闭 Quartz 调度程序吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9395490/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com