Spring Task(简略笔记)

介绍

Spring Task是Spring框架提供的任务调度工具,可以按照约定的时间自动执行某个代码逻辑。

Corn表达式

corn表达式其实就是一个字符串,通过corn表达式可以定有任务触发的时间

构成规则:分为6或7个域,由空格分隔开,每个域代表一个含义

每个域的含义分别为:秒,分钟,小时,日,月,周,年(可选)

表达式网站

在线Cron表达式生成器

" https://cron.qqe2.com/ "

使用步骤

启动类加上:

复制代码
@EnableScheduling  //开启任务调度
java 复制代码
@SpringBootApplication
@EnableTransactionManagement //开启注解方式的事务管理
@Slf4j
@EnableCaching //开启缓存注解功能
@EnableScheduling  //开启任务调度
public class SkyApplication {
    public static void main(String[] args) {
        SpringApplication.run(SkyApplication.class, args);
        log.info("server started");
    }
}
java 复制代码
package com.sky.task;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author Mtz
 * @version 1.0
 * @2023/9/2010:41
 * @function
 * @comment
 */
@Component
@Slf4j
public class MyTask {

    /*
    定时任务
     */
    @Scheduled(cron = "0/5 * * * * ? ")
    public void executeTask() {
        log.info("定时任务开始执行:{}", new Date());
    }

}
相关推荐
落魄实习生37 分钟前
Agent Scope Java 2.x 系列【10】Middleware
java·开发语言·ai
Ztt6666666661 小时前
壶口敞开以后,器身反而更显从容
笔记·其他·百度·微信公众平台·微信开放平台
ZealSinger1 小时前
Spring7 RestTemplate弃用迁移
java·spring·后端开发·restclient
朝朝辞暮i2 小时前
C++ 第 13 课:值传递 —— 为什么函数里改了,外面却没变?
java·c++·算法
弹简特2 小时前
【Java项目-企悦抽】16-抽奖模块01-获取活动完整信息接口实现
java·开发语言·状态模式·springboot
萧瑟余晖2 小时前
Spring 资源与环境配置体系详解
spring
泡茶喝茶写代码2 小时前
A股量化数据工程:从 REST 接口到策略信号(第 1 篇):指数列表与实时行情接入
java·python·股票数据api·股票数据·股票数据api接口·股票api数据接口·股票量化数据接口
Sarvartha3 小时前
接口基础知识
java·开发语言
知产xiao_xin3 小时前
了解商号权
经验分享·笔记·知识产权
不才不才不不才3 小时前
Spring 源码系列(29): 20 道高频面试题源码级解析合集
java·后端·spring