第一步:启动类添加注解 @EnableScheduling
@EnableDiscoveryClient
@ComponentScan(basePackages = {"com.wind"})
@SpringBootApplication
@EnableFeignClients
@EnableScheduling//定时器注解
public class DataApplication {
public static void main(String[] args) throws InterruptedException, IOException {
SpringApplication.run(DataApplication.class,args);
}
}
第二步:创建task编写代码
@Component
public class XxxTask {
@Scheduled(fixedRate = 1000*6)//6秒执行一次
public void test() {
System.out.println("定时任务Task测试");
}
}
第三步:启动项目测试
2021-06-25 11:44:36.725 INFO 26248 --- [1)-192.168.41.1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-06-25 11:44:36.725 INFO 26248 --- [1)-192.168.41.1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2021-06-25 11:44:36.733 INFO 26248 --- [1)-192.168.41.1] o.s.web.servlet.DispatcherServlet : Completed initialization in 8 ms
定时任务Task测试
定时任务Task测试
定时任务Task测试
成功!