Sat*_*sam 3 spring scheduled-tasks job-scheduling spring-scheduled
我正在做一个计划项目,该项目定期执行多个作业。我正在使用cron调度,如下例所示。作业已成功执行,没有问题。但是,出于需求,我想计算并保留DB中计划作业的下一次运行时间。对于以下配置,是否有解决方案来获取作业的上一次和下一次启动时间?
配置示例:
import java.util.Date;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
@Component
@EnableScheduling
public class DemoServiceBasicUsageCron
{
@Scheduled(cron="1,3,30,32 * * * * ?")
public void demoServiceMethod()
{
System.out.println("Curent date time is - "+ new Date());
}
}
Run Code Online (Sandbox Code Playgroud)
您可以使用CronSequenceGenerator。
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.support.CronSequenceGenerator;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.Date;
@Component
public class MyJob {
public static final String CRON_EXPRESSION = "0 0 5 * * *";
@PostConstruct
public void init() {
CronSequenceGenerator cronTrigger = new CronSequenceGenerator(CRON_EXPRESSION);
Date next = cronTrigger.next(new Date());
System.out.println("Next Execution Time: " + next);
}
@Scheduled(cron = CRON_EXPRESSION)
public void run() {
// Your custom code here
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3510 次 |
| 最近记录: |