spring定时任务总结

    

1、简介
这个系列介绍Spring框架实现定时任务的两种方式以及一些高级的用法,包括:
1、使用Quartz,这是一个功能比较强大的的调度器,可以让你的程序在指定时间执行,也可以按照某一个频度执行,配置起来稍显复杂,稍后会详细介绍。
2、Spring3.0以后自带的task,可以将它看成一个轻量级的Quartz,而且使用起来比Quartz简单许多,稍后会介绍。

2、Quartz的使用
作业类继承自特定基类的方式:org.springframework.scheduling.quartz.QuartzJobBean

①定义作业类:

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.jobDetailFactoryBean ">
    <property name="jobClass" value="com.gy.Job1" />
    <property name="jobDataAsMap">
        <map>
            <entry key="timeout" value="0" />
        </map>
    </property>
</bean>

说明:org.springframework.scheduling.quartz.JobDetailFactoryBean有两个属性,

jobClass属性即我们在java代码中定义的任务类,jobDataAsMap 任务类中的属性值map集合

③配置作业调度的触发方式(触发器)
Quartz的作业触发器有两种,分别是

org.springframework.scheduling.quartz.SimpleTriggerFactoryBean

org.springframework.scheduling.quartz.CronTriggerFactoryBean

第一种SimpleTriggerFactoryBean,只支持按照一定频度调用任务,如每隔30分钟运行一次。

配置方式如下:

<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
    <property name="jobDetail" ref="job2" />
    <property name="startDelay" value="0" /><!-- 调度工厂实例化后,经过0秒开始执行调度 -->
    <property name="repeatInterval" value="2000" /><!-- 每2秒调度一次 -->
</bean>

第二种CronTriggerFactoryBean,支持到指定时间运行一次,如每天12:00运行一次等。

<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
    <property name="jobDetail" ref="job1" />
    <!—每天12:00运行一次 -->
    <property name="cronExpression" value="0 0 12 * * ?" />
</bean>

④配置调度工厂

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="triggers">
        <list>
            <ref bean="cronTrigger" /> //这个配置的就是任务执行时间设置的bean(也就是第三步)
        </list>
    </property>
</bean>

⑤启动你的应用即可,即将工程部署至tomcat或其他容器。
到此,spring中Quartz的基本配置就介绍完了,当然了,使用之前,要导入相应的spring的包与Quartz的包,这些就不消多说了。

其实可以看出Quartz的配置看上去还是挺复杂的,没有办法,因为Quartz其实是个重量级的工具,如果我们只是想简单的执行几个简单的定时任务,有没有更简单的工具 请看我第下文Spring task的介绍。
特别的,关于CronExpression 的详细用户,请参考dary1715的博客

⑤启动你的应用即可,即将工程部署至tomcat或其他容器。
到此,spring中Quartz的基本配置就介绍完了,当然了,使用之前,要导入相应的spring的包与Quartz的包,这些就不消多说了。

其实可以看出Quartz的配置看上去还是挺复杂的,没有办法,因为Quartz其实是个重量级的工具,如果我们只是想简单的执行几个简单的定时任务,有没有更简单的工具 请看我第下文Spring task的介绍。
特别的,关于CronExpression 的详细用户,请参考dary1715的博客

3、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"/>

其他关于Spring Task使用的例子请参照 参考链接1 参考链接2

4 使用Spring @Scheduled

首先要配置我们的spring.xml


xmlns 多加下面的内容、

[html]  view plain  copy
  1. xmlns:task="http://www.springframework.org/schema/task"  


然后xsi:schemaLocation多加下面的内容、

[html]  view plain  copy
  1. http://www.springframework.org/schema/task  
  2. http://www.springframework.org/schema/task/spring-task-3.1.xsd  

最后是我们的task任务扫描注解

[html]  view plain  copy
  1. <task:annotation-driven/>  

我的配置扫描位置是:

[html]  view plain  copy
  1. <context:annotation-config/>  
  2.     <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>  
  3.     <context:component-scan base-package="com.test"/>  


扫描的是com.test这样的包下的内容、

下面需要接口和实现(我的这几个Java文件都是com.test的包下的、)

[java]  view plain  copy
  1. public interface IMyTestService {  
  2.        public void myTest();  
  3. }  



[java]  view plain  copy
  1. @Component  //import org.springframework.stereotype.Component;  
  2. public class MyTestServiceImpl  implements IMyTestService {  
  3.       @Scheduled(cron="0/5 * *  * * ? ")   //每5秒执行一次  
  4.       @Override  
  5.       public void myTest(){  
  6.             System.out.println("进入测试");  
  7.       }  
  8. }  

执行后控制台就会打印出   进入测试   了


需要注意的几点:

1、spring的@Scheduled注解  需要写在实现上、

2、 定时器的任务方法不能有返回值(如果有返回值,spring初始化的时候会告诉你有个错误、需要设定一个proxytargetclass的某个值为true、具体就去百度google吧)

3、实现类上要有组件的注解@Component


剩下的就是corn表达式了、具体使用以及参数请百度google、

下面只例出几个式子

CRON表达式    含义 
"0 0 12 * * ?"    每天中午十二点触发 
"0 15 10 ? * *"    每天早上10:15触发 
"0 15 10 * * ?"    每天早上10:15触发 
"0 15 10 * * ? *"    每天早上10:15触发 
"0 15 10 * * ? 2005"    2005年的每天早上10:15触发 
"0 * 14 * * ?"    每天从下午2点开始到2点59分每分钟一次触发 
"0 0/5 14 * * ?"    每天从下午2点开始到2:55分结束每5分钟一次触发 
"0 0/5 14,18 * * ?"    每天的下午2点至2:55和6点至6点55分两个时间段内每5分钟一次触发 
"0 0-5 14 * * ?"    每天14:00至14:05每分钟一次触发 
"0 10,44 14 ? 3 WED"    三月的每周三的14:10和14:44触发 
"0 15 10 ? * MON-FRI"    每个周一、周二、周三、周四、周五的10:15触发 






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值