1.引入除spring.jar外的Quartz的jar包quartz-all-1.8.6.jar
2.自定义实现任务类 QuartzJob#[普通的POJO]
publicclass QuartzJob {
publicvoid work1(){
System.out.println("Quartz的任务调度!!!work1启用……");
}
publicvoid work2(){
System.out.println("Quartz的任务调度!!!work2启用……");
}
}
<strong><span style="font-size:14px;">#3.Spring 配置 quartz-config.xml</span></strong>
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="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">
<!-- 线程执行器配置,用于任务注册 -->
<beanid="executor"
class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<propertyname="corePoolSize"value="10"/>
<propertyname="maxPoolSize"value="100"/>
<propertyname="queueCapacity"value="500"/>
</bean>
<!-- 要调用的工作类 -->
<beanid="quartzJob"class="QuartzJob"></bean>
<!--调度业务 : 定义调用对象和调用对象的方法 -->
<beanid="jobtask1" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 调用的类 -->
<propertyname="targetObject"ref="quartzJob"/>
<!-- 调用类中的方法 -->
<propertyname="targetMethod"value="work1"/>
</bean>
<beanid="jobtask2"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 调用的类 -->
<propertyname="targetObject"ref="quartzJob"/>
<!-- 调用类中的方法 -->
<propertyname="targetMethod"value="work2"/>
</bean>
<!-- 定义触发时间 -->
<beanid="doTime1"class="org.springframework.scheduling.quartz.CronTriggerBean">
<propertyname="jobDetail"ref="jobtask1"/>
<!-- cron表达式 --><!-- 每隔10秒执行一次 -->
<propertyname="cronExpression"value="0/3 * * * * ?" />
</bean>
<beanid="doTime2"class="org.springframework.scheduling.quartz.CronTriggerBean">
<propertyname="jobDetail"ref="jobtask2"/>
<propertyname="cronExpression"value="0/5 * * * * ?" />
</bean>
<!--设置调度 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 -->
<beanid="startQuertz"lazy-init="false"autowire="no"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<propertyname="triggers">
<list>
<refbean="doTime1"/>
<refbean="doTime2"/>
</list>
</property>
<propertyname="taskExecutor"ref="executor"/>
</bean>
</beans>
4.Servlet的配置文件web.xml中加载Spring配置文件
1
2
3
4
5
6
|
<
context-param
>
<
param-name
>contextConfigLocation</
param-name
>
<
param-value
>
/WEB-INF/classes/quartz-config.xml
</
param-value
>
</
context-param
>
|