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 From 2013-09-27
with in 09:00 AM - 12:00 PM
to 2013-09-30
Explanation:
Run a job from 2013-09-27
to 2013-09-30
but only between 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.
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-2.x/tutorials/tutorial-lesson-05
Does cron expression in unix/linux allow specifying exact start and end dates
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
Updated I 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();
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)
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.
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 ? * ?")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With