其他分享
首页 > 其他分享> > 使用awaitility包进行Scheduling Task

使用awaitility包进行Scheduling Task

作者:互联网

 

依赖:
  Later versions of the awaitility library do not work for this test, so you have to specify version 3.1.2.
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<version>3.1.2</version>
<scope>test</scope>
</dependency>



 1 package com.springboot;
 2 
 3 import org.slf4j.Logger;
 4 import org.slf4j.LoggerFactory;
 5 import org.springframework.scheduling.annotation.Scheduled;
 6 import org.springframework.stereotype.Component;
 7 
 8 import java.text.SimpleDateFormat;
 9 import java.util.Date;
10 
11 @Component
12 public class ScheduleTask {
13 
14     public static final Logger logger = LoggerFactory.getLogger(ScheduleTask.class);
15     public static final SimpleDateFormat sdf =
16             new SimpleDateFormat("一年中的第D天-第F个E-yyyy-MM-dd-HH:mm:ss");
17 
18     //固定每5s刷新一次
19     @Scheduled(fixedRate = 5000L)
20     public void currentTime(){
21         //{}:占位符
22         logger.info("现在时间是 {}",sdf.format(new Date()));
23     }
24 }

 

需要在主类中使用@EnableScheduling开启

 1 package com.springboot;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 import org.springframework.scheduling.annotation.EnableScheduling;
 6 
 7 @SpringBootApplication
 8 @EnableScheduling
 9 public class Application {
10 
11     public static void main(String[] args) {
12         SpringApplication.run(Application.class, args);
13     }
14 
15 }

 

  运行效果: 

 


 


 


 

标签:awaitility,Task,springframework,class,import,Scheduling,org,public
来源: https://www.cnblogs.com/luo115/p/16062241.html