0 day streak
java / beginner
Snippet

Automating Tasks with @Scheduled

The @Scheduled annotation enables you to run methods periodically. The 'fixedRate' attribute determines the interval in milliseconds.

snippet.java
java
1
2
3
4
@Scheduled(fixedRate = 5000)
public void reportCurrentTime() {
System.out.println("Executed every 5 seconds");
}
spring
Breakdown
1
@Scheduled(fixedRate = 5000)
Tells Spring to execute this method every 5000ms (5 seconds).
2
public void reportCurrentTime()
The method to be executed. It must have a void return type and no arguments.