spring两种定时器实现方式

1.详细介绍每种任务调度工具的使用方式,包括 Quartz spring task 两种。
第一步:编写任务类
Java代码  
  1. public class Job2 {  
  2. public void doJob2() {  
  3. System.out.println("不继承QuartzJobBean方式-调度进行中...");  
  4. }  
 可以看出,这就是一个普通的类,并且有一个方法。
第二步:配置作业类
Xml代码  
  1. <bean id="job2"  
  2. class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  
  3. <property name="targetObject">  
  4. <bean class="com.gy.Job2" />  
  5. </property>  
  6. <property name="targetMethod" value="doJob2" />  
  7. <property name="concurrent" value="false" /><!-- 作业不并发调度 -->  
  8. </bean>  
 说明:这一步是关键步骤,声明一个MethodInvokingJobDetailFactoryBean,有两个关键属性:targetObject指定任务类,targetMethod指定运行的方法。
第三步:配置作业调度的触发方式(触发器)
Quartz的作业触发器有两种,分别是
org.springframework.scheduling.quartz.SimpleTriggerBean
org.springframework.scheduling.quartz.CronTriggerBean
第一种 SimpleTriggerBean ,只支持按照一定频度调用任务,如每隔30分钟运行一次。
配置方式如下:
Xml代码  
  1. <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">  
  2. <property name="jobDetail" ref="job2" />  
  3. <property name="startDelay" value="0" /><!-- 调度工厂实例化后,经过0秒开始执行调度 -->  
  4. <property name="repeatInterval" value="2000" /><!-- 每2秒调度一次 -->  
  5. </bean>  
第二种 CronTriggerBean ,支持到指定时间运行一次,如每天12:00运行一次等。
配置方式如下:
Xml代码  
  1. <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">  
  2. <property name="jobDetail" ref="job2" />  
  3. <!—每天12:00运行一次 -->  
  4. <property name="cronExpression" value="0 0 12 * * ?" />  
  5. </bean>  
以上两种调度方式根据实际情况,任选一种即可。
第四步:配置调度工厂 
Xml代码  
  1. <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
  2. <property name="triggers">  
  3. <list>  
  4. <ref bean="cronTrigger" />  
  5. </list>  
  6. </property>  
  7. </bean>  
说明:该参数指定的就是之前配置的触发器的名字。
第五步:启动你的应用即可,即将工程部署至tomcat或其他容器

Spring-Task
上节介绍了在Spring 中使用Quartz,本文介绍Spring3.0以后自主开发的定时任务工具,spring task,可以将它比作一个轻量级的Quartz,而且使用起来很简单,除spring相关的包外不需要额外的包,而且支持注解和配置文件两种
形式,下面将分别介绍这两种方式。
第一种:配置文件方式
第一步:编写作业类
即普通的pojo,如下:
Java代码  
  1. import org.springframework.stereotype.Service;  
  2. @Service  
  3. public class TaskJob {  
  4.       
  5.     public void job1() {  
  6.         System.out.println(“任务进行中。。。”);  
  7.     }  
  8. }  
 第二步:在spring配置文件头中添加命名空间及描述
Xml代码  
  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.     xmlns:task="http://www.springframework.org/schema/task"   
  3.     。。。。。。  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">  
 第三步:spring配置文件中设置具体的任务
Xml代码  
  1.  <task:scheduled-tasks>   
  2.         <task:scheduled ref="taskJob" method="job1" cron="0 * * * * ?"/>   
  3. </task:scheduled-tasks>  
  4.   
  5. <context:component-scan base-package=" com.gy.mytask " />  
说明:ref参数指定的即任务类,method指定的即需要运行的方法,cron及cronExpression表达式,具体写法这里不介绍了,详情见上篇文章附录。
<context:component-scan base-package="com.gy.mytask" />这个配置不消多说了,spring扫描注解用的。
到这里配置就完成了,是不是很简单。


实例代码:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<!-- <task:scheduled-tasks> -->
<!-- <task:scheduled ref="taskJob" method="addAllBothty" cron="0 0 12 * * ?"/> --> 十二点执行
<!-- <task:scheduled ref="taskJob" method="checkYj_hd" cron="0 */3 * * * ? "/> -->
<!-- </task:scheduled-tasks> -->
<context:component-scan base-package="com.sinosoft.utils" />

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值