Java 在特定的开始、结束日期和时间限制内运行 Quartz 调度程序作业
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19051350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run Quartz Scheduler Job with specific start, end date and within time constraints
提问by AZ_
I am using Quartz-Scheduler for repetitive tasks but I am facing a trouble. In my server side my user wants to specify some date range like From2013-09-27
with in09:00 AM - 12:00 PM
to2013-09-30
我正在使用 Quartz-Scheduler 执行重复性任务,但遇到了麻烦。在我的服务器端,我的用户想要指定一些日期范围,例如From 2013-09-27
with in 09:00 AM - 12:00 PM
to2013-09-30
Explanation:
解释:
Run a job from 2013-09-27
to 2013-09-30
but only between 09:00 AM - 12:00 PM
从2013-09-27
到2013-09-30
但仅在之间运行作业09:00 AM - 12:00 PM
I am facing trouble in writing a Cron expression for it, furthermore my user is non-technical so my user wants me to create Cron expression automatically from both time stamp values.
我在为它编写 Cron 表达式时遇到了麻烦,而且我的用户不是技术人员,所以我的用户希望我从两个时间戳值自动创建 Cron 表达式。
Please help me out. Let me know if there is another way.
请帮帮我。让我知道是否有其他方法。
I have seen many resources on Google but I still can't find nothing.
我在谷歌上看到了很多资源,但我仍然找不到任何东西。
Links:
链接:
http://quartz-scheduler.org/documentation/quartz-1.x/tutorials/crontrigger
http://quartz-scheduler.org/documentation/quartz-1.x/tutorials/crontrigger
http://quartz-scheduler.org/documentation/quartz-2.x/tutorials/tutorial-lesson-05
http://quartz-scheduler.org/documentation/quartz-2.x/tutorials/tutorial-lesson-05
Does cron expression in unix/linux allow specifying exact start and end dates
unix/linux 中的 cron 表达式是否允许指定确切的开始和结束日期
Update
更新
I have written one but it's not working
我已经写了一个但它不起作用
|------------------------------------------------------------------|
| Seconds | Minutes | Hours | DayOfMonth | Month | DayOfWeek | Year|
| | | | | | | |
| 0 | 0 | 9-12 | 27-30 | 9 | ? | 2013|
|------------------------------------------------------------------|
trying to map 2013-09-27
to 2013-09-30
but only between 09:00 AM - 12:00 PM
试图映射2013-09-27
到2013-09-30
但仅在09:00 AM - 12:00 PM
UpdatedI have also tried it running with
更新我也试过它运行
Trigger trigger = TriggerBuilder.newTrigger().withIdentity(NAME_TRIGGER_TASK_UPDATER, GROUP_TASK_TRIGGER)
.withSchedule(CronScheduleBuilder.cronSchedule("0 0 9-12 19-22 10 ? *")).build();
but it doesn't give any error nor go into my execute method of my job
但它没有给出任何错误,也没有进入我工作的执行方法
cronSchedule("0 0 9-12 ? * ?") throws invalid schedule exception.
The code below runs it without respecting the start and end date.
下面的代码在不考虑开始和结束日期的情况下运行它。
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();
采纳答案by AmeetC
What is the error you get when you say it is not working?
当您说它不起作用时,您得到的错误是什么?
You can try the following code (Edit:applies to Quartz 2.2). This approach does not specify the start/end dates and year in the cron expression, instead uses the Trigger methods to specify them. (Note: I haven't tested it myself, let me know if it works for you)
您可以尝试以下代码(编辑:适用于 Quartz 2.2)。这种方法没有在 cron 表达式中指定开始/结束日期和年份,而是使用 Trigger 方法来指定它们。(注意:我自己没有测试过,如果它对你有用,请告诉我)
Edit:I had the chance to test this code, I ran the code below and kept changing the system clock and all triggers were successful between 9 am to 12 am from start to end date.
编辑:我有机会测试此代码,我运行下面的代码并不断更改系统时钟,并且所有触发器在从开始到结束日期的上午 9 点到凌晨 12 点之间都成功。
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");
}
}
}
If the above code does not work, try to replace the cronSchedule("0 0 9-12 * * ?")
with cronSchedule("0 0 9-12 ? * ?")
如果上面的代码不能正常工作,尝试更换cronSchedule("0 0 9-12 * * ?")
与cronSchedule("0 0 9-12 ? * ?")