Search code examples
javajakarta-eequartz-scheduler

How to run a Quartz Scheduler on a previous or next business day


In this article it is explained how to exclude Quartz from running on specific calendar days, but what I need is something different, I need to run the job in the previous or next business day if the trigger calendar results on a holiday.

For example, assume the job is scheduled to run monthly, including Friday, 29 March 2019 that is a holiday.

This means that:

  • Not Holiday: Thursday, 28 March 2019 (when the job should run if it runs before holiday)
  • Holiday: Friday, 29 March 2019
  • Holiday: Saturday, 30 March 2019
  • Holiday: Sunday, 31 March 2019
  • Not Holiday: Monday, 1 April 2019 (when the job should run if it runs after holiday)

One way to achieve this is to run the job every day and analyze whether it should run on that day or not, and retrigger if necessary, but is there a better way?

UPDATE

If I have to trigger the job every day, why not use instead the Java EE 8 TimerService?


Solution

  • I would suggest to run it on every working day using Cron Expression for example (Running every weekday at 5 PM)

    0 00 17 * * MON-FRI
    

    and Inside a function which gets invoked by Cron expression, you can call holidayService.isHoliday(date) which will tell if the date is a holiday. If it is Holiday then you can skip running the report.