Spring 3:任务命名空间:如何找出下次执行的时间?

发布于 2024-08-30 21:31:44 字数 177 浏览 5 评论 0原文

我有一个 bean,它有一个在上下文配置中使用 按计划执行的方法。

有没有办法让我在执行该方法期间找到下一次计划运行的时间?

同样的方法也手动执行,并且接收调度程序信息的机制可能不会中断来自调度程序外部的执行...

I have a bean that has a method executed on a schedule using <task:scheduled> in the context configuration.

Is there a way for me to find the time of the next scheduled run during execution of that method?

The same method is also executed manually, and the mechanism for receiving scheduler information may not break executions from outside the scheduler...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

卸妝后依然美 2024-09-06 21:31:44

配置样式是生成调度程序和调度的底层 Spring 工厂 bean 的便捷快捷方式。为了方便起见,它很有用,但比直接使用底层调度程序工厂灵活性要差得多。

话虽如此,调度程序本身需要通过其 API 公开“下一次触发时间”信息,而这取决于实现。例如,我没有找到从标准 ScheduledExecutorService 实现中获取此信息的方法。

然而,Quartz 确实通过 Trigger 类上的 getNextFireTime() 方法公开了这一点。

如果您愿意放弃 直接使用 Quartz-Spring 集成,然后就可以访问 Trigger (或 TriggerBean)并以这种方式得到你想要的东西。

The <task:scheduled> configuration style is a convenient shortcut on to underlying Spring factory beans that generate schedulers and schedules. As a convenience, it's useful, but is much less flexible than using the underlying scheduler factories directly.

Having said that, the schedulers themselves would need to expose the "next fire time" information via their API, and that's implementation-dependent. For example, I don't see a way to get this information from the standard ScheduledExecutorService implementations.

Quartz, however, does expose this, via the getNextFireTime() method on the Trigger class.

If you're willing to abandon <task:scheduled> and use the Quartz-Spring integration directly, then you can get access to the Trigger (or TriggerBean) and get what you want that way.

温柔一刀 2024-09-06 21:31:44

对于那些不使用quartz而是spring的人,我已经通过扩展CronTrigger并记住nextExecutionTime来实现获得下一个执行时间:

import org.springframework.scheduling.support.CronTrigger;

public class MyCronTrigger extends CronTrigger {
    public MyCronTrigger(String expression) {
        super(expression);
    }

    @Override
    public Date nextExecutionTime(TriggerContext triggerContext) {
        Date date = super.nextExecutionTime(triggerContext);
        nextExecutionTime = new Date(date.getTime()); //remember
        return date;
    }
}

For those not using quartz but spring, I have achieved to get next execution time by extending CronTrigger and remembering nextExecutionTime:

import org.springframework.scheduling.support.CronTrigger;

public class MyCronTrigger extends CronTrigger {
    public MyCronTrigger(String expression) {
        super(expression);
    }

    @Override
    public Date nextExecutionTime(TriggerContext triggerContext) {
        Date date = super.nextExecutionTime(triggerContext);
        nextExecutionTime = new Date(date.getTime()); //remember
        return date;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文