springboot集成schedule实现自动任务
点关注不迷路,欢迎再来!
精简博客内容,尽量已专业术语来分享。
努力做到对每一位认可自己的读者负责。
帮助别人的同时更是丰富自己的良机。
一.启动类@EnableScheduling
package com.sun.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.scheduling.annotation.EnableScheduling;
/**
* 客户端
* @author andy
*
*/
@EnableEurekaClient
@EnableScheduling
@SpringBootApplication
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
}
二.Cron常用表达式
/**
* 自动任务Schedule
* @author andy
*/
@Component
public class ScheduleCongfig {
@Scheduled(cron = "0/05 * * * * *")
public void reportCurrentTime() {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
System.out.println("每隔五秒钟执行一次: " + dateFormat.format(new Date()));
}
}
打印结果:
每隔五秒钟执行一次: 2019-07-03 08:34:40
每隔五秒钟执行一次: 2019-07-03 08:34:45
每隔五秒钟执行一次: 2019-07-03 08:34:50
每隔五秒钟执行一次: 2019-07-03 08:34:55