Spring Task定时任务的配置和使用详解
spring中使用定时任务
1、基于xml配置文件使用定时任务
首先配置spring开启定时任务
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd"> <task:annotation-driven /> <!-- 定时器开关--> <bean id="myTask" class="com.spring.task.MyTask"></bean> <task:scheduled-tasks>
<!-- 这里表示的是每隔五秒执行一次 -->
<task:scheduled ref="myTask" method="show" cron="*/5 * * * * ?" />
<task:scheduled ref="myTask" method="print" cron="*/10 * * * * ?"/>
</task:scheduled-tasks> <!-- 自动扫描的包名 -->
<context:component-scan base-package="com.spring.task" /> </beans>
定义自己的任务执行逻辑
package com.spring.task; /**
* 定义任务
*/
public class MyTask { public void show() {
System.out.println("show method 1");
} public void print() {
System.out.println("print method 1");
}
}
2、基于注解使用定时任务
package com.spring.task;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; /**
* 基于注解的定时器
*/
@Component
public class MyTask2 { /**
* 定时计算。每天凌晨 01:00 执行一次
*/
@Scheduled(cron = "0 0 1 * * *")
public void show() {
System.out.println("show method 2");
} /**
* 启动时执行一次,之后每隔2秒执行一次
*/
@Scheduled(fixedRate = 1000*2)
public void print() {
System.out.println("print method 2");
}
}
这样,当项目启动,定时任务就会按照规则按时执行了。
3、Spring Boot中使用定时任务
Spring Boot中使用更加方便。
引入springboot starter包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
在程序入口启动类添加@EnableScheduling,开启定时任务功能
@SpringBootApplication
@EnableScheduling
public class Application { public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
定义定时任务逻辑
@Component
public class MyTask3 { private int count=0; @Scheduled(cron="*/6 * * * * ?")
private void process() {
System.out.println("this is scheduler task runing "+(count++));
}
}
任务执行规则说明
先来看看@Scheduled注解的源码
@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 -1;
String fixedDelayString() default "";
long fixedRate() default -1;
String fixedRateString() default "";
long initialDelay() default -1;
String initialDelayString() default "";
}
可以看出,注解中可以传8种参数:
- cron:指定cron表达式
- zone:默认使用服务器默认时区。可以设置为
java.util.TimeZone中的zoneId - fixedDelay:从上一个任务完成开始到下一个任务开始的间隔,单位毫秒
- fixedDelayString:同上,时间值是
String类型 - fixedRate:从上一个任务开始到下一个任务开始的间隔,单位毫秒
- fixedRateString:同上,时间值是
String类型 - initialDelay:任务首次执行延迟的时间,单位毫秒
- initialDelayString:同上,时间值是
String类型
cron表达式的使用方法
Cron表达式是一个字符串,字符串以5或6个空格隔开,分为6或7个域,每一个域代表一个含义,Cron有如下两种语法格式:
- Seconds Minutes Hours DayofMonth Month DayofWeek Year
- Seconds Minutes Hours DayofMonth Month DayofWeek
每一个域可出现的字符如下:
- Seconds: 可出现", - * /"四个字符,有效范围为0-59的整数
- Minutes: 可出现", - * /"四个字符,有效范围为0-59的整数
- Hours: 可出现", - * /"四个字符,有效范围为0-23的整数
- DayofMonth: 可出现", - * / ? L W C"八个字符,有效范围为0-31的整数
- Month: 可出现", - * /"四个字符,有效范围为1-12的整数或JAN-DEC
- DayofWeek: 可出现", - * / ? L C #"四个字符,有效范围为1-7的整数或SUN-SAT两个范围。1表示星期天,2表示星期一, 依次类推
- Year: 可出现", - * /"四个字符,有效范围为1970-2099年
每一个域都使用数字,但还可以出现如下特殊字符,它们的含义是:
*:表示匹配该域的任意值,假如在Minutes域使用*, 即表示每分钟都会触发事件。?:只能用在DayofMonth和DayofWeek两个域。它也匹配域的任意值,但实际不会。因为DayofMonth和 DayofWeek会相互影响。例如想在每月的20日触发调度,不管20日到底是星期几,则只能使用如下写法: 13 13 15 20 ?, 其中最后一位只能用?,而不能使用,如果使用*表示不管星期几都会触发,实际上并不是这样。-:表示范围,例如在Minutes域使用5-20,表示从5分到20分钟每分钟触发一次。/:表示起始时间开始触发,然后每隔固定时间触发一次,例如在Minutes域使用5/20,则意味着5分钟触发一次,而25,45等分别触发一次。,:表示列出枚举值值。例如:在Minutes域使用5,20,则意味着在5和20分每分钟触发一次。L:表示最后,只能出现在DayofWeek和DayofMonth域,如果在DayofWeek域使用5L,意味着在最后的一个星期四触发。W:表示有效工作日(周一到周五),只能出现在DayofMonth域,系统将在离指定日期的最近的有效工作日触发事件。例如:在 DayofMonth使用5W,如果5日是星期六,则将在最近的工作日:星期五,即4日触发。如果5日是星期天,则在6日(周一)触发;如果5日在星期一 到星期五中的一天,则就在5日触发。另外一点,W的最近寻找不会跨过月份。LW:这两个字符可以连用,表示在某个月最后一个工作日,即最后一个星期五。#:用于确定每个月第几个星期几,只能出现在DayofMonth域。例如在4#2,表示某月的第二个星期三。
看来这个东西还是要经常用才不会忘啊。。。
Spring Task定时任务的配置和使用详解的更多相关文章
- spring task定时器的配置使用
spring task的配置方式有两种:配置文件配置和注解配置. 1.配置文件配置 在applicationContext.xml中增加spring task的命名空间: xmlns:task=&qu ...
- Spring task定时任务执行一段时间后莫名其妙停止的问题
前因: 我写了一个小项目,主要功能是用Spring task定时任务每天定时给用户发送邮件.执行了几个月一直没有问题,前几天,莫名其妙的突然不再发送邮件了. 只好花费一些时间来查看到底是什么原因造成的 ...
- Quartz学习——SSMM(Spring+SpringMVC+Mybatis+Mysql)和Quartz集成详解(转)
通过前面的学习,你可能大致了解了Quartz,本篇博文为你打开学习SSMM+Quartz的旅程!欢迎上车,开始美好的旅程! 本篇是在SSM框架基础上进行的. 参考文章: 1.Quartz学习——Qua ...
- (转)java之Spring(IOC)注解装配Bean详解
java之Spring(IOC)注解装配Bean详解 在这里我们要详细说明一下利用Annotation-注解来装配Bean. 因为如果你学会了注解,你就再也不愿意去手动配置xml文件了,下面就看看 ...
- spring在IoC容器中装配Bean详解
1.Spring配置概述 1.1.概述 Spring容器从xml配置.java注解.spring注解中读取bean配置信息,形成bean定义注册表: 根据bean定义注册表实例化bean: 将bean ...
- CentOS 6.3下Samba服务器的安装与配置方法(图文详解)
这篇文章主要介绍了CentOS 6.3下Samba服务器的安装与配置方法(图文详解),需要的朋友可以参考下 一.简介 Samba是一个能让Linux系统应用Microsoft网络通讯协议的软件, ...
- MYSQL服务器my.cnf配置文档详解
MYSQL服务器my.cnf配置文档详解 硬件:内存16G [client] port = 3306 socket = /data/3306/mysql.sock [mysql] no-auto-re ...
- Eclipse进行C/C++开发——Eclipse+CDT+MinGW的配置与使用详解
http://hi.baidu.com/ltb6w/item/3a51f11926fda60ce75c361d Eclipse进行C/C++开发——Eclipse+CDT+MinGW的配置与使用详解 ...
- webpack安装配置使用教程详解
webpack安装配置使用教程详解 www.111cn.net 更新:2015-09-01 编辑:swteen 来源:转载 本文章来为各位详细的介绍一下关于webpack安装配置使用教程吧,这篇文章对 ...
随机推荐
- springboot热部署JRebel插件
激活参考:https://www.52pojie.cn/thread-906163-1-1.html 最后设置为离线,不然每次启动要重新激活 环境idea是2018.1.1版本 JRebel是最新版2 ...
- 02 Struts2框架----学习笔记2(了解一下,已过时)
1.*号通配符优化struts.xml代码 创建一个UserAction的动作类 package action; import com.opensymphony.xwork2.ActionSuppor ...
- Docker创建镜像 并推拉Harbor
创建镜像 一.根据dockerfile创建镜像 文件详解 1.mkdir dockerfile/lib/centos7base/ 创建目录 2.创建Dockerfile vim Dockerfile ...
- Java基础笔试练习(十一)
1.下面的方法,当输入为2的时候返回值是多少? public static int getValue(int i) { int result = 0; switch (i) { case 1: res ...
- MySQL必知必会1
MySQL必知必会 了解SQL 什么是数据库:数据库(database)保存有阻止的数据的容器,可以把数据库想象成一个文件柜. 什么是表:表(table) 某种特定类型结构的结构化清单,数据库中的 ...
- Nvidia Jetson TX2开发板学习历程(1)- 详细开箱、上电过程
考试周已经结束了,开发板也已经到了.希望借着这个假期能够好好的利用这块开发板学习Linux系统以及Tensorflow的相关知识. 我打算将学习历程通过博客的方式写出来,作为自己的笔记,也可以供以后拿 ...
- PB 计算公式算出结果赋值给另外一列
在数据窗口中添加一个公式列 --在itmchanged事件中写的计算赋值代码 String ls_gs,ls_sqldecimal{2} ls_gsjg if dwo.name='gs1' then ...
- jvm问题排查工具、命令
dump生成:jmp -dump:live,format=b,file=/tmp/some.bin PID.其中,加上live表示只dump存活的对象. 线程栈信息生成:jstack PID > ...
- Springboot 结合百度IORC实现自定义模板图片识别
前言: 首先呢,最近再公司的项目当中遇到这样的一个问题,就是需要识别图片,提取图片当中的关键语句,而且识别的语句当然是人家手写体识别,翻来覆去一想,最终还是决定使用百度的OCR帮助我解决这一项需求 话 ...
- vim操作常用命令总结
这里记录下linux在vim编辑器中的常用命令 vi 的三种模式: 一般模式:以vi打开一个文件时,就是一般模式:可以移动光标,删除字符或删除整行,可以复制.粘贴等操作 编辑模式:在一般模式按下 i ...