spring boot 集成 quartz 定时任务

 spring boot: @EnableScheduling开启计划任务支持,@Scheduled计划任务声明

1.pom.xml 引入依赖

<dependency>
     <groupId>org.quartz-scheduler</groupId>
      <artifactId>quartz</artifactId>
      <version>2.1.7</version>
 </dependency>

2.在启动类上添加注解 @EnableScheduling

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

3.测试类

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;


/***
 *
 * Quartz设置项目全局的定时任务
 * @Component注解的意义 泛指组件
 * @author
 *
 */
@Component
public class QuartzTask {
    
      SimpleDateFormat sdf=new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");

    @Scheduled(cron = "0 0/1 * * * ?") // 每分钟执行一次
    public void work() throws Exception {
        System.out.println("执行调度任务:" + sdf.format(new Date()));
    }

    @Scheduled(fixedRate = 5000) // 每5秒执行一次
    public void play() throws Exception {
        System.out.println("每5秒执行一次执行Quartz定时器任务:" + sdf.format(new Date()));
    }

    @Scheduled(cron = "0/2 * * * * ?") // 每2秒执行一次
    public void doSomething() throws Exception {
        System.out.println("每2秒执行一个的定时任务:" + sdf.format(new Date()));
    }

    @Scheduled(cron = "0 15 14 ? * *") // 每天14:15:00执行
    public void goWork() throws Exception {
        System.out.println("每天14:15:00执行一次的定时任务:" + sdf.format(new Date()));
    }
}

4.结果

5.corn 表达式

常用表达式例子

  (1)0 0 2 1 * ? *   表示在每月的1日的凌晨2点调整任务

  (2)0 15 10 ? * MON-FRI   表示周一到周五每天上午10:15执行作业

  (3)0 15 10 ? * 6L 2002-2006   表示2002-2006年的每个月的最后一个星期五上午10:15执行作

  (4)0 0 10,14,16 * * ?   每天上午10点,下午2点,4点 

  (5)0 0/30 9-17 * * ?   朝九晚五工作时间内每半小时 

  (6)0 0 12 ? * WED    表示每个星期三中午12点 

  (7)0 0 12 * * ?   每天中午12点触发 

  (8)0 15 10 ? * *    每天上午10:15触发 

  (9)0 15 10 * * ?     每天上午10:15触发 

  (10)0 15 10 * * ? *    每天上午10:15触发 

  (11)0 15 10 * * ? 2005    2005年的每天上午10:15触发 

  (12)0 * 14 * * ?     在每天下午2点到下午2:59期间的每1分钟触发 

  (13)0 0/5 14 * * ?    在每天下午2点到下午2:55期间的每5分钟触发 

  (14)0 0/5 14,18 * * ?     在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发 

  (15)0 0-5 14 * * ?    在每天下午2点到下午2:05期间的每1分钟触发 

  (16)0 10,44 14 ? 3 WED    每年三月的星期三的下午2:10和2:44触发 

  (17)0 15 10 ? * MON-FRI    周一至周五的上午10:15触发 

  (18)0 15 10 15 * ?    每月15日上午10:15触发 

  (19)0 15 10 L * ?    每月最后一日的上午10:15触发 

  (20)0 15 10 ? * 6L    每月的最后一个星期五上午10:15触发 

  (21)0 15 10 ? * 6L 2002-2005   2002年至2005年的每月的最后一个星期五上午10:15触发 

  (22)0 15 10 ? * 6#3   每月的第三个星期五上午10:15触发

链接 : https://www.cnblogs.com/javahr/p/8318728.html

原文地址:https://www.cnblogs.com/Steven5007/p/10045637.html