我在Spring启动应用程序中使用Quartz。 我需要设置一个触发器,它会在创建后立即触发,然后每隔30天触发一次。
所以,我创建了jobDetail
JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
jobDetailFactoryBean.setJobClass(CheckContract.class);
jobDetailFactoryBean.setName("JobDetail" + contract.getContractId());
jobDetailFactoryBean.setGroup("timeoutJobDetail");
jobDetailFactoryBean.setDescription("Job for timeout");
jobDetailFactoryBean.setDurability(false);
Map<String, Long> map = new HashMap<String, Long>();
map.put("contractId", contract.getContractId());
map.put("userId", user.getUserId());
jobDetailFactoryBean.setJobDataAsMap(map);
jobDetailFactoryBean.afterPropertiesSet();
JobDetail jobDetail = jobDetailFactoryBean.getObject();
然后我创建触发器并将两者传递给调度程序
SimpleTriggerFactoryBean trigger = new SimpleTriggerFactoryBean();
trigger.setName("checkContract" + contract.getContractId());
trigger.setGroup("contractTriggers");
trigger.setJobDetail(jobDetail);
trigger.setStartTime(new Date());
trigger.setRepeatInterval(30 * 24 * 60 * 60 *1000);
trigger.afterPropertiesSet();
scheduler.getScheduler().scheduleJob(jobDetail, trigger.getObject());
创建触发器后,我得到此异常
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.IllegalArgumentException: Repeat interval must be >= 0]
指向trigger.setRepeatInterval(30 * 24 * 60 * 60 *1000);
我真的不知道......
答案 0 :(得分:0)
要构建区间构建器,请尝试使用CalendarIntervalScheduleBuilder
示例:
public Trigger get30DayTrigger(TriggerKey triggerKey, Date startTime) {
CalendarIntervalScheduleBuilder intervalBuilder = CalendarIntervalScheduleBuilder
.calendarIntervalSchedule()
.withMisfireHandlingInstructionDoNothing();
intervalBuilder.withIntervalInDays(30);
TriggerBuilder<?> triggerBuilder = TriggerBuilder
.newTrigger()
.withIdentity(triggerKey)
.withSchedule(intervalBuilder);
if (startTime != null) {
triggerBuilder.startAt(startTime);
}
return triggerBuilder.build()
}
然后你可以做类似
的事情TriggerKey key = new TriggerKey("checkContract" + contract.getContractId(), "contractTriggers");
Trigger trigger = get30DayTrigger(key, new Date());
scheduler.getScheduler().scheduleJob(jobDetail, trigger);
答案 1 :(得分:0)
30 * 24 * 60 * 60 * 1000 溢出整数限制。这就是你看到错误的原因。尝试执行类似下面的操作,将结果转换为long
trigger.setRepeatInterval(30L * 24 * 60 * 60 *1000);