springboot整合quartz
项目创建
File–>New–>Project
Spring Initializr
Maven Project
Core — Lombok
Web — Web
Template Engines — Freemarker
SQL — mybatis,jdbc,mysql
I/O — Quartz Scheduler
使用spring自带定时器的任务实现
- springboot启用计划任务
- @EnableScheduling
标注启动定时任务
@SpringBootApplication
@EnableScheduling
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
- @Scheduled
定义某个定时作业(或工作)
public class MyJob1 {
@Scheduled(cron = "*/5 * * * * ?")
public void execute() {
log.info("任务正执行:" + new Date().toLocaleString());
}
}
案例代码
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.tang