Spring 中使用 SpringTask 实现定时任务

2023-05-16

文章目录

    • SpringBoot 中使用
        • 一. 启动类添加注解
        • 二. 编辑定时任务处理类
        • 三. 运行项目
    • SpringMVC 中使用
    • 常用 `cron` 表达式
    • 参考链接

SpringBoot 中使用

一. 启动类添加注解

  • @EnableScheduling开启定时任务
@EnableScheduling
@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

二. 编辑定时任务处理类

  • @Service 加入Spring容器管理
  • @Scheduled() 添加执行时间
  • cron = "*/5 * * * * ? " 设置具体的执行时间(每隔5 秒执行)
@Service
public class TaskService {
    @Scheduled(cron = "*/5 * * * * ? ")
    public void taskDemo() {
        System.out.println("SpringTask 定时任务   " + new Date());
    }
}

三. 运行项目

  • 即可看到控制台的输出
SpringTask 定时任务   Tue Aug 03 14:29:10 CST 2021
SpringTask 定时任务   Tue Aug 03 14:29:15 CST 2021
SpringTask 定时任务   Tue Aug 03 14:29:20 CST 2021
SpringTask 定时任务   Tue Aug 03 14:29:25 CST 2021
SpringTask 定时任务   Tue Aug 03 14:29:30 CST 2021
SpringTask 定时任务   Tue Aug 03 14:29:35 CST 2021
SpringTask 定时任务   Tue Aug 03 14:29:40 CST 2021

SpringMVC 中使用

  • 完整项目代码:https://gitee.com/aoweibrave/task-demo.git
  • 创建mavem 项目时,选择模板:maven-archetype-webapp
  • 引入Spring的依赖
  • 修改web.xml
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
  <display-name>Archetype Created Web Application</display-name>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
</web-app>

  • 修改applicationContext.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 http://www.springframework.org/schema/beans/spring-beans.xsd
	   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
	   http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
       http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd"
       default-autowire="byName" default-lazy-init="false">
    <!--    配置包扫描-->
    <context:component-scan base-package="com.example.demo.service"/>
    <!--    开启 SpringTask 注解-->
    <task:annotation-driven/>
</beans>
  • 编辑定时任务处理类
@Service
public class TaskService {
    @Scheduled(cron ="*/2 * * * * ? ")
    public void check(){
        System.out.println(new Date()+"定时任务");
    }
}
  • 运行项目,即可看到控制台的输出
Tue Aug 03 15:41:16 CST 2021定时任务
Tue Aug 03 15:41:18 CST 2021定时任务
Tue Aug 03 15:41:20 CST 2021定时任务
Tue Aug 03 15:41:22 CST 2021定时任务
Tue Aug 03 15:41:24 CST 2021定时任务
Tue Aug 03 15:41:26 CST 2021定时任务

常用 cron 表达式

CRON表达式         执行时间
"*/5 * * * * ? "    每隔5秒执行一次
"0 */1 * * * ? "   每隔1分钟执行一次
"0 0 12 * * ?"    每天中午十二点触发
"0 0 23 * * ?"    每天23点执行一次
"0 0 1 * * ?"    每天凌晨1点执行一次
"0 0 1 1 * ?"    每月1号凌晨1点执行一次
"0 0 23 L * ?"    每月最后一天23点执行一次
"0 0 1 ? * L"    每周星期天凌晨1点实行一次
"0 26,29,33 * * * ?"26分、29分、33分执行一次
"0 0 0,13,18,21 * * ?"    每天的0点、13点、18点、21点都执行一次
"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触发

参考链接

springboot定时任务:https://www.bilibili.com/video/BV1B4411m7Yu
定时任务利器之Spring Task:https://www.bilibili.com/video/BV1kt41137xv
整理一些corn表达式:https://blog.csdn.net/yang920106/article/details/73733628
Spring定时任务不执行的解决:https://blog.csdn.net/qq_35595521/article/details/54314310

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring 中使用 SpringTask 实现定时任务 的相关文章

  • 【设计模式】生产者消费者模型

    带你轻松理解生产者消费者模型 xff01 生产者消费者模型可以说是同步与互斥最典型的应用场景了 xff01 文末附有模型简单实现的代码 xff0c 若有疑问可私信一起讨论 文章目录 一 xff1a 为什么要使用生产者消费者模型 xff1f
  • matlab的for循环

    a span class token operator 61 span span class token function rand span span class token punctuation span span class tok
  • 论文笔记之 Collaborative Deep Learning for Recommender Systems

    这篇论文是KDD2015的一篇用DL去做RS的论文 想法挺有意思的 看过论文的同学都知道整体的模型可以用下图表示 xff1a 这里只讲讲整体的思路与理解 xff1a 1 xff09 这是一个CF和CBF结合用bayes去做 2 xff09
  • QGC地面站参数调节

    校准 xff1a 1 选择机架 xff1a 一般用DJI Flame Wheel F450机架 xff0c 选择之后点击 应用并重启 xff1b 2 传感器校准 xff1a 无人机会重新连接地面站 xff0c 依次校准 磁罗盘 陀螺仪 xf
  • 【Java】线程超时(设置一段代码执行超时时间)

    引用原文 xff1a https blog csdn net educast article details 51864912 代码 public class ThreadTest public static void main Strin
  • HDFS入门简介

    HDFS是什么 xff1f 易于扩展的分布式文件系统运行在大量普通廉价机器上提供容错机制为大量用户提供性能不错的存取服务 设计目标 xff1a 自动快速检测应对硬件错误流式访问数据 以流的方式访问数据 xff0c 设计用于数据的批量处理 缺
  • Android对于Fragment的使用以及底部导航栏问题

    fragment 一直提示fragment类型不对 该错误是在我们创建的fragment类文件中导错了包 底部导航栏不显示文字 在该处加上app labelVisibilityMode 61 34 labeled 34 就可以全部显示了 原
  • 建立个人网站1:腾讯云域名购买,域名解析以及使用GitHub初步搭建网站

    建立个人网站1 申请域名 xff0c 域名解析以及使用GitHub初步显示 腾讯云的学生优惠申请了一个域名 然后用了GitHub的服务器搭了网站 xff0c github优点是不用备案 xff0c 域名解析把GitHub的个人域名复制过来就
  • SQL语句快查

    SQL语句 0 MYSQL登陆 mysql u root p 1 创建数据库 span class token keyword CREATE span span class token keyword DATABASE span span
  • Numpy 数据类型及转换

    今天写代码遇到数据类型相关的问题 xff0c 记忆不是很清楚 xff0c 所以总结归纳了一下 NumPy 数据类型 名称描述bool 布尔型数据类型 xff08 True 或者 False xff09 int 默认的整数类型 xff08 类
  • 安装phantomjs-prebuilt失败(已解决)

    今天使用 pyecharts snapshot 插件需要安装 phantomjs xff0c 结果报了下面得错误 xff0c 我不快乐了 xff1a 解决方法一 xff1a sudo npm i nrm g 安装nrm nrm 查看使用方法
  • 通信原理 AMI码和HDB3码的编码方式

    AMI 简介 AMI 消息码的 1 交替地变换为 43 1 和 1 xff0c 而 0 保持不变 优点 xff1a 没有直流成分 xff0c 高 xff0c 低频分量少 xff0c 编码电路简单 xff0c 可利用传号极性交替这一规律 观察
  • ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', syste

    好几天没用MySQL xff0c 今天出现了这个ERROR 2013 HY000 Lost connection to MySQL server at 39 reading initial communication packet 39 s
  • Ubuntu 10.04下安装libgtk2.0-dev

    转自 xff1a http chaoyang blog ustc edu cn index php archives 133 去看原文吧 xff0c 有些图粘不过来 好文章啊 学了一些知识 xff0c 谢谢作者 Problem 输入如下命令
  • Linux添加应用图标

    在Linux中 xff0c 有些软件因为采用源码编译或者其他的方式直接来进行安装 xff0c 一般通过bash脚本进行启动 xff0c 但对于有的用户来说 xff0c 更希望在桌面有一个桌面软件图标来方便打开 所以 xff0c 我们接下来将
  • NS2协议分析与仿真

    一 NS2安装 ns2需要的环境较为复杂 xff0c gcc版本不易过高 xff0c 以免无法编译成功 xff0c 推荐使用gcc 4 8 g 43 4 8 span class token comment 安装依赖 span span c
  • 【力扣周赛】第344场周赛

    力扣周赛 第344场周赛 6416 xff1a 找出不同元素数目差数组题目描述解题思路 6417 xff1a 频率跟踪器题目描述解题思路 6418 xff1a 有相同颜色的相邻元素数目题目描述解题思路 6419 xff1a 使二叉树所有路径
  • linux删除桌面图标

    进入 local share applications wine Programs 里面的文件命名一般是 lt 软件名称 gt desktop 删除不要的文件 xff0c 在进入 config menus applications merg
  • 初学shell脚本之-bash: /home/test/hello.sh: Permission denied

    今天学习shell脚本在windows写了个shell脚本 span class token operator span span class token operator span bin span class token operato
  • git push 失败 commit count: 3, latest commit: ****. missing Change-Id in message footer

    git push 失败 commit count 3 latest commit 76f0bdf missing Change Id in message footer 如 xff1a 76f0bdf这次提交没有id 执行 git log查

随机推荐