Spring 定时任务scheduled-tasks详解

16 篇文章 0 订阅

1. Spring 定时任务scheduled-tasks简单介绍

Spring内部有一个task是Spring自带的一个设定时间自动任务调度,提供了两种方式进行配置,一种是注解的方式,而另外一种就是XML配置方式了。注解方式比较简洁,XML配置方式相对而言有些繁琐,但是应用场景的不同,两者又各有优点,所以具体使用还是根据需求来划分。因为任务调度这样的需求,通常改动都是比较多的,如果用注解的方式改动就变得麻烦了,必须去重新编译。所以更多的时候我选择用XML配置的方式。

2. Spring定时任务scheduled-tasks配置

(1)XML配置方式

第一步:编写作业类

即普通的pojo,如下:

复制代码
import org.springframework.stereotype.Service;  
@Service  
public class TaskJob {  
      
    public void job1() {  
        System.out.println(“任务进行中。。。”);  
    }  
} 
复制代码

第二步:添加spring-task配置文件,相关头信息如下:

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<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-4.0.xsd
                         http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd">
       	<task:scheduler id="taskScheduler" pool-size="100" />
<task:scheduled-tasks scheduler="taskScheduler">
        <task:scheduled ref="taskJob" method="job1" cron="0 0 5 * * ?"/>
	<!-- task:scheduled ref="voiceFileClearJob" method="execute" initial-delay="5000" fixed-delay="3600000"/> 
	<task:scheduled ref="versionListenJob" method="execute" initial-delay="5000" fixed-delay="5000"/>  
	<task:scheduled ref="statJob" method="statLgj" cron="0 59 23 * * ?"/>
	<task:scheduled ref="statJob" method="statBadNameAndQQ" cron="23 28 20 * * ?"/> -->
    </task:scheduled-tasks> 

</beans>
复制代码

说明:ref参数指定任务类,method指定需要运行的方法,cron及cronExpression表达式见结尾。

ref是工作类

method是工作类中要执行的方法

initial-delay是任务第一次被调用前的延时,单位毫秒

fixed-delay是上一个调用完成后再次调用的延时

fixed-rate是上一个调用开始后再次调用的延时(不用等待上一次调用完成)

cron是表达式,表示在什么时候进行任务调度


第三步:在spring配置文件中引入spring-task配置文件

。。。。。
<!-- 引入Spring的任务配置文件。 -->
    <import resource="xxx.xml" />
。。。。。

这样配置就完成了,可以进行测试验证了。

(2)注解方式

首先看下源文件中@Scheduled注解的定义

复制代码
@Target({java.lang.annotation.ElementType.METHOD, java.lang.annotation.ElementType.ANNOTATION_TYPE})  
@Retention(RetentionPolicy.RUNTIME)  
@Documented  
public @interface Scheduled  
{  
  public abstract String cron();  
  
  public abstract long fixedDelay();  
  
  public abstract long fixedRate();  
} 
复制代码

可以看出该注解有三个方法或者叫参数,分别表示的意思是:

cron:指定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.即从上一个任务开始到下一个任务开始的间隔,单位是毫秒。

 

下面来说明一下具体配置步骤:

第一步:编写pojo

复制代码
import org.springframework.scheduling.annotation.Scheduled;    
import org.springframework.stereotype.Component;  
  
@Component(“taskJob”)  
public class TaskJob {  
    @Scheduled(cron = "0 0 3 * * ?")  
    public void job1() {  
        System.out.println(“任务进行中。。。”);  
    }  
} 
复制代码

第二步:在spring配置文件开启task:

<task:annotation-driven/>

这样 就实现了注解方式的配置

3. Spring定时任务scheduled-tasks并行执行

Spring scheduled-tasks默认是串行执行,时常发生task任务太多,而导致执行任务排队等待,此时就需要配置并行执行

只要在配置文件xml中加上如下配置即可:

<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"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">

    <!-- Enables the Spring Task @Scheduled programming model -->
    <task:executor id="executor" pool-size="5" />
    <task:scheduler id="scheduler" pool-size="10" />
    <task:annotation-driven executor="executor" scheduler="scheduler" />

</beans>

4. cron及cronExpression表达式

一个cron表达式有至少6个(也可能7个)有空格分隔的时间元素

按顺序依次为 
秒(0~59) 
分钟(0~59)
小时(0~23)
天(月)(0~31,但是你需要考虑你月的天数)
月(0~11)
天(星期)(1~7 1=SUN 或 SUN,MON,TUE,WED,THU,FRI,SAT)
年份(1970-2099)

- 区间

* 通配符

? 你不想设置那个字段

其中每个元素可以是一个值(如6),一个连续区间(9-12),一个间隔时间(8-18/4)(/表示每隔4小时),一个列表(1,3,5),通配符。由于"月份中的日期"和"星期中的日期"这两个元素互斥的,必须要对其中一个设置?.

字段 允许值 允许的特殊字符

秒 
 
0-59 
 
, - * / 

分 
 
0-59 
 
, - * / 

小时 
 
0-23 
 
, - * / 

日期 
 
1-31 
 
, - * ? / L W C 

月份 
 
1-12 或者 JAN-DEC 
 
, - * / 

星期 
 
1-7 或者 SUN-SAT 
 
, - * ? / L C # 

年(可选) 
 
留空, 1970-2099 
 
, - * / 
CRON表达式    含义 
"0 0 12 * * ?"    每天中午十二点触发 
"0 15 10 ? * *"    每天早上10:15触发 
"0 15 10 * * ?"    每天早上10:15触发 
"0 15 10 * * ? *"    每天早上10:15触发 
"0 15 10 * * ? 2005"    2005年的每天早上10:15触发 
"0 * 14 * * ?"    每天从下午2点开始到2点59分每分钟一次触发 
"0 0/5 14 * * ?"    每天从下午2点开始到2:55分结束每5分钟一次触发 
"0 0/5 14,18 * * ?"    每天的下午2点至2:55和6点至6点55分两个时间段内每5分钟一次触发 
"0 0-5 14 * * ?"    每天14:00至14:05每分钟一次触发 
"0 10,44 14 ? 3 WED"    三月的每周三的14:10和14:44触发 
"0 15 10 ? * MON-FRI"    每个周一、周二、周三、周四、周五的10:15触发
0 0 10,14,16 * * ? 每天上午10点,下午2点,4点
0 0/30 9-17 * * ?   朝九晚五工作时间内每半小时
0 0 12 ? * WED 表示每个星期三中午12点 
"0 0 12 * * ?" 每天中午12点触发 
"0 15 10 ? * *" 每天上午10:15触发 
"0 15 10 * * ?" 每天上午10:15触发 
"0 15 10 * * ? *" 每天上午10:15触发 
"0 15 10 * * ? 2005" 2005年的每天上午10:15触发 
"0 * 14 * * ?" 在每天下午2点到下午2:59期间的每1分钟触发 
"0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发 
"0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发 
"0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发 
"0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44触发 
"0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发 
"0 15 10 15 * ?" 每月15日上午10:15触发 
"0 15 10 L * ?" 每月最后一日的上午10:15触发 
"0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发 
"0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发 

"0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发

一些子表达式能包含一些范围或列表
例如:子表达式(天(星期 )可以为 “MON-FRI”,“MON,WED,FRI”,“MON-WED,SAT”
“*”字符代表所有可能的值
因此,“*”在子表达式(月 )里表示每个月的含义,“*”在子表达式(天(星期) )表示星期的每一天
“/”字符用来指定数值的增量


例如:在子表达式(分钟里的“0/15”表示从第0分钟开始,每15分钟
在子表达式(分钟)里的“3/20”表示从第3分钟开始,每20分钟(它和“3,23,43”)的含义一样
“?”字符仅被用于天(月和天(星期两个子表达式,表示不指定值
当2个子表达式其中之一被指定了值以后,为了避免冲突,需要将另一个子表达式的值设为“?”
“L” 字符仅被用于天(月)和天(星期)两个子表达式,它是单词“last”的缩写
但是它在两个子表达式里的含义是不同的。
在天(月子表达式中,“L”表示一个月的最后一天
在天(星期自表达式中,“L”表示一个星期的最后一天,也就是SAT
如果在“L”前有具体的内容,它就具有其他的含义了
例如:“6L”表示这个月的倒数第6天,“FRIL”表示这个月的最一个星期五
注意:在使用“L”参数时,不要指定列表或范围,因为这会导致问题








  • 5
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Scheduled定时任务是一种在Spring Boot中创建定时任务的方式。目前主要有三种创建方式: 1. 基于注解(@Scheduled)的静态任务:通过在方法上添加@Scheduled注解来指定任务的执行时间。 2. 基于接口(SchedulingConfigurer)的动态任务:通过实现SchedulingConfigurer接口,可以根据数据库的内容动态调度任务。 3. 基于注解的多线程定时任务:通过使用@Scheduled注解和多线程来实现定时任务的并发执行。 在使用Spring Scheduled定时任务时,需要在启动类上添加@EnableScheduling注解来开启定时任务功能。然后可以在方法上使用@Scheduled注解来指定任务的执行时间,或者实现SchedulingConfigurer接口来添加定时任务。同时,可以配置定时任务的多线程非阻塞运行,以提高任务的并发性能。 以上是关于Spring Scheduled定时任务的简要介绍和使用方式。如果需要更详细的信息,可以参考引用\[1\]和引用\[2\]中的内容。 #### 引用[.reference_title] - *1* [SpringBoot之Scheduled定时任务详解](https://blog.csdn.net/weixin_41003771/article/details/102655202)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [spring schedule定时任务详解](https://blog.csdn.net/qq_34480904/article/details/122410711)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值