定时任务之EnableScheduling注解

传送门

SpringMVC的源码解析(精品)
Spring6的源码解析(精品)
SpringBoot3框架(精品)
MyBatis框架(精品)
MyBatis-Plus
SpringDataJPA
SpringCloudNetflix
SpringCloudAlibaba(精品)
Shiro
SpringSecurity
java的LOG日志框架
Activiti(敬请期待)
JDK8新特性
JDK9新特性
JDK10新特性
JDK11新特性
JDK12新特性
JDK13新特性
JDK14新特性
JDK15新特性
JDK16新特性
JDK17新特性
JDK18新特性
JDK19新特性
JDK20新特性
JDK21新特性
其他技术文章传送门入口

前言

记录一个定时任务注解,很简单的,就可以在SpringBoot框架中执行

package com.hotent.assembly.conf.task;

import com.hotent.assembly.manager.LcCrkglWlxxManager;
import com.hotent.assembly.manager.LcWxjfWlxxManager;
import org.activiti.bpmn.converter.XMLStreamReaderUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

@Configuration
@EnableScheduling
public class TaskHandler {
    protected static final Logger LOGGER = LoggerFactory.getLogger(TaskHandler.class);
    @Autowired
    LcCrkglWlxxManager lcCrkglWlxxManager;

    @Scheduled(cron = "0 0 0 * * ?")// 每天凌晨00:00
//    @Scheduled(cron = "0 * * * * ?")// 每分钟的0秒   本地调试测试用
    public void updateLcCrkglWlxxZlztJob() {
        // 质量有效状态+所检日期取数规则:“物料+批次”查询最近所检申请单【已完结状态】中所检日期,如果大于阈值(180天)为“超期”
        //定时任务,每天凌晨00:00 ,   LC_CRKGL_WLXX.SJRQ +180 <LC_CRKGL_MAIN.SJDAT   如果符合条件,更新LC_CRKGL_WLXX.ZLZT = 0
        LOGGER.error("执行任务:质量有效状态+所检日期取数规则:“物料+批次”查询最近所检申请单【已完结状态】中所检日期,如果大于阈值(180天)为“超期”");
        lcCrkglWlxxManager.updateLcCrkglWlxxZlzt();
    }
}
@Configuration
@EnableScheduling
public class TaskHandler {
    @Scheduled(cron = "0 0 3 1/1 * ?")
    public void userVipJob() {
    }
}

在这里插入图片描述

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Boot通过@EnableScheduling注解来开启定时任务的功能。下面是实现动态定时任务的步骤: 1. 首先,在你的Spring Boot应用的主类上添加@EnableScheduling注解,启用定时任务的支持。例如: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication @EnableScheduling public class YourApplication { public static void main(String[] args) { SpringApplication.run(YourApplication.class, args); } } ``` 2. 接下来,在需要执行定时任务的方法上添加@Scheduled注解,并设置定时任务的执行规则。例如,我们可以使用cron表达式来定义定时任务的执行时间。以下是一个示例: ```java import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component public class MyScheduledTask { @Scheduled(cron = "0 0/5 * * * *") // 每5分钟执行一次 public void doSomething() { // 定时任务要执行的操作 } } ``` 3. 现在,当应用启动后,定时任务会按照定义的规则自动执行。 如果你想在运行时动态地修改定时任务的执行规则,可以借助Spring的ScheduledTaskRegistrar类。可以在应用程序中注入ScheduledTaskRegistrar对象,然后使用其方法来注册、取消和修改定时任务。这样你就可以实现动态的定时任务调度了。 希望这个回答对你有帮助!如果你还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蓝影铁哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值