使用Spring Task轻松完成定时任务
一、背景
最近项目中需要使用到定时任务进行库存占用释放的需求,就总结了如何使用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()
orfixedDelay()
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轻松完成定时任务的更多相关文章
- Spring Task中的定时任务无法注入service的解决办法
1.问题 因一个项目(使用的是Spring+SpringMVC+hibernate框架)需要在spring task定时任务中调用数据库操作,在使用 @Autowired注入service时后台报错, ...
- spring-boot-route(二十)Spring Task实现简单定时任务
Spring Task是Spring 3.0自带的定时任务,可以将它看作成一个轻量级的Quartz,功能虽然没有Quartz那样强大,但是使用起来非常简单,无需增加额外的依赖,可直接上手使用. 一 如 ...
- 使用Spring整合Quartz轻松完成定时任务
一.背景 上次我们介绍了如何使用Spring Task进行完成定时任务的编写,这次我们使用Spring整合Quartz的方式来再一次实现定时任务的开发,以下奉上开发步骤及注意事项等. 二.开发环境及必 ...
- Spring整合Quartz轻松完成定时任务
一.背景 上次我们介绍了如何使用Spring Task进行完成定时任务的编写,这次我们使用Spring整合Quartz的方式来再一次实现定时任务的开发,以下奉上开发步骤及注意事项等. 二.开发环境及必 ...
- 基于Spring Task的定时任务调度器实现
在很多时候,我们会需要执行一些定时任务 ,Spring团队提供了Spring Task模块对定时任务的调度提供了支持,基于注解式的任务使用也非常方便. 只要跟需要定时执行的方法加上类似 @Schedu ...
- spring定时任务轮询(spring Task)
定时任务轮询比如任务自服务器启动就开始运行,并且每隔5秒执行一次. 以下用spring注解配置定时任务.1.添加相应的schema xmlns:task=" xsi:schemaLocati ...
- 【spring】task 任务调度(定时任务)
1.定时任务的几种实现可以看这里:http://gong1208.iteye.com/blog/1773177 2.需要导入spring的jar包,可以参看之前的[spring]相关文章 3.这里使用 ...
- Spring task定时任务执行一段时间后莫名其妙停止的问题
前因: 我写了一个小项目,主要功能是用Spring task定时任务每天定时给用户发送邮件.执行了几个月一直没有问题,前几天,莫名其妙的突然不再发送邮件了. 只好花费一些时间来查看到底是什么原因造成的 ...
- 转载《spring定时任务轮询(spring Task)》
亲测可用 原文网址:http://blog.csdn.net/wanglha/article/details/51026697 本博主注:xmlns:task="http://www.spr ...
随机推荐
- Java中private、protected、public和default的区别
public: 具有最大的访问权限,可以访问任何一个在classpath下的类.接口.异常等.它往往用于对外的情况,也就是对象或类对外的一种接口的形式. protected: 主要的作用就是用来保护子 ...
- python读取caffemodel文件
caffemodel是二进制的protobuf文件,利用protobuf的python接口可以读取它,解析出需要的内容 不少算法都是用预训练模型在自己数据上微调,即加载"caffemodel ...
- js修改伪类的值
css文件 p.change:after { content: attr(data-content); } js文件 $(this).addClass('change').attr('data-con ...
- 父子页面之间元素相互操作(iframe子页面)
js/jquery获取iframe子页面中元素的方法: 一.使用window.frames["iframe的ID"]获取元素 window.onload = function() ...
- 异常 Exception
异常:是指在程序运行的过程中发生的一些不正常的时间. 分为受查异常和非受查异常. 受查异常:编译时期出现的异常 除了RuntimeException的异常,必须处理以及throws 非受查异常:运 ...
- Angular 之坑??
1. 场景:bootstrap的popup画面中有一个select元素, <select > <option> 1 </option> <option ng- ...
- 常见ES5方法
• ES5 JSON扩展JSON.parseJSON.stringify • ES5 Object扩展Object.createObject.keys • Date对象Date.now • ES5 F ...
- Bubble Cup 8 finals G. Run for beer (575G)
题意: 给定一个带权无向图,每条边的代价为边权/当前速度,每次到达一个新节点,速度都会除以10. 求0号点到n-1号点的最小代价,如果多解输出点数最少的解,输出代价.路径点数.路径经过的点. 1< ...
- KALI Linux problems & Study Red Hat | Ubuntu
Problem When you ask some website with https head.you may met the problem secure connection failed ...
- java基础 常用组件
几个常用组件: 在图形用户界面编程中,我们常常会提供用户登陆界面,比如登陆到会员管理系统,登陆到工资管理系统,仓库管理系统等,如下图我们就会用到: 1. 文本框(JTextField) 2. 密码框( ...