使用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 ...
随机推荐
- .net 项目生成时自动更新版本号
https://www.codeproject.com/articles/31236/how-to-update-assembly-version-number-automaticall Exampl ...
- pip 与pip3
pip 3用于python3 版本 pip2 用于python2版本 pip好像可以通用 "pip2" is for Python2, "pip3″ is for Pyt ...
- Ajax方式上传文件
用到两个对象 第一个对象:FormData 第二个对象:XMLHttpRequest 目前新版的Firefox 与 Chrome 等支持HTML5的浏览器完美的支持这两个对象,但IE9尚未支持 For ...
- JS中的动态表格
写法一:(有点啰嗦) //--------------XML DOM--------------------------------------function addTR(){ //1.取三个框的值 ...
- html5中画布和SVG的比较
SVG是基于XML的图形语言,在DOM解析中其每个元素都是可以用的,这样就可以为SCG元素附加JavaScript事件处理器,实现更加丰富的效果. 在SVG中,每个被绘制的图形均被视为对象,如果SVG ...
- Vue.js简单实践
直接上代码,一个简单的新闻列表页面(.cshtml): @section CssSection{ <style> [v-cloak] { display: none; } </sty ...
- eclipse启动tomcat, http://localhost:8080无法访问
原地址 症状: tomcat在eclipse里面能正常启动,而在浏览器中访问http://localhost:8080/不能访问,且报404错误.同时其他项目页面也不能访问. 关闭eclipse里面的 ...
- eafier 簡單易用 HTML、CSS 網頁編輯器(可自動插入 Tag 標籤)
很久很久以前,一般人要做網頁的話大概都會用 FrontPage 之類的工具,更進階一點的會用 Dreamweaver 等軟體.雖然上述軟體提供很方便的「所見即所得」的編輯預覽模式,但寫出來的網頁程式碼 ...
- Create a new Windows service on windows server2012
netsh http add iplisten ipaddress=0.0.0.0:15901 sc.exe create "FPPService" binPath= " ...
- 如何将已部署在ASM的资源迁移到ARM中
使用过Azure的读者都知道,Azure向客户提供了两个管理portal,一个是ASM,一个是ARM,虽然Azure官方没有宣布说淘汰ASM,两个portal可能会在很长的一段时间共存,但是考虑到AR ...