10_SpringBoot+Quartz

image-20200708160944615

参考资料:https://www.springcloud.cc/spring-boot.html

中文文档2:https://felord.cn/_doc/_springboot/2.1.5.RELEASE/_book/index.html

1. 导入依赖

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>

2. 任务类

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.demo.quartz;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.Date;

@Component
public class MyQuartz {
@Scheduled(cron="*/2 * * * * ? ") // 每隔 2s 执行一次
public void testQuartz(){
System.out.println("testQuartz:"+new Date().toLocaleString());
}
}

3. @EnableScheduling

在启动类添加 @EnableScheduling 配置,然后直接启动测试。

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

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

10_SpringBoot+Quartz
https://janycode.github.io/2018/06/20/08_框架技术/04_SpringBoot/10_SpringBoot+Quartz/
作者
Jerry(姜源)
发布于
2018年6月20日
许可协议