我需要在finally块中关闭Quartz调度程序吗?
我正在使用 StdSchedulerFactory 中的 Quartz 2.0.1 调度程序。
我在代码中捕获了 SchedulerException
。
我应该在finally块中关闭调度程序:
} finally {
scheduler.shutdown();
}
还是应该在try块中关闭调度程序?
shutdown 方法可能会抛出 SchedulerException
,因此看来 shutdown 不应该在 finally 块中。
I am using Quartz 2.0.1 scheduler from StdSchedulerFactory
.
I am catching SchedulerException
in my code.
Should I shutdown the scheduler in a finally block:
} finally {
scheduler.shutdown();
}
or should I do the shutdown in the try block?
The shutdown method can throw SchedulerException
, so it seems the shutdown should not be in the finally block.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在任何情况下,您都不必在finally块中执行此操作,因为如果调度程序成功启动,它不会抛出
SchedulerException
,因此如果您到达SchedulerException的catch块
这意味着调度程序从未启动。因此,您不应该关闭从未启动的调度程序。以下是项目主页中的示例程序。
另外,从上面的链接,
You don't have to do it in the finally block in any case because if the scheduler is started successfully it won't throw the
SchedulerException
and hence if you are reaching the catch block ofSchedulerException
that means the scheduler was never started. So, you should not shutdown a scheduler that is never started.Here is a sample program from the project homepage.
Also, from the above link,
根据上面的答案,如果除了启动/关闭之外还有其他代码,它仍然可能会遇到问题。例如,如果您有这样的情况:
应用程序永远不会调用 shutdown,因为您最终会遇到 ArrayIndexOutOfBoundsException(或类似的情况)。有很多方法可以解决这个问题,但最简单的方法可能是将所有中间代码包装在异常处理程序中并在那里“处理”事物。例如:
公共类 QuartzTest {
}
Riffing on the above answer, it may still run into problems if there is other code than the start/shutdown. For example if you have something like this:
The application will never call shutdown because you'll end up with an ArrayIndexOutOfBoundsException (or something like that). There are quite a few ways to solve this, but this simplest would likely be to wrap all intermediate code in an exception handler and "handle" things there. For example:
public class QuartzTest {
}