一、背景 

  最近项目中需要使用到定时任务进行库存占用释放的需求,就总结了如何使用Spring Task进行简单配置完成该需求,本文介绍Spring3.0以后自定义开发的定时任务工具,

  spring task,我们可以将它比作一个轻量级的Quartz,使用简单方便,除spring相关的包外不需要额外的包,而且支持注解和配置文件两种形式,下面我会分别介绍这两种方式。

二、定时任务开发步骤

开发环境

  • Spring 4.2.6.RELEASE
  • Maven 3.3.9
  • JDK 1.7
  • Idea 15.04

【1】.基于配置文件

 1.编写普通java class

package com.hafiz.www.cron;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory; /**
* Desc:第一个基于SpringTask的调度任务
* Created by hafiz.zhang on 2016/12/11.
*/
public class FirstCron {
private static final Logger logger = LoggerFactory.getLogger(FirstCron.class); public void cron() {
logger.info("定时任务进行中.......");
// do something else
}
}

2.在spring配置文件头中添加命名空间及描述(下面加粗处)并配置定时任务

 <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.2.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"> <bean id="firstCron" class="com.hafiz.www.cron.FirstCron"/>
<task:scheduled-tasks>
<task:scheduled ref="firstCron" method="cron" cron="0/5 * * * * ?"/>
</task:scheduled-tasks>
</beans>

我们设置每5秒钟运行一次。关于Spring Task 的 cron表达式,请参见另一篇博客:摆脱Spring 定时任务的@Scheduled cron表达式的困扰

【2】基于注解

 我们可以使用@Scheduled注解进行开发,首先我们看下,该注解的源码

 package org.springframework.scheduling.annotation;

 import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.scheduling.annotation.Schedules; @Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(Schedules.class)
public @interface Scheduled {
String cron() default ""; String zone() default ""; long fixedDelay() default -1L; String fixedDelayString() default ""; long fixedRate() default -1L; String fixedRateString() default ""; long initialDelay() default -1L; String initialDelayString() default "";
}

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

  • cron:指定cron表达式
  • zone:官方文档解释:A time zone for which the cron expression will be resolved。指定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.即从上一个任务开始到下一个任务开始的间隔,单位是毫秒。
  • initialDelay:官方文档解释:Number of milliseconds to delay before the first execution of a fixedRate() or fixedDelay() task.任务第一次被调用前的延时,单位毫秒

1.首先编写注解的定时任务类

 package com.hafiz.www.cron;

 import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled; /**
* Desc:第一个基于SpringTask的调度任务
* Created by hafiz.zhang on 2016/12/11.
*/
public class FirstCron {
private static final Logger logger = LoggerFactory.getLogger(FirstCron.class); @Scheduled(cron = "0/5 * * * * ?")
public void cron() {
logger.info("定时任务进行中.......");
// do something else
}
}

2.在spring配置文件头中添加命名空间及描述(下面加粗处)并开启定时任务注解驱动

 <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.2.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"> <bean id="firstCron" class="com.hafiz.www.cron.FirstCron"/>
<task:scheduler id="qbScheduler" pool-size="10"/>
<task:annotation-driven scheduler="qbScheduler" mode="proxy"/>
<!--简单来说,我们只需要<task:annotation-driven/>这一句即可,这些参数不是必须的 -->
</beans>

 以上我们就完成了基于注解的定时任务的开发,是不是很简单?

运行结果:

三、总结

  其实有些知识我们表面上看起来很难,但是当我们实际操作的时候,发现挺简单的,只要遇到问题我们勤思考多思考,就一定会有解决办法。关于定时任务,还有一种基于Spring Quartz的实现,以后有需要,我们再进行介绍。欢迎留言交流.......

使用Spring Task轻松完成定时任务的更多相关文章

  1. Spring Task中的定时任务无法注入service的解决办法

    1.问题 因一个项目(使用的是Spring+SpringMVC+hibernate框架)需要在spring task定时任务中调用数据库操作,在使用 @Autowired注入service时后台报错, ...

  2. spring-boot-route(二十)Spring Task实现简单定时任务

    Spring Task是Spring 3.0自带的定时任务,可以将它看作成一个轻量级的Quartz,功能虽然没有Quartz那样强大,但是使用起来非常简单,无需增加额外的依赖,可直接上手使用. 一 如 ...

  3. 使用Spring整合Quartz轻松完成定时任务

    一.背景 上次我们介绍了如何使用Spring Task进行完成定时任务的编写,这次我们使用Spring整合Quartz的方式来再一次实现定时任务的开发,以下奉上开发步骤及注意事项等. 二.开发环境及必 ...

  4. Spring整合Quartz轻松完成定时任务

    一.背景 上次我们介绍了如何使用Spring Task进行完成定时任务的编写,这次我们使用Spring整合Quartz的方式来再一次实现定时任务的开发,以下奉上开发步骤及注意事项等. 二.开发环境及必 ...

  5. 基于Spring Task的定时任务调度器实现

    在很多时候,我们会需要执行一些定时任务 ,Spring团队提供了Spring Task模块对定时任务的调度提供了支持,基于注解式的任务使用也非常方便. 只要跟需要定时执行的方法加上类似 @Schedu ...

  6. spring定时任务轮询(spring Task)

    定时任务轮询比如任务自服务器启动就开始运行,并且每隔5秒执行一次. 以下用spring注解配置定时任务.1.添加相应的schema xmlns:task=" xsi:schemaLocati ...

  7. 【spring】task 任务调度(定时任务)

    1.定时任务的几种实现可以看这里:http://gong1208.iteye.com/blog/1773177 2.需要导入spring的jar包,可以参看之前的[spring]相关文章 3.这里使用 ...

  8. Spring task定时任务执行一段时间后莫名其妙停止的问题

    前因: 我写了一个小项目,主要功能是用Spring task定时任务每天定时给用户发送邮件.执行了几个月一直没有问题,前几天,莫名其妙的突然不再发送邮件了. 只好花费一些时间来查看到底是什么原因造成的 ...

  9. 转载《spring定时任务轮询(spring Task)》

    亲测可用 原文网址:http://blog.csdn.net/wanglha/article/details/51026697 本博主注:xmlns:task="http://www.spr ...

随机推荐

  1. Java之类的构造器(反射)

    反射: Java反射机制:指的是在Java程序运行状态中,对于任何一个类,都可以获得这个类的所有属性和方法;对于给定的一个对象,都能够调用它的任意一个属性和方法.这种动态获取类的内容以及动态调用对象的 ...

  2. java网络编程精解demo1---读取用户控制台的输入的数据并显示

    package test3; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream ...

  3. Sublime 插件- px 转rem

    一个CSS的px值转rem值的Sublime Text 3自动完成插件. 插件效果如下: 安装 克隆项目   https://github.com/hyb628/cssrem.git 进入packag ...

  4. Android Automotive开发之一《编译自己的SDK 》 // TOBEDONE

    自己动手编译最新Android源码及SDK : http://blog.csdn.net/dd864140130/article/details/51718187官方文档,怎样编译sdk : http ...

  5. href,src,url 整理

    一.href 和 src 的定义及区别 href:Hypertext Reference(超文本引用),指定网络资源的位置,从而在当前元素或者当前文档和由当前属性定义的需要的锚点或资源之间定义一个链接 ...

  6. win7系统的右键菜单只显示一个白色框不显示菜单项 解决办法

    如上图所示,桌面或其他大部分地方点击右键菜单,都只显示一个白色框,鼠标移上去才有菜单项看,并且效果很丑 解决办法: 计算机-右键-属性-高级-性能-设置-视觉效果-淡入淡出或滑动菜单到视图,将其前面的 ...

  7. SpringMVC学习记录5

    Springmvc流程中的扩展点有很多,可以在很多地方插入自己的代码逻辑达到控制流程的目的. 如果要对Controller的handler方法做统一的处理.我想应该会有很多选择,比如:@ModelAt ...

  8. java初始化

    一.成员初始化 1.成员变量没有赋值,则被初始化成默认值. 2.局部变量没有赋值,编译时报错. 二.构造器初始化 1.成员变量在构造器初始化之前,已经被初始化. 2.变量定义的顺序决定了初始化的顺序. ...

  9. touch

    Linux touch 命令   在 Linux 下运用 touch 命令创建一个空文件.当然我们也可以使用其他命令例如 vi, nano 或是任意一个编辑工具来实现.但是你可能需要更多的步骤来完成操 ...

  10. CSS Sprites优缺点

    利用CSS Sprites能很好地减少网页的http请求,从而大大的提高页面的性能,这也是CSS Sprites最大的优点,也是其被广泛传播和应用的主要原因: CSS Sprites能减少图片的字节, ...