记录下spring自带的定时任务用法。
spring中使用定时任务
基于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"); } }
基于注解使用定时任务
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"); } }
这样,当项目启动,定时任务就会按照规则按时执行了。
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种参数:
java.util.timezone
中的zoneidstring
类型string
类型string
类型cron表达式的使用方法
cron表达式是一个字符串,字符串以5或6个空格隔开,分为6或7个域,每一个域代表一个含义,cron有如下两种语法格式:
每一个域可出现的字符如下:
每一个域都使用数字,但还可以出现如下特殊字符,它们的含义是:
*
:表示匹配该域的任意值,假如在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 Cloud Feign统一设置验证token实现方法解析
SpringBoot整合TomCat实现本地图片服务器代码解析
最简单的在IntelliJ IDEA导入一个本地项目教程(图文)
网友评论