我需要在finally块中关闭Quartz调度程序吗?

发布于 2025-01-08 14:38:35 字数 315 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紫﹏色ふ单纯 2025-01-15 14:38:35

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

以下是项目主页中的示例程序。

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();
        }
    }
}

另外,从上面的链接,

一旦使用 StdSchedulerFactory.getDefaultScheduler() 获取调度程序,您的应用程序将不会终止,直到您调用 Scheduler.shutdown(),因为会有活动线程。

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 of SchedulerException 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.

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();
        }
    }
}

Also, from the above link,

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

梦里°也失望 2025-01-15 14:38:35

根据上面的答案,如果除了启动/关闭之外还有其他代码,它仍然可能会遇到问题。例如,如果您有这样的情况:

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();
            System.out.println(args[5]);
            scheduler.shutdown();

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

应用程序永远不会调用 shutdown,因为您最终会遇到 ArrayIndexOutOfBoundsException(或类似的情况)。有很多方法可以解决这个问题,但最简单的方法可能是将所有中间代码包装在异常处理程序中并在那里“处理”事物。例如:
公共类 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();
        try {
             System.out.println(args[5]);
        } catch (Exception e) {
             e.printStackTrace();
        }
        scheduler.shutdown();

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

}

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:

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();
            System.out.println(args[5]);
            scheduler.shutdown();

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

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 {

public static void main(String[] args) {

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

        // and start it off
        scheduler.start();
        try {
             System.out.println(args[5]);
        } catch (Exception e) {
             e.printStackTrace();
        }
        scheduler.shutdown();

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

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文