Springboot学习笔记(二)-定时任务

springboot中要使用定时任务需要在配置类或启动类上标注注解@EnableScheduling,并在定时执行的无参方法上标注注解@Scheduled,程序启动后会根据@Scheduled所提供的信息定时执行。

Scheduled参数

参数名 含义
cron = "* * * * * ?" 每秒执行
zone 时区,默认为本地时区TimeZone.getDefault()
fixedDelay = 1000 上次任务执行完成后1秒开始
fixedDelayString = "1000" 等同fixedDelay = 1000
fixedRate = 1000 每秒执行
fixedRateString = "1000" 等同fixedRate = 1000
initialDelay = 1000 初始延时1秒执行
initialDelayString = "1000" 等同initialDelay = 1000

cron表达式

不用记,网上有在线cron生成器

**String

公司规定的代码规范中不允许使用魔法数字,可以用这些参数规避。

代码

@Component
public class ScheduledHelloTask {
    private static final Logger LOGGER = LoggerFactory.getLogger(ScheduledHelloTask.class);

    private int getSecond() {
        return Calendar.getInstance().get(Calendar.SECOND);
    }

    // 每秒执行
    @Scheduled(cron = "* * * * * ?", zone = "Asia/Shanghai")
    public void sayHello() {
        LOGGER.info("Hello World!");
    }

    // 任务执行完成后延时1秒开始
    @Scheduled(fixedDelay = 1000)
    public void sayHello1() throws InterruptedException {
        LOGGER.info(getSecond() + "春暖花开~");
        Thread.sleep(1000);
    }

    // 每秒执行,效果等同{cron = "* * * * * ?"}
    @Scheduled(initialDelay = 2000, fixedRate = 1000)
    public void sayHello2() throws InterruptedException {
        LOGGER.info(getSecond() + "你好~");
    }
}

关闭

有时候我们在得到自己需要的结果后想关闭定时任务,比如通过前台发送链接来开启上面的打印Hello World!任务,希望它执行10次后关闭。
此时就不能在类ScheduledHelloTask上添加@Component了, 因为我们需要动态注册bean来实现。改造如下:

@EnableScheduling
public class ScheduledHelloTask {
    private static final Logger LOGGER = LoggerFactory.getLogger(ScheduledHelloTask.class);
    private AtomicInteger atomicInteger = new AtomicInteger();

    public AtomicInteger getAtomicInteger() {
        return atomicInteger;
    }

    public void setAtomicInteger(AtomicInteger atomicInteger) {
        this.atomicInteger = atomicInteger;
    }

    // 每秒执行
    @Scheduled(cron = "* * * * * ?", zone = "Asia/Shanghai")
    public void sayHello() {
        int count = atomicInteger.incrementAndGet();
        LOGGER.info("第" + count + "次:Hello World!");
    }
}

添加ScheduleController,代码如下:

@RestController
public class ScheduleController {
    private static final String BEAN_NAME = "scheduledHelloTask";

    @GetMapping
    public String sayHi() throws InterruptedException {
        AnnotationConfigApplicationContext applicationContext =
                new AnnotationConfigApplicationContext();
        if (!applicationContext.containsBean(BEAN_NAME)) {
            applicationContext.register(ScheduledHelloTask.class);
        }
        applicationContext.refresh();
        while (applicationContext.containsBean(BEAN_NAME)) {
            if (applicationContext.getBean(ScheduledHelloTask.class).getAtomicInteger().get() == 10) {
                applicationContext.removeBeanDefinition(BEAN_NAME);
            }
        }
        return "success";
    }
}

两次访问localhost:8080,结果如下:

预期效果已实现!

posted @   舒山  阅读(1957)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示