Configuring Quartz
Configuring Quartz
Quartz File Structure
The Quartz configuration file contains three types of bean definitions:
- The schedulerFactory bean, which contains the list of trigger beans that it needs to execute.
- The trigger beans (xxxTrigger), which configure the triggers for each job. This is usually either a SimpleTriggerBean, which runs a recurring job at an interval (specified in milliseconds), or a CronTriggerBean, which runs a job once at a specified time.
- The job beans (xxxJob), which specify the class and method that will be called for each job as well as any arguments that need to be passed.
There is generally only one factory bean definition per quartz.xml file. Each job has a job bean definition and a trigger bean definition.
For more information about Quartz see the Quartz website.
Quartz Cron Configuration
The time/trigger to execute the scheduled job can be set with the cronExpression property. The cron expression contains six required components and one optional component. A cron expression is written on a single line and each component is separated from the next by space. Only the last, or rightmost, component is optional. The table below describes the cron components in detail.
Components of a Cron Expression:
Position | Meaning | Allowed Special Characters |
---|---|---|
1 | Seconds (0-59) | , - * / |
2 | Minutes (0-59) | , - * / |
3 | Hours (0-23) | , - * / |
4 | Day of month (1-31) | , - * / ? L C |
5 | Month | (either JAN-DEC or 1-12) , - * / |
6 | Day of week (either SUN-SAT or 1-7) | , - * / ? L C # |
7 | Year (optional, 1970-2099), when empty, full range is assumed | , - * / |
Each component accepts the typical range of values that you would expect, such as 0-59 for seconds and minutes and 1-31 for day of the month. For the month and day of the week components, you can use numbers, such as 1-7 for day of the week, or text such as SUN-SAT.
Each field also accepts a given set of special symbols, so placing a * in the hours component means every hour, and using an expression such as 6L in the day-of-the-week component means last Friday of the month. The table below describes cron wildcards and special symbols in detail.
Cron Expression Wildcards and Special Symbols:
Special Character | Description |
---|---|
* | Any value. This special character can be used in any field to indicate that the value should not be checked. Therefore, our example cron expression will be fired on any day of the month, any month, and any day of the week between 1970 and 2099. |
? | No specific value. This special character is usually used with other specific values to indicate that a value must be present but will not be checked. |
- | Range. For example 10-12 in the Hours field means hours 10, 11, and 12. |
, | List separator. Allows you to specify a list of values, such as MON, TUE, WED in the Day of week field. |
/ | Increments. This character specifies increments of a value. For example 0/1 in the Minute field in our example means every 1-minute increment of the minute field, starting from 0. |
L | L is an abbreviation for Last. The meaning is a bit different in Day of month and Day of week. When used in the Day of month field, it means the last day of the month (31st of March, 28th or 29th of February, and so on). When used in Day of week, it has the same value as 7---Saturday. The L special character is most useful when you use it with a specific Day of week value. For example, 6L in the Day of week field means the last Friday of each month. |
This value is allowed only for the Day of week field and it specifies the nth day in a month. For example 1#2 means the first Monday of each month. | |
C[PD:*] | The Calendar value is allowed for the Day of month and Day of week fields. The values of days are calculated against a specified calendar. Specifying 20C in the Day of month field fires the trigger on the first day included in the calendar on or after the 20th. Specifying 6C in the Day of week field is interpreted as the first day included in the calendar on or after Friday. |
[PD:*]C At the time of writing, support for the C special character and specifying both Day of week and Day of month values has not been not completed.
* info extracted from Pro Spring