【微服务架构 - 16 - Quartz】01 Quartz 的使用

pom.xml


<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-quartz</artifactId>
        </dependency>

Application


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

使用 @EnableScheduling 注解来开启计划任务

创建任务

创建一个每隔两秒钟打印当前时间的任务来测试 Quartz

@Component
public class PrintCurrentTimeTask {
    @Scheduled(cron = "0/2 * * * * ?")
    public void printCurrentTime() {
        System.out.println("Current Time is:" + new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
    }
}

启动服务,控制台打印效果如下:

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_37581282/article/details/88322313
今日推荐