1

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):

  • Run a job every day between 8:30 and 12:45, with a period of five minutes. E.g. 8:30, 8:35, 8:40, .... 12:40, 12:45.

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?

sydnal
  • 317
  • 2
  • 15
  • I'm guessing you need to create two separate schedule triggers (8-11 0/5 and 12 0-45/5) and [set them both to the job](https://stackoverflow.com/questions/6258199/add-multiple-triggers-to-single-quartz-job) – daniu Dec 04 '18 at 14:58
  • You want it to go from 8:30 to 9:00 and then jump to 12:00 to 12:45? I'm clarifying as your statements seem contradictory. – Andrew T Finnell Dec 04 '18 at 15:02
  • No I want it to run every 5 mins between 8:30 and 12:45, every day, without any gap. Which part was contradictory? – sydnal Dec 04 '18 at 15:10
  • 1
    I believe you are correct in that it doesn't seem possible in a single job. You can likely create your own custom Trigger though. You don't have to split the times it runs into two Triggers, but rather you can create a single trigger to run from 8:30 to 12:45, and then another to re-run it the next day. You can set the Start and End times for the Triggers. So have a trigger run daily to start and end the actual trigger from 8:45 to 12:45. – Andrew T Finnell Dec 04 '18 at 18:14

2 Answers2

1

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);
    }
}
Andrew T Finnell
  • 13,417
  • 3
  • 33
  • 49
  • Thanks for the answer. In the end I might have to use such a solution but I will keep looking at the alternative libs for a more straightforward way. Ideally I want to be able to schedule this using a single job/task definition, concise and less error-prone. I am not sure what I'm looking for exists at this point. Keeping the question open for now in case there are other ideas/alternative opinions. – sydnal Dec 04 '18 at 19:37
1

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.

DailyTimeIntervalTrigger Example

Jan Moravec
  • 1,808
  • 1
  • 15
  • 18
  • Yep that seems to be what I am looking for. Tbh. I did come upon the same conclusion about fixating on cron triggers and checked its API, but somehow was not able to find this, thanks. – sydnal Dec 05 '18 at 16:38