1、简介:Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它能够与J2EE与J2SE应用程序相结合也能够单独使用。Quartz能够用来建立简单或为运行十个,百个,甚至是好几万个Jobs这样复杂的程序。Jobs能够作成标准的Java组件或 EJBs。Quartz的最新版本为Quartz 2.3.0。java
2、由于定时调度,在不少业务上面都会涉及,想要根据本身的规则来生成本身想要的达到的目的。因此利用quartz来时间定时任务的触发。是很是有必要的。spring
3、springmvc下须要的jar包spring-mvc
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.3.9.RELEASE</version> </dependency> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.3</version> </dependency>
说明:quartz是基础包。spring-context-support包是调度设置的依赖包。spring-context能够不用添加。mvc
4、spring-quartz.xml的配置测试
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd"> <!-- 加入须要执行的类 --> <bean id="timingSchedule" class="com.troy.jpa.schedule.TimingSchedule"/> <!-- 加入定时执行的方法 --> <bean id="timingScheduleJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <!-- 定时执行的类 --> <property name="targetObject" ref="timingSchedule"/> <!-- 具体的方法 --> <property name="targetMethod" value="execute"/> </bean> <!-- 调度触发器,设置本身想要的时间规则 --> <bean id="timingScheduleTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <!-- 加入相关的执行类和方法 --> <property name="jobDetail" ref="timingScheduleJobDetail"/> <!-- 设置时间规则 (为了方便测试,设置成一分钟一次。具体的规则见详情)--> <property name="cronExpression" value="00 * * * * ?"/> </bean> <!-- 加入调度工厂 ,设置调度触发器便可--> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="timingScheduleTrigger"/> </list> </property> </bean> </beans>
5、须要执行的类和方法spa
package com.troy.jpa.schedule; import java.util.Date; public class TimingSchedule { //定时执行的方法 public void execute(){ System.out.println("执行时间"+ new Date()); } }
6、执行效果code
执行时间Wed Jul 12 21:01:00 CST 2017 执行时间Wed Jul 12 21:02:00 CST 2017 执行时间Wed Jul 12 21:03:00 CST 2017 执行时间Wed Jul 12 21:04:00 CST 2017
7、时间设置规则xml
1 秒 是 0-59 , - * / 2 分 是 0-59 , - * / 3 小时 是 0-23 , - * / 4 日 是 1-31 , - * ? / L W 5 月 是 1-12 or JAN-DEC , - * / 6 周 是 1-7 or SUN-SAT , - * ? / L # 7 年 否 empty 或 1970-2099 , - * /