Quartz has provide 2 type of trigger
- SimpleTrigger (use SimpleScheduleBuilder)
- CronTrigger (use CronScheduleBuilder)
Triggers
There are different types of triggers that you can select based on your scheduling needs. The two most common types are simple triggers and cron triggers.
SimpleTrigger
SimpleTrigger should meet your scheduling needs if you need to have a job execute exactly once at a specific moment in time, or at a specific moment in time followed by repeats at a specific interval. For example, if you want the trigger to fire at exactly 11:23:54 AM on January 13, 2015, or if you want it to fire at that time, and then fire five more times, every ten seconds.
With this description, you may not find it surprising to find that the properties of a SimpleTrigger include:
a start-time, and end-time, a repeat count, and a repeat interval.
The repeat count can be zero, a positive integer, or the constant value SimpleTrigger.REPEAT_INDEFINITELY. The repeat interval property must be zero, or a positive long value, and represents a number of milliseconds. Note that a repeat interval of zero will cause ‘repeat count’ firings of the trigger to happen concurrently (or as close to concurrently as the scheduler can manage).
CronTrigger
CronTrigger is often more useful than SimpleTrigger, if you need a job-firing schedule that recurs based on calendar-like notions, rather than on the exactly specified intervals of SimpleTrigger.
With CronTrigger, you can specify firing-schedules such as every Friday at noon, or every weekday and 9:30 am, or even every 5 minutes between 9:00 am and 10:00 am on every Monday, Wednesday and Friday during January.
Cron Expressions
Cron-Expressions are used to configure instances of CronTrigger. Cron-Expressions are strings that are actually made up of seven sub-expressions, that describe individual details of the schedule. These sub-expression are separated with white-space, and represent:
- Seconds
- Minutes
- Hours
- Day-of-Month
- Month
- Day-of-Week
- Year (optional field)
An example of a complete cron-expression is the string “0 0 12 ? * WED” – which means “every Wednesday at 12:00:00 pm”.
CronTrigger Example 1 – an expression to create a trigger that simply fires every 5 minutes “0 0/5 * * * ?”
CronTrigger Example 2 – an expression to create a trigger that fires every 5 minutes, at 10 seconds after the minute (i.e. 10:00:10 am, 10:05:10 am, etc.).
“10 0/5 * * * ”
CronTrigger Example 3 – an expression to create a trigger that fires at 10:30, 11:30, 12:30, and 13:30, on every Wednesday and Friday.
“0 30 10-13 ? * WED,FRI”
CronTrigger Example 4 – an expression to create a trigger that fires every half hour between the hours of 8 am and 10 am on the 5th and 20th of every month. Note that the trigger will NOT fire at 10:00 am, just at 8:00, 8:30, 9:00 and 9:30
“0 0/30 8-9 5,20 * ?”
Note that some scheduling requirements are too complicated to express with a single trigger – such as “every 5 minutes between 9:00 am and 10:00 am, and every 20 minutes between 1:00 pm and 10:00 pm”.
Difference Between Cron Trigger and Simple Trigger in Quartz Scheduler?
The differences between the two are merely how you wish to schedule the execution of your jobs. There are no other differences in terms of best practices or thread safety.
SimpleTrigger is useful for jobs that you want to execute exactly once at a specific time, optionally followed by repeated execution at a specific interval.
CronTrigger is much different in that it is for jobs that are inherently recurring on some calendar based schedule. So with CronTrigger you can schedule a job that runs every Sunday at 1AM.