SpringTask

1.定时任务概述

在项目中开发定时任务应该一种比较常见的需求, 在Java中开发定时任务主要有三种解决方案:
一是使用JDK自带的Timer,二是使用第3三方组件Quartz,三是使用Spring Task.
Timer是JDK自带的定时任务工具,其简单易用,但是对于复杂的定时规则无法满足,在实际项目开发中也很少使用到。
Quartz 功能强大,但是使用起来相对笨重。
而Spring Task则具备前两者的优点(功能强大且简单易用),使用起来很简单,除Spring相关的包外不需要额外的包,而且支持注解和配置文件两种形式。

2.使用XML实现

Task包下的TaskJob类

@Component
public class TaskJob {
    private SimpleDateFormat df=new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");

    //定义定时任务方法
    public void job1(){
        System.out.println("job1");
        System.out.println(df.format(new Date()));
    }

}

spring.xml

<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="org.example"/>

    <!--    配置定时任务规则
                ref:任务类名
                method:方法名
    -->
    <task:scheduled-tasks>
    <!--  定时任务1 2秒发送一次-->
        <task:scheduled ref="taskJob" method="job1" cron="0/2 * * * * ?"/>
    </task:scheduled-tasks>


</beans>

测试类

 public static void main(String[] args) {
        //上下文环境
        ApplicationContext ac=new ClassPathXmlApplicationContext("spring.xml");
        TaskJob taskJob= (TaskJob) ac.getBean("taskJob");
    }

3.使用注解实现

Taskjob2类

@Component
public class TaskJob2 {
    private SimpleDateFormat df=new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");


    //定义定时任务方法
    @Scheduled(cron = "0/5 * * * * ?")
    public void job1(){
        System.out.println("job2");
        System.out.println(df.format(new Date()));
    }
}

spring2.xml

<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="org.example"/>

    <task:annotation-driven></task:annotation-driven>

</beans>

4.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)




可以直接使用在线cron生成器

posted @   lwx_R  阅读(47)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示