Quartz是一个完全由Java编写的开源作业调度框架,当你想实现定时做些事情的时候,它就派上用场啦!目前Quartz比较稳定的版本是2.2.1,所以我这里就以这个版本为例,如果你使用Quartz2.x系列,那你的Spring版本必须3.1版本及以上(假如你需要将Quartz跟Spring整合的话),Quartz并不一定需要跟Spring整合哈,它完全可以脱离Spring单独工作,只是Spring目前太流行,所以大家都喜欢将其他框架往Spring里整合。
下面是Quartz的一个入门级别的简单示例:
applicationContext-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:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd" default-autowire="byName" default-lazy-init="true"> <bean id="taskJob" class="com.yida.framework.test.FirstJob" /> <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="group" value="job_work" /> <property name="name" value="job_work_name" /> <!--false表示等上一个任务执行完后再开启新的任务 --> <property name="concurrent" value="false" /> <property name="targetObject"> <ref bean="taskJob" /> </property> <property name="targetMethod"> <value>run</value> </property> </bean> <!-- 调度触发器 --> <bean id="myTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="name" value="work_default_name" /> <property name="group" value="work_default" /> <property name="jobDetail"> <ref bean="jobDetail" /> </property> <property name="cronExpression"> <value>0/3 * * * * ?</value> </property> </bean> <!-- 调度工厂 --> <bean id="scheduler" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="myTrigger" /> </list> </property> </bean> </beans>
web.xml里配置加载spring的相关配置文件:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" 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_2_5.xsd"> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:/com/yida/framework/base/config/spring/application*.xml </param-value> </context-param>
编写一个简单的任务测试类:
package com.yida.framework.test;
public class FirstJob {
public void run(String[] args) {
System.out.println("我的第一个测试任务。");
}
}
最后部署你的项目,启动tomcat,任务就会自动执行啦!