如何使用 Spring Boot 验证 cron 作业 [英] How to validate cron job with spring boot
本文介绍了如何使用 Spring Boot 验证 cron 作业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要一个每 00:10 am 执行一次的 cron 作业
I want to have a cron job that executes every 00:10 am
根据在线验证器,这是正确的表达式0 0/10 0/00 ?* * *"但它有?"而 spring 拒绝了这一点.
according to online validators this is the correct expression "0 0/10 0/00 ? * * *" but it has '?' and spring rejects this.
如何正确验证我的 cron 作业?我的假设是这应该工作 '0 0/10 0/00 * * *' 但它没有,请有人指出我正确的方向吗?我究竟做错了什么 ?我想在午夜 10 分钟后完成一项任务
how can i validate my cron job properly ? my assumption is this should work '0 0/10 0/00 * * *' but it doesnt, please can someone point me in the right direction ? what am i doing wrong ? i will like to have a task 10 minutes after midnight
@Transactional
@Scheduled(cron = "0 0/10 1/00 * * *")
public void invalidateOutdatedPolicies() {
InsurancePolicyStatus policyStatus =
System.out.println("Scheduled task done");
}``
推荐答案
你可以像这样验证模式
public class ValidateCrontab {
public static void main(String[] args) {
// {second} {minute} {hour} {day} {month} {week} {year (optional)}
// Second time, day, month and week, these 6 items are mandatory.
CronSequenceGenerator cron1 = new CronSequenceGenerator("0 05 10 23 * ?");
CronSequenceGenerator cron2 = new CronSequenceGenerator("0 05 08 ? * MON-FRI");
Calendar cal = Calendar.getInstance();
//cal.add(Calendar.DATE, 2); // add two days to current date
SimpleDateFormat sdf = new SimpleDateFormat("yyyy MMM dd HH:mm:ss");
System.out.println("current date " + sdf.format(cal.getTime()) +"\n ----");
System.out.println("Next cron trigger date cron1 " + cron1.next(cal.getTime()));
System.out.println("Next cron trigger date cron2 " + cron2.next(cal.getTime()));
}
}
这篇关于如何使用 Spring Boot 验证 cron 作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文