Java 运行具有特定开始、结束日期和时间限制的Quartz调度程序作业
我正在使用Quartz调度程序执行重复性任务,但我面临一个问题。在我的服务器端,我的用户希望指定一些日期范围,如从2013-09-27到2013-09-30的日期范围 说明: 从Java 运行具有特定开始、结束日期和时间限制的Quartz调度程序作业,java,cron,quartz-scheduler,Java,Cron,Quartz Scheduler,我正在使用Quartz调度程序执行重复性任务,但我面临一个问题。在我的服务器端,我的用户希望指定一些日期范围,如从2013-09-27到2013-09-30的日期范围 说明: 从2013-09-27到2013-09-30运行作业,但只能在09:00 AM到12:00 PM之间运行 我在为它编写Cron表达式时遇到了麻烦,而且我的用户是非技术性的,所以我的用户希望我从这两个时间戳值自动创建Cron表达式 请帮帮我。如果有别的办法,请告诉我 我在谷歌上看到了很多资源,但还是什么也找不到 链接:
2013-09-27
到2013-09-30
运行作业,但只能在09:00 AM到12:00 PM之间运行
我在为它编写Cron表达式时遇到了麻烦,而且我的用户是非技术性的,所以我的用户希望我从这两个时间戳值自动创建Cron表达式
请帮帮我。如果有别的办法,请告诉我
我在谷歌上看到了很多资源,但还是什么也找不到
链接:
更新
我已经写了一个,但不起作用
|------------------------------------------------------------------|
| Seconds | Minutes | Hours | DayOfMonth | Month | DayOfWeek | Year|
| | | | | | | |
| 0 | 0 | 9-12 | 27-30 | 9 | ? | 2013|
|------------------------------------------------------------------|
正在尝试将2013-09-27
映射到2013-09-30
,但只能在09:00 AM-12:00 PM
已更新
我也试过用它来运行
Trigger trigger = TriggerBuilder.newTrigger().withIdentity(NAME_TRIGGER_TASK_UPDATER, GROUP_TASK_TRIGGER)
.withSchedule(CronScheduleBuilder.cronSchedule("0 0 9-12 19-22 10 ? *")).build();
但它不会给出任何错误,也不会进入我的作业执行方法
cronSchedule("0 0 9-12 ? * ?") throws invalid schedule exception.
下面的代码运行它时不考虑开始和结束日期
String startDateStr = "2013-09-27 00:00:00.0";
String endDateStr = "2013-09-31 00:00:00.0";
Date startDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").parse(startDateStr);
Date endDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").parse(endDateStr);
CronTrigger cronTrigger = newTrigger()
.withIdentity("trigger1", "testJob")
.startAt(startDate)
.withSchedule(CronScheduleBuilder.cronSchedule("0 0 9-12 * * ?"))
.endAt(endDate)
.build();
public class CronJob {
public static void main(String[] args) throws ParseException, SchedulerException {
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
JobDetail job = newJob(TestJob.class)
.withIdentity("cronJob", "testJob")
.build();
String startDateStr = "2013-09-27 00:00:00.0";
String endDateStr = "2013-09-31 00:00:00.0";
Date startDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").parse(startDateStr);
Date endDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").parse(endDateStr);
CronTrigger cronTrigger = newTrigger()
.withIdentity("trigger1", "testJob")
.startAt(startDate)
.withSchedule(CronScheduleBuilder.cronSchedule("0 0 9-12 * * ?").withMisfireHandlingInstructionDoNothing())
.endAt(endDate)
.build();
scheduler.scheduleJob(job, cronTrigger);
scheduler.start();
}
public static class TestJob implements Job {
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println("this is a cron scheduled test job");
}
}
}
当你说它不工作时,你得到的错误是什么
您可以尝试以下代码(编辑:适用于Quartz 2.2)。这种方法不在cron表达式中指定开始/结束日期和年份,而是使用触发器方法来指定它们。
(注意:我还没有亲自测试过,请告诉我它是否适合您)
编辑:
我有机会测试这段代码,我运行下面的代码并不断更改系统时钟,从开始到结束日期的上午9点到凌晨12点,所有触发器都成功
String startDateStr = "2013-09-27 00:00:00.0";
String endDateStr = "2013-09-31 00:00:00.0";
Date startDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").parse(startDateStr);
Date endDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").parse(endDateStr);
CronTrigger cronTrigger = newTrigger()
.withIdentity("trigger1", "testJob")
.startAt(startDate)
.withSchedule(CronScheduleBuilder.cronSchedule("0 0 9-12 * * ?"))
.endAt(endDate)
.build();
public class CronJob {
public static void main(String[] args) throws ParseException, SchedulerException {
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
JobDetail job = newJob(TestJob.class)
.withIdentity("cronJob", "testJob")
.build();
String startDateStr = "2013-09-27 00:00:00.0";
String endDateStr = "2013-09-31 00:00:00.0";
Date startDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").parse(startDateStr);
Date endDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S").parse(endDateStr);
CronTrigger cronTrigger = newTrigger()
.withIdentity("trigger1", "testJob")
.startAt(startDate)
.withSchedule(CronScheduleBuilder.cronSchedule("0 0 9-12 * * ?").withMisfireHandlingInstructionDoNothing())
.endAt(endDate)
.build();
scheduler.scheduleJob(job, cronTrigger);
scheduler.start();
}
public static class TestJob implements Job {
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println("this is a cron scheduled test job");
}
}
}
如果上述代码不起作用,请尝试将cronSchedule(“0 0 9-12**?”)
替换为cronSchedule(“0 0 9-12?*?”)
您的cron表达式在我看来是正确的。应用程序中的其他quartz触发器和作业是否按预期执行?不,仅运行以下命令:-(Quartz Scheduler Java有什么不同吗?我已经尝试了上面的代码,它工作了,唯一的问题是触发器执行的时间比它应该触发的时间晚20秒。这是Quartz问题还是外部原因?我更改了时钟系统以跟上执行的日期。这可能是原因之一吗确保系统时钟设置正确