SpringBoot整合Quartz定时任务 系统job Spring Boot教程 调度任务

时间:2023-03-09 03:40:43
SpringBoot整合Quartz定时任务 系统job  Spring Boot教程 调度任务

原文地址:https://www.cnblogs.com/allalongx/p/8477368.html

构建工程

创建一个Springboot工程,在它的程序入口加上@EnableScheduling,开启调度任务。

1
2
3
4
5
6
7
8
@SpringBootApplication
@EnableScheduling
public class SpringbootSchedulingTasksApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootSchedulingTasksApplication.class, args);
    }
}

  

创建定时任务

创建一个定时任务,每过5s在控制台打印当前时间。

1
2
3
4
5
6
7
8
9
10
11
12
@Component
public class ScheduledTasks {
    private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class);
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
    @Scheduled(fixedRate = 5000)
    public void reportCurrentTime() {
        log.info("The time is now {}", dateFormat.format(new Date()));
    }
}

  

通过在方法上加@Scheduled注解,表明该方法是一个调度任务。

  • @Scheduled(fixedRate = 5000) :上一次开始执行时间点之后5秒再执行
  • @Scheduled(fixedDelay = 5000) :上一次执行完毕时间点之后5秒再执行
  • @Scheduled(initialDelay=1000, fixedRate=5000) :第一次延迟1秒后执行,之后按fixedRate的规则每5秒执行一次
  • @Scheduled(cron=” /5 “) :通过cron表达式定义规则,什么是cro表达式,自行搜索引擎。

测试

启动springboot工程,控制台没过5s就打印出了当前的时间。

1
2
3
4
2017-04-29 17:39:37.672 INFO 677 — [pool-1-thread-1] com.forezp.task.ScheduledTasks : The time is now 17:39:37
2017-04-29 17:39:42.671 INFO 677 — [pool-1-thread-1] com.forezp.task.ScheduledTasks : The time is now 17:39:42
2017-04-29 17:39:47.672 INFO 677 — [pool-1-thread-1] com.forezp.task.ScheduledTasks : The time is now 17:39:47
2017-04-29 17:39:52.675 INFO 677 — [pool-1-thread-1] com.forezp.task.ScheduledTasks : The time is now 17:39:52

  

在springboot创建定时任务只需2步:

  • 1.在程序的入口加上@EnableScheduling注解。
  • 2.在定时方法上加@Scheduled注解。

SpringBoot整合Quartz定时任务 系统job  Spring Boot教程 调度任务

原文地址: https://www.cnblogs.com/mr-wuxiansheng/p/6971493.html

记录一个SpringBoot 整合 Quartz 的Demo实例

POM.XML文件

<!-- 定时器任务 quartz需要导入的坐标 -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>1.8.5</version>
</dependency>

类似于控制器代码:

SpringBoot整合Quartz定时任务 系统job  Spring Boot教程 调度任务
package com.xiaowu.quartz.demo;

import java.util.Date;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; /***
*
* Quartz设置项目全局的定时任务
*
* @Component注解的意义 泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。一般公共的方法我会用上这个注解
*
*
* @author WQ
*
*/
@Component
public class QuartzDemo { @Scheduled(cron = "0 0/1 * * * ?") // 每分钟执行一次
public void work() throws Exception {
System.out.println("执行调度任务:"+new Date());
} @Scheduled(fixedRate = 5000)//每5秒执行一次
public void play() throws Exception {
System.out.println("执行Quartz定时器任务:"+new Date());
} @Scheduled(cron = "0/2 * * * * ?") //每2秒执行一次
public void doSomething() throws Exception {
System.out.println("每2秒执行一个的定时任务:"+new Date());
} @Scheduled(cron = "0 0 0/1 * * ? ") // 每一小时执行一次
public void goWork() throws Exception {
System.out.println("每一小时执行一次的定时任务:"+new Date());
} }
SpringBoot整合Quartz定时任务 系统job  Spring Boot教程 调度任务

启动SpringBoot项目,即可。

public static void main(String[] args) {
SpringApplication.run(Chapter1Application.class, args);
}

,截图如下:

SpringBoot整合Quartz定时任务 系统job  Spring Boot教程 调度任务