Quartz 是一个彻底由java编写的开源做业调度框架,在Spring + SpringMVC 或者Spring Boot环境中,咱们均可以用它来实现定时任务策略。java
当须要在规定的时间执行一次或在规定的时间段以必定的时间间隔重复触发执行Job时,SimpleTrigger 就能够知足要求,其中重复次数RepeatCount 和重复间隔RepeatInterval 是比较重要的两个属性。spring
咱们能够根据业务须要设置对应的重复次数和重复间隔,也能够将重复次数和重复间隔都设置为0,可是若是咱们单独把重复间隔设置为0,将会出现以下程序运行错误:框架
2019-04-15 20:51:31.948 WARN 9984 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'schedulerFactoryBean' defined in class path resource [com/chb/quartzdemo/QuartzConfig.class]: Invocation of init method failed; nested exception is org.quartz.SchedulerException: Repeat Interval cannot be zero.spa
Invocation of init method failed; nested exception is org.quartz.SchedulerException: Repeat Interval cannot be zero. (该语句的意思是:调用init方法失败,在嵌套SchedulerException的时候:重复间隔不能为零)code
@Bean
SimpleTriggerFactoryBean simpleTriggerFactoryBean(){
SimpleTriggerFactoryBean bean =new SimpleTriggerFactoryBean();
bean.setStartTime(new Date());
bean.setRepeatCount(5);//重复的次数
bean.setJobDetail(methodInvokingJobDetailFactoryBean().getObject());
bean.setRepeatInterval(0);//重复间隔
return bean;
}
复制代码
这是由于当咱们设置了重复次数以后,若是没有了重复间隔的时间,系统就不知道要多久重复一次了,那么也就无法实现定时任务的功能了。开发
聚沙成塔,滴水穿石!get