Spring Task 定时任务、使用 XML 配置实现定时任务、使用注解配置实现定时任务

一、使用 XML 配置实现定时任务

添加spring坐标依赖。

1.1 定义定时任务方法

新建类,添加⾃动注⼊的注解,定义定义任务的⽅法

/**
 * 任意类@Component
 */
@Component
public class TaskJob {
    public void job01(){
        System.out.println("任务 1:" + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
    }

    public void job02(){
        System.out.println("任务 2:" + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
    }
}

1.2 添加配置⽂件 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:context="http://www.springframework.org/schema/context"
       xmlns:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://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.xsd">

    <!-- Spring扫描注解的配置 -->
    <context:component-scan base-package="com.shsxt" />

    <!--
        配置定时规则
            ref:指定的类,即任务类
            method:指定的即需要运⾏的⽅法
            cron:cronExpression表达式
    -->
    <task:scheduled-tasks>
        <!-- 每个两秒执执行一次任务 -->
        <task:scheduled ref="taskJob" method="job01" cron="0/2 * * * * ?"/>
        <!-- 每隔五秒执行一次任务 -->
        <task:scheduled ref="taskJob" method="job02" cron="0/5 * * * * ?"/>
        <!-- 多个定时任务 在这里配置 -->
    </task:scheduled-tasks>
</beans>

1.3 测试定时任务

public class Test01 {
    public static void main(String[] args) {
        System.out.println("定义任务测试...");
        // 获取Spring上下文环境
        ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
        // 获取指定Bean对象
        TaskJob taskJob = (TaskJob) ac.getBean("taskJob");
    }
}

二、使用注解配置实现定时任务

2.1 定义定时任务方法

/**
 * 任意类@Component
 */
@Component
public class TaskJob02 {

    @Scheduled(cron = "0/2 * * * * ?")
    public void job01(){
        System.out.println("任务 1:" + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
    }

    @Scheduled(cron = "0/5 * * * * ?")
    public void job02(){
        System.out.println("任务 2:" + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
    }
}

2.2 添加配置⽂件 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:context="http://www.springframework.org/schema/context"
       xmlns:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://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.xsd">

    <!-- Spring扫描注解的配置 -->
    <context:component-scan base-package="com.shsxt" />

    <!-- 配置定时任务驱动。开启这个配置,spring才能识别@Scheduled注解 -->
    <task:annotation-driven />

</beans>

2.3 测试定时任务

public class Test02 {
    public static void main(String[] args) {
        System.out.println("定义任务测试...");
        // 获取Spring上下文环境
        ApplicationContext ac = new ClassPathXmlApplicationContext("spring02.xml");
        // 获取指定Bean对象
        TaskJob02 taskJob02 = (TaskJob02) ac.getBean("taskJob02");
    }
}

三、Cron 表达式简介

关于 cronExpression 表达式有⾄少 6 个(也可能是 7 个)由空格分隔的时间元素。从左⾄右,这些
元素的定义如下:
1.秒(0–59)
2.分钟(0–59)
3.⼩时(0–23)
4.⽉份中的⽇期(1–31)
5.⽉份(1–12 或 JAN–DEC)
6.星期中的⽇期(1–7 或 SUN–SAT)
7.年份(1970–2099)

0 0 10,14,16 * * ?
每天上午 10,下午 2 点和下午 40 0,15,30,45 * 1-10 * ?
每⽉前 10 天每隔 15 分钟

30 0 0 1 1 ? 2012201211 ⽇午夜过 30 秒时

⼀些例⼦:

"0 0 12 * * ?" 每天中午⼗⼆点触发

"0 15 10 ? * *" 每天早上 1015 触发

"0 15 10 * * ?" 每天早上 1015 触发

"0 15 10 * * ? *" 每天早上 1015 触发

"0 15 10 * * ? 2005" 2005 年的每天早上 1015 触发

"0 * 14 * * ?" 每天从下午 2 点开始到 259 分每分钟⼀次触发

"0 0/5 14 * * ?" 每天从下午 2 点开始到 255 分结束每 5 分钟⼀次触发

"0 0/5 14,18 * * ?" 每天的下午 2 点⾄ 2556 点⾄ 655 分两个时间段内每 5分钟⼀次触发

"0 0-5 14 * * ?" 每天 14:0014:05 每分钟⼀次触发

"0 10,44 14 ? 3 WED" 三⽉的每周三的 14101444 触发

"0 15 10 ? * MON-FRI" 每个周⼀、周⼆、周三、周四、周五的 1015 触 发

"0 15 10 15 * ?" 每⽉ 15 号的 1015 触发

"0 15 10 L * ?" 每⽉的最后⼀天的 1015 触发

"0 15 10 ? * 6L" 每⽉最后⼀个周五的 1015``
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值