Search code examples
cronquartz-schedulercrontrigger

Building a Quartz Cron expression that includes current date


Is it possible to have a (Quartz)scheduler with cron expression that triggers after every 5 days including the today's date. For e.g.. if I am schedule a job on Dec 14, 2016 with an interval of 5 days, then it should be triggered on Dec 14, Dec 19, Dec 24, Dec 29, Jan 3, Jan 8, Jan 13, Jan 18, Jan 23, Jan 28, Feb 2 and so on.... ? I tried "17 33 15 14/5 * ? *" but not giving accurate result. Kindly HELP !!!


Solution

  • CronTrigger is unsuitable for this kind of schedules because these cannot be expressed using a cron expression. What you want to do is use the CalendarIntervalTrigger instead that is specifically tailored for these purposes.

    I am attaching a screenshot of a CalendarIntervalTrigger editor in our Quartz scheduler management and monitoring tool (QuartzDesk).

    CalendarIntervalTrigger editor in the QuartzDesk GUI

    As you can see, all you need to do is:

    1. Set the trigger's start datetime to Dec 14, 2016.
    2. Set the repeat interval unit to 'Day'.
    3. Set the repeat interval to 5 (i.e. 5 days).

    Next to the editor window, there is a view that shows you calculated next fire time for the trigger. Dec 14th and Dec 19th are missing in the list, because today is Dec 20 and the view shows you future trigger fire times only.

    The actual Java code to create a CalendarIntervalTrigger programatically is similar to the code that you use to create a CronTrigger so I am not including it here. The main point is that you have to use a different trigger type and set its the two properties as mentioned above.