第三十九章 SpringBoot计划任务

来源:互联网 发布:淘宝买家留言不显示 编辑:程序博客网 时间:2024/04/30 00:00

SpringBoot的计划任务沿用Spring4.x的计划任务,参考第十章 Spring计划任务

一、配置类添加@EnableScheduling注解,开启对计划任务的支持

@SpringBootApplication@EnableSchedulingpublic class Application {    public static void main(String[] args) {        SpringApplication.run(Application.class, args);   }}

二、编写任务执行类

public class CronJobTest {    @Scheduled(cron = "0/1 * * * * ?")    public void nowTime() {        System.out.println("当前时间:" + DateFormatUtils.format(newDate(), "yyyy-MM-dd HH:mm:ss"));    }}

将任务执行的配置转移至配置文件>
在application.properties中添加

job.nowTime.cron=0/1 * * * * *

任务执行类CronJobTest修改为

@Scheduled(cron = "${job.nowTime.cron}")public void nowTime() {    System.out.println("当前时间:" + DateFormatUtils.format(newDate(), "yyyy-MM-dd HH:mm:ss"));}
0 0
原创粉丝点击