Quartz 在开源任务调度框架中的翘首,它提供了强大任务调度机制,难能可贵的是它同时保持了使用的简单性。
Quartz 允许开发人员灵活地定义触发器的调度时间表,并可以对触发器和任务进行关联映射。
Spring为创建Quartz的Scheduler、Trigger和JobDetail提供了便利的FactoryBean类,以便能够在Spring 容器中享受注入的好处。
此外Spring还提供了一些便利工具类直接将Spring中的Bean包装成合法的任务。
Spring进一步降低了使用Quartz的难度,能以更具Spring风格的方式使用Quartz。
概括来说它提供了两方面的支持:
1)为Quartz的重要组件类提供更具Bean风格的扩展类;
2)提供创建Scheduler的BeanFactory类,方便在Spring环境下创建对应的组件对象,并结合Spring容器生命周期进行启动和停止的动作。
注意:项目中需要加载需要加入spring.jar quartz-all-1.6.0.jar log4j-1.2.14.jar commons-collections.jar jta.jar commons-logging.jar这几个包
配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!--
DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd"
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd ">
<bean id="JobProcessBeanId" class="xxx.xxxxx.xx.xx.xxxxx.JobProcessService">
<property name="jdbcTmp" ref="jdbcTmp"></property>
</bean>
<bean id="XXXXXXReflushJob_onstart" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="JobProcessBeanId" />
</property>
<property name="targetMethod">
<value>doProcess</value>
</property>
</bean>
<bean id="doProcessTime" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="XXXXXXReflushJob_onstart" />
</property>
<!-- cron表达式 -->
<property name="cronExpression">
<value>0 * * * * ?</value>
</property>
</bean>
<!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 -->
<bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="doProcessTime" />
</list>
</property>
</bean>
</beans>
public class JobProcessService{
public void doProcess(){
System.out.println("处理业务");
}
}
本文介绍如何利用Spring框架整合Quartz实现定时任务的配置与管理。通过Spring提供的便捷工具,可以更轻松地创建、配置Quartz的任务调度器、触发器及作业。文中详细展示了配置示例,包括定义任务、触发器以及总管理类。
1135

被折叠的 条评论
为什么被折叠?



