Quartz task scheduler

Quartz task scheduler

When using the jdk timer, it was found that the development needs could not be met; that is, the task could not be performed on the specified date.

This introduces an excellent open source task scheduling framework "quartz".

The version added here is quartz-1.8.6. Quart's official website: http://www.quartz-scheduler.org/ ; the spring version of the framework in the project is spring 3.0 which cannot integrate quartz 2.x and subsequent versions; therefore, the quartz 1.8.6 version is used here.

Regarding the use of quartz in spring, you can view it in the spring documentation. The spring 4.0.4 documentation says that only quartz 2.+ is supported after spring 3.2.

Integrate Quartz in spring for task scheduling; first import the jar packages that need to be used into the lib directory of the project: 

"Org.springframework.context.support-3.0.2.RELEASE.jar": This package is spring repackaged into bean-style classes based on the main classes in quartz

"Quartz-1.8.6.jar": the core package of quartz

To use quartz to schedule tasks in spring; three beans need to be configured 1. Develop task information beans

  •    ① Set execution target
  •    ② Set the corresponding execution method in the execution object
  •    ③ Can it be executed synchronously?
  • 2. Develop the task execution timing (execution trigger) bean
  •    2.1, Simple Trigger (SimpleTrigger)
  •    ① Set task details
  •    ② Set the task delay execution time
  •    ③ Set task execution frequency
  •    2.2. Task Trigger (CronTrigger)
  •    ① Set task details
  •    ② Set the execution timing (cronExpression)
  •     cronExpression: second, minute, hour, day, month, anniversary (optional)
  • 3. Set up the task scheduling factory bean
  •    Set up triggers

package com.intsmaze.quartz;

public class QuartzTask {

    public void doSimpleTriggerTask() {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        System.out.println("doing simpleTrigger task..." + sdf.format(new Date()));


    public void doCronTriggerTask() {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        System.out.println("doing cronTrigger task..." + sdf.format(new Date()));



1. jobDetail task details; including which class is called; which method in the class; whether the task can be executed in parallel during execution.

<!-- Register an ordinary bean -->

   <bean id="quartzTask" class="com.intsmaze.quartz.QuartzTask"></bean>

   <!-- 1. Develop task information information -->

   <bean id="jobDetail1" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">

              <!-- Set the execution object -->

              <property name="targetObject" ref="quartzTask"></property>

              <!-- Set the corresponding execution method in the execution object -->

              <property name="targetMethod" value="doSimpleTriggerTask"></property>

<!-- Can it be executed synchronously; it cannot be executed synchronously (meaning: for example, execute a task in three seconds, a task has not been executed in three seconds, if the setting can be synchronized, then the previous task has not been executed, it will start a The new thread executes the next task and is set to be asynchronous, then the next task must be executed until the previous execution is completed.) -->

              <property name="concurrent" value="false"></property>


   <bean id="jobDetail2" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">

              <!-- Set the execution object -->

              <property name="targetObject" ref="quartzTask"></property>

              <!-- Set the corresponding execution method in the execution object -->

              <property name="targetMethod" value="doCronTriggerTask"></property>

              <!-- Whether it can be executed synchronously; can not be executed synchronously -->

              <property name="concurrent" value="false"></property>


2. Trigger task scheduling trigger; mainly used to define when jobDetail is executed.

There are two most commonly used triggers: Simple Trigger SimpleTrigger and Task Trigger CronTrigger. SimpleTrigger is similar to jdk timer. It can only specify the frequency at which the task is executed, but it cannot specify the precise execution time. CronTrigger can execute the time set by a simple trigger with frequency, or it can develop a complex time plan to execute it. If it can be formulated: perform related tasks at 15 o'clock on the last day of each month.

  <!-- 2. Establish the timing of task execution (task execution trigger) -->

   <bean id="simplerTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">

           <!-- Set task details -->

           <property name="jobDetail" ref="jobDetail1"></property>

           <!-- Set the task delay execution time; delay 2 seconds execution -->

           <property name="startDelay" value="2000"></property>

           <!-- Set the task execution frequency; the execution frequency is executed every 4 seconds -->

           <property name="repeatInterval" value="2000"></property>


   <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">

           <!-- Set task details -->

           <property name="jobDetail" ref="jobDetail2"></property>

           <!-- Set the timing of task execution, cron expression -->

           <property name="cronExpression" value="* * * 18c * ?"></property>


3. SchedulerFactory task scheduling factory; used to schedule various task triggers.

   <!-- 3. Set up the dispatch factory -->

   <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">

           <property name="triggers">


                   <!-- <ref bean="simplerTrigger"/> -->

                   <ref bean="cronTrigger"/>




Start the spring container, and the task scheduling will be executed automatically.

The content received in the cronExpression expression attribute in CronTrigger:

In the expression, the time format is separated by a space, and each time is expressed as: second minute hour day month week year (optional)

Day and week cannot appear at the same time, because they will not be consistent and cause errors.

Values ​​that can appear and conform to:

The'*' character can be used in all fields. Set "*" in the "minute" field to mean "every minute".

The'?' character can be used in the "day" and "day of the week" fields. It is used to specify an "ambiguous value". This will be used when you need to specify one of these two fields instead of the other. Used. You can see its meaning in the following example.

The'-' character is used to specify a range of values, such as "10-12" in the "hour" field, which means "10 o'clock to 12 o'clock".

The',' character specifies several values. For example, set "MON,WED,FRI" in the "day of the week" field to mean "the days Monday, Wednesday, and Friday".

The'/' character is used to specify the increment of a value. For example, setting "0/15" in the "second" field means "0, 15, 30, and 45 seconds". And "5/15" means "5th, 20, 35, and 50". Adding the "*" character before the'/' is equivalent to specifying the start from 0 seconds. Each field has a series of values ​​that can start or end . For the "second" and "minute" fields, the value range is 0 to 59, for the "hour" field it is 0 to 23, for the "day" field it is 0 to 31, and for the "month" The field is 1 to 12. The "/" field just helps you to start the "nth" value within the allowable value range. Therefore, for the "month" field, "7/6" just means that July is turned on instead of "every six months". Please pay attention to the subtle difference.

The'L' character can be used in the two fields of "day" and "day of the week". It is an abbreviation of "last", but has different meanings in these two fields. For example, the "L" in the "day" field means "the last day of the month"-the 31st for January and the 28th for February (non-leap year). In the "day of the week" field, it simply means "7" or "SAT", but if used in the "day of the week" field followed by a certain number, it means "the last week of the month ×" — — For example, "6L" means "the last Friday of the month". When using the'L' option, it is very important to specify a certain list or range, otherwise you will be confused by the result.

'W' can be used in the "day" field. Used to specify the nearest working day (Monday to Friday) for a given date in the calendar. For example, if you set the "day" field to "15W", it means: "the closest working day to the 15th of the month". So if the 15th is a Saturday, the trigger will be called on the 14th, which is Friday. If the 15th is a Sunday, the trigger will fire on the 16th, which is Monday. If the 15th is Tuesday, it will be triggered on that day. However, if you set the "day" field to "1W", and the first is Saturday, the trigger will fire on the next Monday, which is the 3rd of the current month, because it will not cross the boundary of the current month's value range. The'W' character can only be used when the value of the "day" field is a single day instead of a series of values.

'L' and'W' can be combined and used in the "day" field to represent "LW", which means "the last working day of the month".

The'#' character can be used in the "day of the week" field. This character means "the week of the month ×", for example, "6#3" means the third Friday of the month (6 means Friday and "#3" the third Friday of the month). Another example: "2#1" = means the first Monday of the month and "4#5" = the fifth Wednesday of the month. Note that if you specify "#5" in the month without the fifth "week ×", the month will not be triggered.

The'C' character can be used in the "day" and "day of the week" fields. It is an abbreviation for "calendar". It is expressed as a value calculated based on the relevant calendar (if any). If there is no associated calendar, it is equivalent to including all calendars. "Day" field value "5C" means "the first day in the calendar or the 5th and later", "day of the week" field value "1C" means "the first day or Sunday and later in the calendar" ".

The legal characters for the "month" field and the "day of the week" field are not case sensitive.