Quartz是一个任务日程管理系统,一个在预先确定(被纳入日程)的时间到达时,负责执行(或者通知)其他软件组件的系统。
一、pom依赖
<!-- 父工程配置文件里统一配置了依赖的版本,新加入的quartz依赖找不到版本时,需要在父工程中加入版本号对应,负责会报错 -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
<!-- quartz依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
</dependencies>
二、controller层
@Component// 创建该类对象
public class quartzController {
@Scheduled(fixedRate=1000)//每隔一秒钟调用一次
public void run(){
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
}
}
三、启动类
@SpringBootApplication(scanBasePackages= {"com.quartz.controller"})
@EnableScheduling //启动类开启任务调度
public class quartzTest {
public static void main(String[] args) {
SpringApplication.run(quartzTest.class, args);
}
}
四、结果