Spring 3:任务命名空间:如何找出下次执行的时间?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
配置样式是生成调度程序和调度的底层 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 theTrigger
class.If you're willing to abandon
<task:scheduled>
and use the Quartz-Spring integration directly, then you can get access to theTrigger
(orTriggerBean
) and get what you want that way.对于那些不使用quartz而是spring的人,我已经通过扩展CronTrigger并记住nextExecutionTime来实现获得下一个执行时间:
For those not using quartz but spring, I have achieved to get next execution time by extending CronTrigger and remembering nextExecutionTime: