1. 引入quartz的jar包
2. 建立需要运行的调度的类和方法
?
class="java" name="code">package com.baidu.service; import java.util.Date; public class QuartzService{ public void work(){ System.out.println("当前时间:"+new Date().toString()); } }
?
3.创建调度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:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd" default-lazy-init="true"> <!-- 要调用的工作类 --> <bean id="quartzService" class="com.baidu.service.QuartzService"></bean> <!-- 启动触发器的配置开始 --> <bean name="schedulerFactory" lazy-init="false" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref local="taskJobTrigger" /> </list> </property> </bean> <!-- 启动触发器的配置结束 --> <!-- job的配置开始 --> <bean id="myJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="quartzService"></property> <property name="targetMethod" value="work"></property> <property name="concurrent" value="false"></property> </bean> <!-- job的配置结束 --> <!-- 调度的配置开始 --> <bean id="taskJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail"> <ref bean="myJobDetail" /> </property> <property name="cronExpression"> <value>0/10 * * * * ?</value> </property> </bean> <!-- 调度的配置结束 --> </beans>
?
4.引入xml(app-context.xml spring的配置文件)
<import resource="app-scheduling.xml"/>
?
5. web.xml中的配置
<!-- Spring ApplicationContext配置文件的路径,可使用通配符,多个路径用,号分隔 此参数用于后面的Spring Context Loader --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/application-context.xml</param-value> </context-param> <!--Spring的ApplicationContext 载入 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
?
启动tomcat后,如果没有报错的话,就运行work方法了。
?