Spring 3: task namespace: how to find out next run time?

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

Is there a way to find the time for the next scheduled run during this method?

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

+3
source share
2 answers

The configuration style <task:scheduled>is a convenient shortcut for the basic Spring factory beans, which generates schedulers and schedules. For convenience, this is useful, but much less flexible than directly using basic schedulers.

Having said that, the planners themselves will have to provide information about the "next fire time" through their API and depending on the implementation. For example, I see no way to get this information from standard implementations ScheduledExecutorService.

Quartz, however, reveals this using the method getNextFireTime()in the class Trigger.

If you are ready to give up <task:scheduled>and directly use the Quartz-Spring integration , then you can access Trigger(or TriggerBean) and get what you want in this way.

+2

, , 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;
    }
}
+2

Source: https://habr.com/ru/post/1743513/


All Articles