Spring配置Quartz例子
jopen
12年前
1.引入除spring.jar外的Quartz的jar包quartz-all-1.8.6.jar
2.任务类 QuartzJob
public class QuartzJob { public void work1() { System.out.println("Quartz的任务调度!!!work1启用……"); } public void work2() { System.out.println("Quartz的任务调度!!!work2启用……"); } }
2.Spring 配置 quartz-config.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd"> <!-- 线程执行器配置,用于任务注册 --> <bean id="executor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"> <property name="corePoolSize" value="10" /> <property name="maxPoolSize" value="100" /> <property name="queueCapacity" value="500" /> </bean> <!-- 要调用的工作类 --> <bean id="quartzJob" class="QuartzJob"></bean> <!--调度业务 : 定义调用对象和调用对象的方法 --> <bean id="jobtask1" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <!-- 调用的类 --> <property name="targetObject" ref="quartzJob" /> <!-- 调用类中的方法 --> <property name="targetMethod" value="work1" /> </bean> <bean id="jobtask2" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <!-- 调用的类 --> <property name="targetObject" ref="quartzJob" /> <!-- 调用类中的方法 --> <property name="targetMethod" value="work2" /> </bean> <!-- 定义触发时间 --> <bean id="doTime1" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref="jobtask1" /> <!-- cron表达式 --><!-- 每隔10秒执行一次 --> <property name="cronExpression" value="0/3 * * * * ?" /> </bean> <bean id="doTime2" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref="jobtask2" /> <property name="cronExpression" value="0/5 * * * * ?" /> </bean> <!--设置调度 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 --> <bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="doTime1" /> <ref bean="doTime2" /> </list> </property> <property name="taskExecutor" ref="executor" /> </bean> </beans>
3.加载 <!-- 加载spring -->
<context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/classes/quartz-config.xml </param-value> </context-param>