Awaitility
Initialize a spring new project and add awaitility library as dependency in pom.xml
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<version>3.1.2</version>
<scope>test</scope>
</dependency>
Enable
Enable scheduling in you App with @EnableScheduling annotation.
package com.minte9.scheduled_task;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling // Look Here
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
Task
Create you component class to manage scheduled tasks.
package com.minte9.scheduled_task;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component // Look Here
public class Task {
private static final SimpleDateFormat dateFormat =
new SimpleDateFormat("HH:mm:ss");
@Scheduled(fixedRate = 1000) // 1 second
public void runningTime() {
System.out.println(
"Time is: " + dateFormat.format(new Date())
);
}
}
Run
Compile and run you application.
mvn spring-boot:run
# Time is: 17:36:24
# Time is: 17:36:25
# Time is: 17:36:26
Last update: 380 days ago