Before writing a timer to implement task scheduling, this article is used to write the use of quartz in spring to implement task scheduling, directly on the code:
To define a Task object:
package com;/** * 1. 定义任务对象 * * @author Administrator * */publicclass DataBackup { //提供任务方法 - 任务实现的内容 publicvoidbackup(){ System.out.println("备份数据库"); }}
Spring's configuration file
<!--This profile is the basic configuration file for spring, SPRINGMVC,AOP, transaction, and so on on this basis -<Beans xmlns= "http://www.springframework.org/Schema/Beans"Xmlns:context= "http://www.springframework.org/Schema/Context"Xmlns:xsi= "http://www.w3.org/2001/xmlschema-instance"xsi:schemalocation= "http://www.springframework.org/Schema/Beans http://www.springframework.org/Schema/Beans/spring-beans-3.2.xsd http://www.springframework.org/Schema/Context http://www.springframework.org/Schema/Context/spring-context-3.2.xsd"> <context:component-scan base-package ="com"/> <!--2. Handing the task object to IOC container management-- <bean id="Bu" class="com." DataBackup "> </Bean> <!--3. Set Task Scheduler details which task object is available, and which method is in this object? <bean id= "jobdetail" class=" Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean "> < property name="TargetObject" ref="bu"></Property > < property name="Targetmethod" value="Backup"> </Property > </Bean> <!--4. Set this task to schedule Jobdetail execution time, period > for which task assignment execution time and period setjobdetail (Jobdetail detail ) > Allocation Execution period setcronexpression (string expression) Through an expression-cron expression [seconds 0-59 * Minute 0 -59 * 0-23 * Month within 1-31 * Month 1-12 * Week Date 0-6 * Year optional 1970-2099 ? Can only appear in the month and week date fields, indicating that the field does not matter when the date within the month or within the week date is specified Fixed/increment value written in/after # number 0 10 9? 1 2#2January the second Tuesday W working day 1W January first business day L last instance 1L January last day ]-- <bean id= "trigger" class=" Org.springframework.scheduling.quartz.CronTriggerFactoryBean "> < property name="Jobdetail" ref="Jobdetail"></Property > < property name="Cronexpression" value="0 * * * *?" ></Property > </Bean> <!--5 Configure the general scheduling class to load the process of a previously configured task dispatch into a container-- <Bean class="Org.springframework.scheduling.quartz.SchedulerFactoryBean"> < property name="triggers"> <list> <ref Bean="trigger"/> </list> </Property > </Bean></Beans>
Xml
<?xml version= "1.0" encoding= "UTF-8"?><web-app version="3.0"xmlns="Http://java.sun.com/xml/ns/javaee" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation=" Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd "> <listener> <listener-class>Org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--Configure the spring Distributor, and spring indicates that the corresponding servlet "name can be changed" to spring-servlet.xml the configuration file <servlet > <servlet-name >Spring</servlet-name > <servlet-class >Org.springframework.web.servlet.DispatcherServlet</servlet-class > </servlet > <servlet-mapping > <!--will intercept. Do request-- <servlet-name >Spring</servlet-name > <url-pattern >*.do</url-pattern > </servlet-mapping > <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></Web-app>
Expressions commonly used in quartz:
"0 0 12 * *?" trigger 12 o'clock noon every day.
"0 15 10?" * * "trigger 10:15 every day"
"0 15 10 * *?" Daily 10:15 Trigger
"0 15 10 * *?" * "10:15 per day" trigger
"0 15 10 * *?" 2005 "2005-year daily 10:15 Trigger
"0 * 14 * *?" triggers every 1 minutes from 2 o'clock in the afternoon to 2:59 daily
"0 0/5 14 * *?" triggers every 5 minutes from 2 o'clock in the afternoon to 2:55 daily
"0 0/5 14,18 * *?" triggers every 5 minutes from 2 o'clock in the afternoon to 2:55 daily and from 6 o'clock in the afternoon to 6:55
0 0/5 8-10,12-16,20-22 * *?
Every morning 6 o ' 0 0 6 * * *
0 * /2 * * per two hours
23 to the next morning at 7, every 2 hours, and 8 o ' night.
0 0 23-7/2,8 * *?
January-March, daily 4:11 execution
0 11 4 * 1-3?
0 4 1 1 * ?
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Task scheduling under Task scheduling--spring Quartz