开发者

Spring 3: task namespace: How to find out time of next execution?

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 开发者_StackOverflow社区information may not break executions from outside the scheduler...


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.


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;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜