Phellipe Silva
Phellipe Silva

Reputation: 41

Cron Expression, run every two days ignoring weekends

Is it possible to create a Cron Expression that runs every two days and ignore weekends? What I would like to achieve is basically this expression 0 0 9 1/2 * ? but ignoring the weekends. I validate my expressions in the following website http://www.cronmaker.com/

It must have a 48h interval.

Thanks!

Upvotes: 1

Views: 3092

Answers (2)

Jan Moravec
Jan Moravec

Reputation: 1880

I think you can safely use a single CronTrigger and all you need to do is to associate the trigger with a Quartz Calendar that excludes weekends. Quartz distribution comes with multiple Calendar implementations. You may want to check the WeeklyCalendar that allows you to exclude particular week days.

Providing the cron expression is: 0 0 9 1/2 * ?, your job will fire on Mon, Wed, Fri, (Sun will be ignored), Tue, Thu, (Sat will be ignored), Mon, ...

I think this should work. You can also try the CalendarIntervalTrigger with repeat interval unit = day, repeat interval = 2 and the same Calendar that excludes weekends.

Upvotes: 1

Kraiss
Kraiss

Reputation: 999

I have an other answer for you. Maybe simpler.

Basically in Quartz you have three step to run a job :

  • First the trigger fires because the rule of its scheduling is telling to do so
  • Then Quartz check that no scheduler's trigger or job rules is violated else the executin is vetoed (and it is here we will make the magic happen ;)
  • Finally Quartz run the job

If you want to use your expression (e.g.: 0 0 9 1/2 * ?) but ignore the week-end :

  • Instantiate your Quartz scheduler
  • Add to it org.quartz.TriggerListener implementing the rule that you don't want the trigger to fire the week-end.
  • Create and schedule your CRON trigger

A org.quartz.TriggerListener is an interface you can implements to add some rules other your triggers and jobs (see http://www.quartz-scheduler.org/documentation/quartz-1.x/tutorials/TutorialLesson07) You must use the TriggerListener#vetoJobExecution method to implement your specif rules

Upvotes: 0

Related Questions