您的问题是配置 startTime。startTime 是指应该发生触发器的时间。由于日期较旧,这会导致调度程序失火,默认行为是调度程序立即重新触发。
移除 setStartTime,默认行为是将 startTime 设置为当前时间,第一个触发时间将匹配到开始时间之后的 cron 触发器,所以这个星期四。
我通过一起快速的小测试来验证:
public class Test {
public static void main(String[] args) throws ParseException, SchedulerException {
String groupName = "group";
String cronExpression = "0 13 13 ? * THUR";
CronTrigger cronTrigger = new CronTrigger("trigger_" + groupName, groupName, cronExpression);
cronTrigger.setStartTime(new Date(0));
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
JobDetail detail = new JobDetail("testJob", groupName, TestJob.class);
scheduler.scheduleJob(detail, cronTrigger);
scheduler.start();
try {
Thread.sleep(50001);
} catch (Exception ignore) {
}
}
public static class TestJob implements Job {
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println("TEST");
}
}
}
删除 setStartTime 时,我的打印消息不会触发。有了它,打印消息就会触发。