Spring Boot 集成 Quartz
一.创建项目添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
二.DEMO
QuatrzApplication:
@EnableScheduling
@SpringBootApplication
public class QuatrzApplication {
public static void main(String[] args) {
SpringApplication.run(QuatrzApplication.class, args);
}
}
创建任务:每 5 秒钟打印当前时间
@Component
public class PrintCurrentTimeTask {
@Scheduled(cron = "0/5 * * * * ? ")
public void printCurrentTime() {
System.out.println("Current Time is:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
}
}