Reputation: 317
I need to schedule some periodic jobs and I have hit a roadblock with Quartz.
For example:
I do not think this is possible with Quartz (with a single expression/job):
If it was between 8:00 and 12:00 it would be easy but I could not find a way to schedule it except handling 8:30-9:00 and 12:00-12:45 with separate expressions, which I do not want to.
Am I wrong in assuming that this is non-trivial with Quartz? I have also searched for some alternatives but most seem to have a similar cron syntax and I am not sure they could handle it either.
Is there a finer-grained scheduling library that I can use in this scenario?
Upvotes: 1
Views: 1311
Reputation: 1880
This is perfectly possible with Quartz and a single trigger. People often focus on Cron triggers, but Quartz supports other trigger types and these are often more suitable. To implement your scheduling scenario, I recommend that you look into the Quartz DailyTimeIntervalTrigger.
In the screenshot below you can see a DailyTimeIntervalTrigger example with attribute values to cover your use-case.
Upvotes: 1
Reputation: 13638
I'm not certain you can do this, as you've hinted at. It seems possible to create a custom Trigger to do it, but then it becomes quite a bit of work. The other option is to split the dual Triggers by day, not time.
public class TestQuartz {
class ActualJob implements Job {
@Override
public void execute(JobExecutionContext context)
throws JobExecutionException {
}
}
class DailyJob implements Job {
@Override
public void execute(JobExecutionContext context)
throws JobExecutionException {
// Run from now (8:45) every 5 minutes until 12:45 (endDate)
Trigger trigger =
newTrigger()
.startNow()
.endAt(endDate) // 12:45 PM TODAY
.withSchedule(
cronSchedule("0 0/5 * 1/1 * ? *"))
.build();
try {
Scheduler sched = context.getScheduler();
sched.scheduleJob(newJob(ActualJob.class).build(), trigger);
} catch (SchedulerException ex) {
throw new JobExecutionException(ex);
}
}
}
@Test
public void testQuartz() throws SchedulerException {
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sched = sf.getScheduler();
// Run once a day starting at 8:45 AM
Trigger dailyTrigger =
newTrigger().withSchedule(cronSchedule("0 45 8 1/1 * ? *")).build();
JobDetail job = newJob(DailyJob.class).build();
sched.scheduleJob(job, dailyTrigger);
}
}
Upvotes: 1