Spring3定时任务

简介

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

使用 XML 配置

  1. 定义一个 Task 类,添加一个测试方法
public class Task {
    public void cron() {
        System.out.println(new Date() + ", 定时任务执行中 ...");
    }
}
  1. 在 spring.xml 配置文件中添加如下配置
<?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:p="http://www.springframework.org/schema/p"
       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.0.xsd
           http://www.springframework.org/schema/task 
           http://www.springframework.org/schema/task/spring-task.xsd">

    <!-- 定时任务 -->
    <bean id="task" class="com.review.spring.model.Task"/>
    <!-- 创建任务池 -->   
    <task:scheduler id="taskScheduler" pool-size="100" />
    <!-- 定义定时任务 -->   	
    <task:scheduled-tasks scheduler="taskScheduler">
        <!-- 表示周一到周五的每分钟的第五秒执行-->
        <task:scheduled ref="task" method="cron" cron="5 * * * * MON-FRI"/>
    </task:scheduled-tasks>
</beans>
  1. 在测试类中初始化容器,定时任务就会自动执行
public class TestTask {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/spring.xml");
    }
}

使用注解配置

  1. 定义 Task 类,添加如下代码

public class Task {
    (cron="0/2 * * * * *") // 每隔 2 秒运行一次
    public void cron() {
        System.out.println(new Date() + ", 定时任务执行中 ...");
    }
}
  1. 在 spring.xml 配置文件中添加如下配置
<?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:p="http://www.springframework.org/schema/p"
       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-3.0.xsd
           http://www.springframework.org/schema/task 
           http://www.springframework.org/schema/task/spring-task.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="com.review.spring"/>
    <task:annotation-driven/> <!-- 如果是代码式配置, 需要在配置类添加注解  -->
</beans>

cronExpression 的配置说明
cron 表达式是一个字符串,字符串以 5 或 6 个空格隔开,分为 6 或 7 个域,每一个域代表一个含义,cron 有如下两种语法格式:

(1) Seconds Minutes Hours DayofMonth Month DayofWeek Year

(2) Seconds Minutes Hours DayofMonth Month DayofWeek(常用)
在这里插入图片描述
在这里插入图片描述
注意:Spring 中的任务调度以及其 ThreadPoolTaskScheduler 中的 cron 表达式是不支持 L W 字符的;所以最后一天和最近的工作日就无法定义在 cron 表达式中

扩展:如何实现每月最后一天执行任务?
方式一:每个月的最后一天,因为不同的月份最后一天是不一样的,但是好在有个范围:28 – 31;那么我们可以结合 cron 表达式 + 代码实现控制每月的最后一天执行任务;代码如下

(cron = "0 0 0 28-31 * ?")
public void execute() {
    final Calendar c = Calendar.getInstance();
    
    // 判断是不是最后一天
    if (c.get(Calendar.DATE) == c.getActualMaximum(Calendar.DATE)) {
	    // 你的业务逻辑代码
    }
}

方式二:在 cron 表达式中,L 表示最后一个的意思,所以可定义为

(cron = "* * * L * ?")

前提是定义的 cron 表达式支持 L W 指定,例如:Quartz 等框架,如果是 Spring 自带定时器则不支持

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值