一、背景 

  最近项目中需要使用到定时任务进行库存占用释放的需求,就总结了如何使用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. c# treeview 基本知识

    private void Form1_Load(object sender, EventArgs e) { BindTreeView(); treeView1.Focus(); treeView1.G ...

  2. bzoj4443[SCOI2015]小凸玩矩阵

    题意:一个n*m的矩阵(n<=m<=250),要求选出n个数(每行,每列最多选一个),求第k大数的最小值. 首先第k大的意思是从大到小的第k个数(我读错了,WA了一次还以为算法不对...) ...

  3. Zabbix监控mysql performance

    介绍 zabbix监控mysql性能,使用zabbix自带的mysql监控模板,可以监控以下内容OPS(增删改查).mysql慢查询数量.mysql请求\响应流量带宽 配置 新建mysql监控用户 G ...

  4. windows下python的web环境搭建使用(观看Backbone的教程有感)

    pip安装a 下载 get-pip.py (https://pip.pypa.io/en/latest/installing/#python-os-support b python get-pip.p ...

  5. 分布式开放消息系统(RocketMQ)的原理与实践

    分布式消息系统作为实现分布式系统可扩展.可伸缩性的关键组件,需要具有高吞吐量.高可用等特点.而谈到消息系统的设计,就回避不了两个问题: 消息的顺序问题 消息的重复问题 RocketMQ作为阿里开源的一 ...

  6. OVGap iOS与Javascript交互(H5与原生APP交互)

    源代码:https://github.com/windshg/OVGap OVGap:一个轻量级的类库,能够让iOS应用和远程网页的 Javascript 代码进行通信,也就是说,远程的 Javasc ...

  7. Android开发环境搭建

    导读: 学习Android开发第一步就是搭建Android开发环境. 1.安装JDK JDK(Java SE Development Kit)是Java的开发工具集.SE表示标准版. JRE(Java ...

  8. jQuery知识点二 实现隔行变色

    <!DOCTYPE html> <html> <head> <meta name="viewport" content="wid ...

  9. ActiveMQ集群下的消息回流功能

    ------------------------------------------------------------------ "丢失"的消息 如果有broker1和brok ...

  10. 哪些问题是面试官经常问Java工程师的问题 ? --- 转自quora

    Which are the frequently asked interview questions for Java Engineers ? Vivek Vermani, www.buggybrea ...