首页 > 其他分享 >Spring Task定时任务

Spring Task定时任务

时间:2022-09-21 22:11:05浏览次数:95  
标签:10 Task 15 14 触发 Spring import 定时 public

1. 概述

在项目开发中定时任务有三种解决方案:

  1. JDK自带的Timer
  2. 第三方组件Quartz
  3. 使用Spring Task

Timer是JDK自带的定时任务工具,其简单易用,但对复杂的定时规则无法满足,在实际项目开发中很少用。Quartz功能强大,但是使用起来相对笨重。Spring Task则具备前两者的优点,使用简单,出Spring包外无序额外的包,而且支持注解和配置文件两种形式。

2. 使用

添加依赖

<!-- spring -->
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-context</artifactId>
	<version>5.3.14</version>
</dependency>

2.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: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
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/task
        http://www.springframework.org/schema/task/spring-task.xsd">
    <!--    开启自动化扫描 -->
    <context:component-scan base-package="com.xxx"/>
    <!--  配置定时任务驱动。开启这个配置,spring才能识别@Scheduled注解  -->
    <task:annotation-driven/>
</beans>

业务类

package com.xxx.task;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class TaskJob2 {
    // 每隔3s执行一次
    @Scheduled(cron = "0/3 * * * * ?")
    public void job1() {
        System.out.println("任务2:" + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss")
                .format(new Date()));
    }
    // 每隔1s执行一次
    @Scheduled(cron = "0/1 * * * * ?")
    public void job2() {
        System.out.println("任务3:" + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss")
                .format(new Date()));
    }
}

测试类

public class Test {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
        TaskJob2 taskJob2 = ac.getBean("taskJob2", TaskJob2.class);
    }
}

2.2 基于xml的使用

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
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/task
        http://www.springframework.org/schema/task/spring-task.xsd">
    <!--    开启自动化扫描 -->
    <context:component-scan base-package="com.xxx"/>
    <!-- 配置定时任务
      ref: 指代任务类, TaskJob类, method是指TaskJob中的方法
      -->
    <task:scheduled-tasks>
        <!-- 这里可以指定多个定时任务 -->
        <!-- 定时任务1 (每隔2s执行一次) -->
        <task:scheduled ref="taskJob" method="job1" cron="0/2 * * * * ?"/>
    </task:scheduled-tasks>
</beans>

定时任务

package com.xxx.task;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class TaskJob {
    // 定义定时任务的方法
    public void job1() {
        System.out.println("任务1: " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
    }
}

测试类

package com.xxx;
import com.xxx.task.TaskJob;
import com.xxx.task.TaskJob2;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
        TaskJob taskJob = ac.getBean("taskJob", TaskJob.class);
    }
}

3. cron表达式

关于cronExpression表达式有至少6个(或7个)由空格隔开。从左到右,这些元素的定义如下:

0-59  0-59    0-23   1-31   1-12   1-7或JAN-DEC    1970-2099 
1     2       3      4      5      6               7
秒    分钟    小时    号     月     星期几           年份

示例:

0 0 10,14,16 * * ?
星期几不确定  每月  每天  10点14点16点  0分  0秒

0  0,15,30,45  *  1-10  *  ?
每月  1-10号  每个小时  15,30,45分  0s

30  0  0  1  1  ?  2012
2012年  星期不确定  1月  1号   0时  0分  30s

image

示例:

"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 15 10 15 * ?" 每月 15 号的 10:15 触发
"0 15 10 L * ?" 每月的最后一天的 10:15 触发
"0 15 10 ? * 6L" 每月最后一个周五的 10:15

cron表达式在线生成

标签:10,Task,15,14,触发,Spring,import,定时,public
From: https://www.cnblogs.com/liqiju/p/16717342.html

相关文章