一.前言

上面两篇介绍了在Spring 中使用Timer与Quartz,本篇将介绍Spring3.0以后自主开发的定时任务工具,spring task,可以将它比作一个轻量级的Quartz,而且使用起来很简单,除spring相关的包外不需要额外的包,而且支持注解和配置文件两种形式,下面将分别介绍这两种方式。


二、第一种:配置文件方式

 第一步:编写作业类 

             即普通的pojo,如下:

import org.springframework.stereotype.Service;
@Service
public class TaskJob { public void job1() {
System.out.println(“任务进行中。。。”);
}
}

第二步: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">

<task:scheduled-tasks>
                  <task:scheduled ref="taskJob" method="job1" cron="0 * * * * ?"/>
      </task:scheduled-tasks>
     <context:component-scan base-package=" com.gy.mytask " />

</beans>

说明:ref参数指定的即任务类,method指定的即需要运行的方法,cron及cronExpression表达式。<context:component-scan base-package="com.gy.mytask" />这个配置不消多说了,spring扫描注解用的。


第二种:使用注解形式

也许我们不想每写一个任务类还要在xml文件中配置下,我们可以使用注解@Scheduled,我们看看源文件中该注解的定义:

@Target({java.lang.annotation.ElementType.METHOD, java.lang.annotation.ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Scheduled
{
public abstract String cron(); public abstract long fixedDelay(); public abstract long fixedRate();
}

可以看出该注解有三个方法或者叫参数,分别表示的意思是:

cron:指定cron表达式。

fixedDelay:官方文档解释:An interval-based trigger where the interval is measured from the completion time of the previous task. The time unit value is measured in milliseconds.即表示从                       上一个任务完成开始到下一个任务开始的间隔,单位是毫秒。

fixedRate:官方文档解释:An interval-based trigger where the interval is measured from the start time of the previous task. The time unit value is measured in milliseconds.即从上一个任务                        开始到下一个任务开始的间隔,单位是毫秒。

第一步:编写pojo

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

说明:理论上只需要加上<task:annotation-driven />这句配置就可以了,这些参数都不是必须的。

配置完毕,spring task还有很多参数,具体参考xsd文档 http://www.springframework.org/schema/task/spring-task-3.0.xsd

Spring任务调度之Spring-Task的更多相关文章

  1. Spring任务调度<task:scheduled-tasks>【含cron参数详解】 (转载)

    Spring内部有一个task是Spring自带的一个设定时间自动任务调度 task使用的时候很方便,但是他能做的东西不如quartz那么的多! 可以使用注解和配置两种方式,配置的方式如下 引入Spr ...

  2. Spring任务调度

    任务调度是大多数应用系统的常见需求之一,拿论坛来说:每个半个小时生成精华文章的RSS文件,每天凌晨统计论坛用户的积分排名,每隔30分钟执行对锁定过期的用户进行解锁.以上都是以时间为关注点的调度,事实上 ...

  3. spring 任务调度quartz

    简单记录一下spring任务调度quartz的例子 首先添加包 quartz-2.2.3.jar 然后写个简单的TestJob类 package com.job; import java.util.D ...

  4. spring笔记3 spring MVC的基础知识3

    4,spring MVC的视图 Controller得到模型数据之后,通过视图解析器生成视图,渲染发送给用户,用户就看到了结果. 视图:view接口,来个源码查看:它由视图解析器实例化,是无状态的,所 ...

  5. 一句话概括下spring框架及spring cloud框架主要组件

    作为java的屌丝,基本上跟上spring屌丝的步伐,也就跟上了主流技术.spring 顶级项目:Spring IO platform:用于系统部署,是可集成的,构建现代化应用的版本平台,具体来说当你 ...

  6. spring计划任务,springMvc计划任务,Spring@Scheduled,spring定时任务

    spring计划任务,springMvc计划任务,Spring@Scheduled,spring定时任务 >>>>>>>>>>>> ...

  7. 一:Spring Boot、Spring Cloud

    上次写了一篇文章叫Spring Cloud在国内中小型公司能用起来吗?介绍了Spring Cloud是否能在中小公司使用起来,这篇文章是它的姊妹篇.其实我们在这条路上已经走了一年多,从16年初到现在. ...

  8. 基于Spring Boot和Spring Cloud实现微服务架构学习

    转载自:http://blog.csdn.net/enweitech/article/details/52582918 看了几周Spring相关框架的书籍和官方demo,是时候开始总结下这中间的学习感 ...

  9. 基于Spring Boot和Spring Cloud实现微服务架构学习--转

    原文地址:http://blog.csdn.net/enweitech/article/details/52582918 看了几周spring相关框架的书籍和官方demo,是时候开始总结下这中间的学习 ...

  10. 基于Spring Boot和Spring Cloud实现微服务架构

    官网的技术导读真的描述的很详细,虽然对于我们看英文很费劲,但如果英文不是很差,请选择沉下心去读,你一定能收获好多.我的学习是先从Spring boot开始的,然后接触到微服务架构,当然,这一切最大的启 ...

随机推荐

  1. WPF面试准备

    1.wpf中有两类模板,控件模板controltemplate和datatemplate都派生自Frameworktemplate. 总共有三大模板 ControlTemplate,ItemsPane ...

  2. Startup配置类 居然又是约定

    Microsoft.Owin.Host.SystemWeb 这个dll可以让OWin接管IIS的请求,虽然同样是托管在IIS,但是所有的请求都会被OWin来处理.在OWin的4层结构中(Applica ...

  3. SQL case

    case when Value='1' then 'True'  else 'False' end as 'Result'

  4. 10月25日下午PHP静态、抽象、接口

    多态(运行多态)概念:当父类引用指向子类实例,由于子类里面对父类的方法进行了重写,父类引用在调用该方法的时候表现出的不同状态.条件:1.必须发生在继承下2.必须重写父类方法3.父类引用调用该方法 如果 ...

  5. winston写日志(译)

    使用 有两种方式去使用winston,直接通过默认的logger,或者实例化自己的Logger,前者设计的目的是在你的应用程序中共享logger比较方便. 使用默认Logger 使用默认的logger ...

  6. 解决COM组件80070005错误

    前段时间在维护公司以前的项目时遇到一个问题,客户需要添加一个word文档合并功能,按理说这功能比较好实现,只要调用Office自带的COM组件就搞定了,但实际上并非如此,在客户端部署上以后运行报错,提 ...

  7. 启动网卡报:Device eth0 does not seem to be present”解决办法

    Device eth0 does not seem to be present”解决办法 : 用ifconfig查看发现缺少eth0,只有lo:用ifconfig -a查看发现多出了eth1的信息. ...

  8. centos7安装nexus私服2.14

    今天项目经理叫搭个nexus私服,记录一下 nexus下载比较慢,可在本地下载,然后用sftp上传到linux系统去,下载地址https://www.sonatype.com/download-oss ...

  9. SameSite Cookie,防止 CSRF 攻击

    因为 HTTP 协议是无状态的,所以很久以前的网站是没有登录这个概念的,直到网景发明 cookie 以后,网站才开始利用 cookie 记录用户的登录状态.cookie 是个好东西,但它很不安全,其中 ...

  10. 腾讯WEB前端开发面试经历,一面二面HR面,面面不到!

    [一面]~=110分钟  2014/09/24 11:20  星期三 进门静坐30分钟做题. 填空题+大题+问答题 >>填空题何时接触电脑 何时接触前端运算符 字符串处理        延 ...