使用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配置文件头中添加命名空间及描述(下面加粗处)并配置定时任务
1 <beans xmlns="http://www.springframework.org/schema/beans" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns:task="http://www.springframework.org/schema/task" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans 5 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 6 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"> 7 8 <bean id="firstCron" class="com.hafiz.www.cron.FirstCron"/> 9 <task:scheduled-tasks> 10 <task:scheduled ref="firstCron" method="cron" cron="0/5 * * * * ?"/> 11 </task:scheduled-tasks> 12 </beans>
我们设置每5秒钟运行一次。关于Spring Task 的 cron表达式,请参见另一篇博客:摆脱Spring 定时任务的@Scheduled cron表达式的困扰
【2】基于注解
我们可以使用@Scheduled注解进行开发,首先我们看下,该注解的源码
1 package org.springframework.scheduling.annotation; 2 3 import java.lang.annotation.Documented; 4 import java.lang.annotation.ElementType; 5 import java.lang.annotation.Repeatable; 6 import java.lang.annotation.Retention; 7 import java.lang.annotation.RetentionPolicy; 8 import java.lang.annotation.Target; 9 import org.springframework.scheduling.annotation.Schedules; 10 11 @Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE}) 12 @Retention(RetentionPolicy.RUNTIME) 13 @Documented 14 @Repeatable(Schedules.class) 15 public @interface Scheduled { 16 String cron() default ""; 17 18 String zone() default ""; 19 20 long fixedDelay() default -1L; 21 22 String fixedDelayString() default ""; 23 24 long fixedRate() default -1L; 25 26 String fixedRateString() default ""; 27 28 long initialDelay() default -1L; 29 30 String initialDelayString() default ""; 31 }
可以看出该注解有五个方法或者叫参数,分别表示的意思是:
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.编写注解的定时任务类
1 package com.hafiz.www.cron; 2 3 import org.slf4j.Logger; 4 import org.slf4j.LoggerFactory; 5 import org.springframework.scheduling.annotation.Scheduled; 6 7 /** 8 * Desc:第一个基于SpringTask的调度任务 9 * Created by hafiz.zhang on 2016/12/11. 10 */ 11 public class FirstCron { 12 private static final Logger logger = LoggerFactory.getLogger(FirstCron.class); 13 14 @Scheduled(cron = "0/5 * * * * ?") 15 public void cron() { 16 logger.info("定时任务进行中......."); 17 // do something else 18 } 19 }
2.在spring配置文件头中添加命名空间及描述(下面加粗处)并开启定时任务注解驱动
1 <beans xmlns="http://www.springframework.org/schema/beans" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns:task="http://www.springframework.org/schema/task" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans 5 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 6 http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"> 7 8 <bean id="firstCron" class="com.hafiz.www.cron.FirstCron"/> 9 <task:scheduler id="qbScheduler" pool-size="10"/> 10 <task:annotation-driven scheduler="qbScheduler" mode="proxy"/> 11 <!--简单来说,我们只需要<task:annotation-driven/>这一句即可,这些参数不是必须的 --> 12 </beans>
以上我们就完成了基于注解的定时任务的开发,是不是很简单?
运行结果:
三、总结
其实有些知识我们表面上看起来很难,但是当我们实际操作的时候,发现挺简单的,只要遇到问题我们勤思考多思考,就一定会有解决办法。关于定时任务,还有一种基于Spring Quartz的实现,以后有需要,我们再进行介绍。欢迎留言交流.......
转载于:https://www.cnblogs.com/gscq073240/articles/7830238.html
使用Spring Task轻松完成定时任务相关推荐
- 再见 Spring Task,这个定时任务框架真香!
最近有朋友问到定时任务相关的问题. 于是,我简单写了一篇文章总结一下定时任务的一些概念以及一些常见的定时任务技术选型.希望能对小伙伴们有帮助! 个人能力有限.如果文章有任何需要补充/完善/修改的地方, ...
- spring中轻松实现定时任务,quartz表达式的在线Cron表达式生成器推荐
东西虽简单,保留下来,开箱即用,省时省力!!!!! 1.首先在pom.xml中引入quartz相关包 <dependency> <groupId>o ...
- 使用Spring整合Quartz轻松完成定时任务
一.背景 上次我们介绍了如何使用Spring Task进行完成定时任务的编写,这次我们使用Spring整合Quartz的方式来再一次实现定时任务的开发,以下奉上开发步骤及注意事项等. 二.开发环境及必 ...
- Spring Task定时任务的配置和使用详解
spring中使用定时任务 1.基于xml配置文件使用定时任务 首先配置spring开启定时任务 <beans xmlns="http://www.springframework.or ...
- 使用Spring Task完成定时任务
1. 前言 上一篇我们学习了Quartz作为定时任务的框架的使用, 这一篇我们来学习Spring全家桶的SpringTask, 对于主张简单易用的Spring家族来说, SpringTask无疑也是一 ...
- java实现每天定时执行任务,Spring Task定时任务每天零点执行一次的操作
最近根据项目的需求,需要限制用户每天的发送短信数量.这样以来就需要写一个定时任务,每天去置零一次所有用户的发送短信统计数量. 首先,在application.xml文件中添加 接着就是编写自己的业务处 ...
- 使用Spring Task实现定时任务
文章目录 SpringMVC 配置方式 添加命名空间 配置Task注解扫描 Springboot配置方式 定义定时任务 异常处理 项目中实现定时任务有多种方式,除了TimerTask这种小工具之外,以 ...
- Spring Task定时任务
◆Spring Task是Spring 3.0后推出的定时任务模块 ◆Spring Task的职责是按周期后台自动执行任务 ◆Spring Task可利用Cron表达式实现灵活的定时处理 在第一行当中 ...
- 【Spring】两步轻松实现定时任务
[Spring]两步轻松实现定时任务 已完结 文章目录 [Spring]两步轻松实现定时任务 一.两步编码实现定时任务 1. 开启定时任务注解 2. 设置执行时间 3. cron表达式实现 二.cro ...
最新文章
- Example002定时打开窗口
- 3号团队-团队任务4:每日例会(2018-12-3)
- /proc/acpi详细介绍
- android 加载动画效果_这效果炸了,网易云音乐“宇宙尘埃”特效
- vue json 导出 excel
- Java虚拟机(JVM)简介
- Anaconda 完全入门指南
- ESP3 + ESP-IDF | 串口1 - 简单的串口回环测试
- (树莓派、Arduino、物联网、智能家居、机器人)传感器、机械装置、电子元件
- 微信公众号开发教程java_微信公众号开发java框架:wx4j(入门篇)
- 小猫咪,Naughty baby
- 双光子成像和近红外二区荧光共聚焦成像/树状大分子CT/MRI双模态成像造影剂/锰螯合物磁共振成像(MRI)
- CHD+CM-1 安装
- Python包pretty_errors
- 漫步在云台山茶园穿越,感受来自李亮先生的美意
- Revit相关问题:符号线,转转问题,生成三维视图
- 中盈Zonewin NX-1900 打印机驱动
- Linux 下 Login 和 Logout 详解
- idea激活码?学生如何白嫖使用idea?
- 【精品干货】100000+文章速成法宝——15种标题撰写技巧