本文来自网络一些博客的整理(包括gong1208的博客 dary1715的博客)
一、简介
这个系列介绍Spring框架实现定时任务的两种方式以及一些高级的用法,包括:
一、使用Quartz,这是一个功能比较强大的的调度器,可让你的程序在指定时间执行,也能够按照某一个频度执行,配置起来稍显复杂,稍后会详细介绍。
二、Spring3.0之后自带的task,能够将它当作一个轻量级的Quartz,并且使用起来比Quartz简单许多,稍后会介绍。java
二、Quartz的使用
做业类继承自特定基类的方式:org.springframework.scheduling.quartz.QuartzJobBeanspring
①定义做业类:tomcat
import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.scheduling.quartz.QuartzJobBean; public class Job1 extends QuartzJobBean { private int timeout; private static int i = 0; //调度工厂实例化后,通过timeout时间开始执行调度 public void setTimeout(int timeout) { this.timeout = timeout; } /** * 要调度的具体任务 */ @Override protected void executeInternal(JobExecutionContext context) throws JobExecutionException { System.out.println("定时任务执行中…"); } }
②spring配置文件中配置做业类JobDetailBean网络
<bean name="job1" class="org.springframework.scheduling.quartz.JobDetailBean"> <property name="jobClass" value="com.gy.Job1" /> <property name="jobDataAsMap"> <map> <entry key="timeout" value="0" /> </map> </property> </bean>
说明:org.springframework.scheduling.quartz.JobDetailBean有两个属性,jobClass属性即咱们在java代码中定义的任务类,jobDataAs框架
③配置做业调度的触发方式(触发器)
Quartz的做业触发器有两种,分别是ide
org.springframework.scheduling.quartz.SimpleTriggerBean工具
org.springframework.scheduling.quartz.CronTriggerBeanthis
第一种SimpleTriggerBean,只支持按照必定频度调用任务,如每隔30分钟运行一次。.net
配置方式以下:code
<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运行一次等。
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref="job2" /> <!—天天12:00运行一次 --> <property name="cronExpression" value="0 0 12 * * ?" /> </bean>
④配置调度工厂
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="cronTrigger" /> </list> </property> </bean>
⑤启动你的应用便可,即将工程部署至tomcat或其余容器。
到此,spring中Quartz的基本配置就介绍完了,固然了,使用以前,要导入相应的spring的包与Quartz的包,这些就不消多说了。
其实能够看出Quartz的配置看上去仍是挺复杂的,没有办法,由于Quartz实际上是个重量级的工具,若是咱们只是想简单的执行几个简单的定时任务,有没有更简单的工具 请看我第下文Spring task的介绍。
特别的,关于CronExpression 的详细用户,请参考dary1715的博客
三、Spring Task
上节介绍了在Spring 中使用Quartz,本文介绍Spring3.0之后自主开发的定时任务工具,spring task,能够将它比做一个轻量级
的Quartz,并且使用起来很简单,除spring相 关的包外不须要额外的包,并且支持注解和配置文件两种
形式,下面将分别介绍这两种方式。
配置文件方式:
①配置做业类
即普通的pojo,以下:
import org.springframework.stereotype.Service; @Service public class TaskJob { public void job1() { System.out.println(âtask exe...â); } }
②在spring配置文件头中添加命名空间及描述
<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配置文件中设置具体的任务
<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表达式,具体写法这里不介绍了,详情见上篇文章附录。
这个配置不消多说了,spring扫描注解用的。
到这里配置就完成了,是否是很简单。
使用注解形式:
① 编写普通的POJO
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component(“taskJob”) public class TaskJob { @Scheduled(cron = "0 0 3 * * ?") public void job1() { System.out.println(“任务进行中。。。”); } }
②添加task相关的配置:
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd" default-lazy-init="false"> <context:annotation-config /> <!—spring扫描注解的配置 --> <context:component-scan base-package="com.gy.mytask" /> <!—开启这个配置,spring才能识别@Scheduled注解 --> <task:annotation-driven scheduler="qbScheduler" mode="proxy"/> <task:scheduler id="qbScheduler" pool-size="10"/>