`
ITsource源代码
  • 浏览: 42159 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Spring进阶-怎样集成定时调度Quartz

阅读更多

 

 


 

    在一些项目里面比如进销存系统,对一些过期图片的定时清理或者库存不足发出预警提示,就需要用到定时调度技术。

 

    每当经过一段时间,程序会自动执行,就是定时调度。如果要使用定时调度,则必须保证程序始终运行才行哦,也就是说定时调度是在程序之外又启动了一个新的线程。那么怎样才能实现定时调度呢?

 

   

 

    方案一:可以使用JDK自带的java.util.Timer对象。可以创建定制的timer或者调用某些方法的timer。包装timer的工作由Spring TimerFactoryBean完成,不过这种方法不能准确设置一个起始时间。

 

 

 

    方案二:使用OpenSymphony Quartz框架。Quartz是OpenSymphony开源组织在Job scheduling领域又 一个开源项目,网站地址:http://www.quartz-scheduler.org;它可以与J2EE与J2SE应用程序相结合也可以单独使用。 Quartz可以用来创建简单或为运行好几万个Jobs这样复杂的程序。(Struts2里面的WebWork就是来自OpenSymphony。)

 

 

 

    Quartz使用Trigger, Job以及JobDetail等对象来进行各种类型的任务调度。为了让基于Spring的应用程序方便使用,Spring提供了一些类来简化quartz的用法。

 

 

 

    开发步骤:

 

    一、加入spring和quartz相关jar文件

 

     

 

    二、Spring配置文件

 

    1.Spring配置文件一:早期配置,麻烦

 

<?xml version="1.0" encoding="UTF-8"?>

 

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

 

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 -->

 

<bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">

 

<property name="triggers">

 

<list>

 

<ref bean="doTime" />

 

</list>

 

</property>

 

</bean>

 

<!-- 定义触发时间 -->

 

<bean id="doTime" class="org.springframework.scheduling.quartz.CronTriggerBean">

 

<property name="jobDetail">

 

<ref bean="jobtask" />

 

</property>

 

<property name="cronExpression">

 

<!-- cron表达式:在每天早上8点到晚上8点期间每1分钟触发一次 -->

 

<!-- <value>0 0/1 8-20 * * ?</value> -->

 

<!-- cron表达式:每5分钟触发一次 -->

 

<value>0 0/5 * * * ?</value>

 

</property>

 

</bean>

 

<!-- 定义调用对象和调用对象的方法 -->

 

<bean id="jobtask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">

 

<!-- 调用的类 -->

 

<property name="targetObject" ref="quartzJob" />

 

<!-- 调用类中的方法名称 -->

 

<property name="targetMethod">

 

<value>work</value>

 

</property>

 

</bean>

 

<!-- 要调用的工作类 -->

 

<bean id="quartzJob" class="cd.itsource.pss.service.impl.QuartzJobServiceImpl">

 

<property name="baseDao" ref="baseDao" />

 

</bean>

 

 

 

</beans>

 

 

 

    2.Spring配置文件二:加入spring命名空间,简化配置,强焊啊。原理就是在上面配置文件一的基础上spring再次做了一次封装,从而简化配置(具体可以看spring-task.xsd文件)。

 

<?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:task="http://www.springframework.org/schema/task"

 

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

 

http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">

 

 

 

<!-- cron表达式:在每天早上8点到晚上8点期间每1分钟触发一次 -->

 

<!--value>0 0/1 8-20 * * ?</value -->

 

<!-- cron表达式:每1分钟触发一次 -->

 

<!-- <value>0 0/1 * * * ?</value> -->

 

<!-- cron表达式:每天AM3点钟触发一次 -->

 

<!-- <value>0 0 3 * * ?</value> -->

 

 

 

<task:scheduled-tasks>

 

<!-- 执行quartzJob里面的work方法,执行频率是cron表达式 -->

 

<task:scheduled ref="quartzJob" method="work" cron="0 0/1 * * * ?" />

 

</task:scheduled-tasks>

 

 

 

<!-- 要调用的工作类 -->

 

<bean id="quartzJob" class="cn.itsource.pss.service.impl.QuartzJobServiceImpl">

 

<property name="baseDao" ref="baseDao" />

 

</bean>

 

 

 

</beans>

 

 

 

    三、Junit

 

    系统时间跑到整分的时候上面service的work方法自动被执行,间隔1分钟,再次被执行,后面循环执行。

 

     

 

 

    关注他

 

          

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics