1.详细介绍每种任务调度工具的使用方式,包括
Quartz
和
spring task
两种。
第一步:编写任务类
Java代码
public class Job2 { public void doJob2() { System.out.println("不继承QuartzJobBean方式-调度进行中..."); } }
可以看出,这就是一个普通的类,并且有一个方法。
第二步:配置作业类
Xml代码
<bean id="job2" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject"> <bean class="com.gy.Job2" /> </property> <property name="targetMethod" value="doJob2" /> <property name="concurrent" value="false" /><!-- 作业不并发调度 --> </bean>
说明:这一步是关键步骤,声明一个MethodInvokingJobDetailFactoryBean,有两个关键属性:targetObject指定任务类,targetMethod指定运行的方法。
第三步:配置作业调度的触发方式(触发器)
Quartz的作业触发器有两种,分别是
org.springframework.scheduling.quartz.SimpleTriggerBean
org.springframework.scheduling.quartz.CronTriggerBean
第一种
SimpleTriggerBean
,只支持按照一定频度调用任务,如每隔30分钟运行一次。
配置方式如下:
Xml代码
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean"> <property name="jobDetail" ref="job2" /> <property name="startDelay" value="0" /><!-- 调度工厂实例化后,经过0秒开始执行调度 --> <property name="repeatInterval" value="2000" /><!-- 每2秒调度一次 --> </bean>
第二种
CronTriggerBean
,支持到指定时间运行一次,如每天12:00运行一次等。
配置方式如下:
Xml代码
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref="job2" /> <!—每天12:00运行一次 --> <property name="cronExpression" value="0 0 12 * * ?" /> </bean>
以上两种调度方式根据实际情况,任选一种即可。
第四步:配置调度工厂
Xml代码
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="cronTrigger" /> </list> </property> </bean>
说明:该参数指定的就是之前配置的触发器的名字。
第五步:启动你的应用即可,即将工程部署至tomcat或其他容器
Spring-Task
上节介绍了在Spring 中使用Quartz,本文介绍Spring3.0以后自主开发的定时任务工具,spring task,可以将它比作一个轻量级的Quartz,而且使用起来很简单,除spring相关的包外不需要额外的包,而且支持注解和配置文件两种
形式,下面将分别介绍这两种方式。
第一种:配置文件方式
第一步:编写作业类
即普通的pojo,如下:
Java代码
import org.springframework.stereotype.Service; @Service public class TaskJob { public void job1() { System.out.println(“任务进行中。。。”); } }
第二步:在spring配置文件头中添加命名空间及描述
Xml代码
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:task="http://www.springframework.org/schema/task" 。。。。。。 xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
第三步:spring配置文件中设置具体的任务
Xml代码
<task:scheduled-tasks> <task:scheduled ref="taskJob" method="job1" cron="0 * * * * ?"/> </task:scheduled-tasks> <context:component-scan base-package=" com.gy.mytask " />
说明:ref参数指定的即任务类,method指定的即需要运行的方法,cron及cronExpression表达式,具体写法这里不介绍了,详情见上篇文章附录。
<context:component-scan base-package="com.gy.mytask" />这个配置不消多说了,spring扫描注解用的。
到这里配置就完成了,是不是很简单。
参考文献:
http://gong1208.iteye.com/blog/1773177
实例代码:
<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" />