spring 任务调度quartz
简单记录一下spring任务调度quartz的例子
首先添加包
quartz-2.2.3.jar
然后写个简单的TestJob类
package com.job; import java.util.Date; public class TestJob { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public void excute(){ System.out.println(name+"===定时任务:"+new Date()); } }
再加配置文件application-job.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd" > <bean id="testJob" class="com.job.TestJob"> <property name="name" value="testtest"></property> <!-- 给testJob里的name属性赋值 --> </bean> <bean id="testJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="testJob"></property> <!-- 调用testJob --> <property name="targetMethod" value="execute" /> <!-- testJob里的execute()方法 --> <property name="concurrent" value="false" /><!-- 作业不并发调度 --> </bean> <bean id="testJobCron" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="testJobDetail" /> <!-- 调用 id="testJobDetail"的 bean --> <property name="cronExpression" value="0/5 * * * * ?" /> <!-- 每5秒执行一次 --> </bean> <!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 --> <bean id="schedulerFactoryBean" lazy-init="false" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="testJobCron" /> <!-- 调用 id="testJobCron"的 bean --> </list> </property> </bean> </beans>
最后应该加web.xml里载一下配置文件
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:application-*.xml</param-value> </context-param>
估计大概应该也许或者就差不多可以了吧