Java使用定时任务详解

定时任务

SpringBoot定时任务

默认单线程

1.开启注解

在Spring启动类上加上@EnableScheduling

2.设置执行时间:

  • 使用fixRate:

    在方法上加上@Scheduled(fixRate=30* 60 *1000)

    fixRate=(小时* 分钟* 秒* 1000毫秒*)--只能实现指定间隔

  • 使用Cron表达式:

    在方法上加上@Scheduled(cron="0 0 9-22/4 * *")

    @Scheduled(cron="秒 分 时 日 月 星期 年(可选)")

    Cron表达式讲解:

    在线生成cron表达式:https://cron.qqe2.com/

    分隔符:

    符号 作用 例子
    列出所有值 如果在分钟中使用 5,8表示 分钟为5和8时触发
    - 范围 在分钟中使用5-8表示分钟从5到8每分钟都会触发
    * 该域的任意值 在分钟中使用*,表示对每分钟不做限制
    / 起始时间时触发,然后每隔固定时间触发一次 在分钟中使用5/8,表示五分钟时触发一次,之后每隔8分钟触发一次

    专有符号: --tips:除?外,Spring定时任务不支持其他转有符号

符号 作用 例子
只能用在月和星期互斥时
L 表示最后 ,只能出现在星期和月时 在周(周从星期日开始)中使用5L,表示最后一个星期四
W 表示周一到周五(工作日)只能出现在月中,系统自动离最近的工作日开始时触发
LW L和W连用,表示某月最后一个工作日
# 用于确定的每个月的第几个星期几只能出现在周中 设定3#2表示某月的第二个星期二执行
C 只能用在月和周中,需要关联日历 在月中使用5C 表示每个月的五号执行

使用异步多线程

  1. 开启异步注解

Spring启动类上加上@EnableScheduling的同时加上@EnableAsync

  1. 设置异步执行

在方法上加上@Scheduled(cron="0 0 9-22/4 * *")的同时加上@Async


Spring定时任务XML配置(注解形式几乎同上)

使用springTask: ----springTask为spring自带的

  1. 引用spring-context的依赖(使用springboot项目基本可省)

  2. 添加配置文件spring.xml,开启注解扫描

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc" 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.1.xsd
	http://www.springframework.org/schema/tx
	http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
	http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.1.xsd
    http://www.springframework.org/schema/mvc
     http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
     http://www.springframework.org/schema/task
	http://www.springframework.org/schema/task/spring-task-3.1.xsd">

<context:component-scan base-package="com.code.xxx" />

</beans>
  1. 定义定时任务方法添加@Component--交给spring容器管理
public class Todo{
    public void task1(){
    System.out.println("task1开始")}
}
  1. 在spring.xml中配置:
<task:scheduled-tasks>
   <!-- 每两秒执行一次-->
   <task:scheduler ref="todo" method="task1" cron="0/2 * * * * ?"/>
</task:scheduled-tasks>
  1. 测试定时任务:

5.1获取springContext

   ApplicationContext context=new ClassPathXmlApplication("spring.xml")

5.2. 获取指定的Bean对象

    Todo todo1=(Todo)context.getBean("todo")
  1. 重要:在xml中开启定时任务驱动
<task:annotation-driven>
posted @   ProsperousEnding  阅读(880)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
  1. 1 原来你也在这里 周笔畅
  2. 2 世间美好与你环环相扣 柏松
  3. 3 起风了 吴青峰
  4. 4 极恶都市 夏日入侵企划
  5. 5 所念皆星河 CMJ
  6. 6 所念皆星河-歌词版 房东的猫
  7. 7 卡农-钢琴版 dylanf
  8. 8 The truth that you leave Pianoboy高至豪
  9. 9 虹之间 米叔啊
虹之间 - 米叔啊
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.

编曲:高佳

制作人:陈美威

和声编写:金贵晟

和声:金贵晟

配唱:陈美威

配唱协力:徐威

Vocal录音师:徐威

Vocal录音室:飞行者录音棚

和声录音师:徐威

和声录音室:飞行者录音棚

混音:陈宇轩/Jovi Chen

混音录音室:飞行者录音棚

OP:SAMP (Beijing) Co., Ltd索雅音乐版权代理(北京)有限公司

SP:SAMP (Beijing) Co., Ltd索雅音乐版权代理(北京)有限公司

想借天使的翅膀

抓住云端的彩虹

总在将要触碰时消散

错觉的地久天长

其实是一无所有

童话说雨后会有一道彩虹

却不曾说过它也会转瞬成空

想要把绚烂紧紧握在手中

忽然发现你已不见

站在无尽红尘中

仰望曾有你的苍穹

得到以后转眼又落空

究竟什么是永恒

都无法拥有完整

Oh my love

我看见希望闪耀在虹之间

光芒凝结于你我的那片天

不再追问你为何不能停留

微笑看见爱的浮现

雨后的天空重又出现彩虹

天使的恩惠亲吻着世间万众

不再追问你为何不能停留

放下了执念微笑现在

点击右上角即可分享
微信分享提示