How to configure Quartz in Spring

Quartz is a powerful enterprise-level task scheduling framework. Spring inherits and simplifies Quartz. Let's see how to configure Quartz

Spring configuration files in Spring:


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
 
  <!-- Timer factory -->
  <bean id="timerFactorBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
      <property name="triggers">
          <list>
             <!-- Multiple timing tasks can be configured-->
             <ref bean="JobTrigger"/>
          </list>
      </property>
  </bean>
  <! -- Define the execution time-->
  <bean id="JobTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
     <property name="jobDetail" ref="orderJobDetail"/>
     <!-- run every 2 minutes at 9:10am -->
<property name="cronExpression" value="0 10/2 9 * * ?" />
  </bean>

  <!-- Define the method for running scheduled tasks-->
  <bean id="orderJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
      <property name="targetObject" ref=" orderJob" />
      <property name="concurrent" value="false"/>
      <property name="targetMethod" value="run" />
  </bean>
 
  <!-- Execute timer classes and methods-->
  <bean id="orderJob" class="timer. OrderAutoJob"></bean>
 
</beans> Class package timer for

timing tasks to run ; public class OrderAutoJob { public void run() {






System.out.println("OrderAutoJob timing start....");

System.out.println("OrderAutoJob timing end....");
}
}


测试程序:
package Test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestAutoOrder {
public static void main(String[] args) {
System.out.println("Test start.");
                ApplicationContext context = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
                System.out.print("Test end..");      hours seconds minutes Cron expressions include the following 7 fields: About cron expressions (from the web): }
}










day of
month month
week
year (optional field)


Cron triggers take advantage of a series of special characters as follows: The

backslash (/) character represents an incremental value. For example, "5/15" in the seconds field means every 15 seconds starting at the 5th second.

The question mark (?) character and the letter L character are only available in day-of-month and day-of-week fields. A question mark indicates that the field does not contain a specific value. So, if you specify a day of the month, you can insert a "?" in the day of the week field, indicating that the day of the week value doesn't matter. The letter L character is short for last. Placed in the date of the month field, it means that it is scheduled to be executed on the last day of the month. In the day of the week field, if "L" exists alone, it is equal to "7", otherwise it represents the last instance of the day of the week in the current month. So "0L" means that it is scheduled to execute on the last Sunday of the month.

The letter (W) character in the day-of-month field schedules execution on the weekday closest to the specified value. Putting "1W" in the date of month field means to schedule execution on the first working day of the month.

The pound (#) character specifies a specific weekday instance for a given month. Put "MON#2" in the day of the week field to schedule the task on the second Monday of the month.

The asterisk (*) character is a wildcard character, meaning that the field can accept any possible value.
Field Allowed Values ​​Allowed special characters
seconds 0-59 , -*/
minute 0-59 , -*/
hour 0-23 , -*/
date 1-31 , -* ?/LWC
month 1-12 or JAN-DEC , - * /
Week 1-7 or SUN-SAT , - * ? / LC #
year (optional) leave blank, 1970-2099 , - * /
expression meaning
"0 0 12 * * ?" fires "0 15 10 ? * *" every day at 12 noon
"0 15 10 ? * *" every day at 10:15 am
"0 15 10 * * ?" triggers
"0 15 10 * * ? *" every day at 10:15 am
"0 15 10 * * ? 2005" every day at 10:15 am triggers
"0 * 14 * * ?" every day at 10:15 am Fires "0 0/5 14 * * ?" every 1 minute between 2pm and 2:59pm every day and fires "0 0/5 14,18
every 5 minutes between 2pm and 2:55pm every day " * * ?" triggers "0 0-5 14 * * ?"
every 5 minutes between 2:00 pm and 2:55 pm and 6:00 pm - 6:55 pm every day
Triggers "0 10,44 14 ? 3 WED" every 1 minute
Every March at 2:10 pm and 2:44 on Wednesdays
"0 15 10 ? * MON-FRI" Triggers at 10:15 am Monday to Friday
" 0 15 10 15 * ?" triggers "0 15 10 L * ?" at 10:15 am on the 15th of every month and triggers "0 15 10 ? * 6L"
at 10:15 am on the last day of every month Fires on the last Friday of every month at 10:15 am "0 15 10 ? * 6L 2002-2005" Fires at 10:15 am on the last Friday of every month from 2002 to 2005


" 0 15 10 ? * 6#3" Triggered
every at 6:00 am every two hours, 08:00 23-7/2, 8 * * *








Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327089001&siteId=291194637