Languages
[Edit]
EN

Spring @Scheduled every 5 min practical example

1 points
Created by:
Tyreese-Aguirre
289

Quick solution: 

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Slf4j
@Service
public class CronExampleService {

    @Scheduled(cron = "0 0/5 * * * *")
    public void eachFiveMinutes() {
        log.info("test info " + LocalDateTime.now());
    }
}

Output:

2021-07-14 05:15:00.030  INFO 21716 --- [   scheduling-1] example.service.CronExampleService    : test info 2021-07-14T05:15:00.026327
2021-07-14 05:20:00.006  INFO 21716 --- [   scheduling-1] example.service.CronExampleService    : test info 2021-07-14T05:20:00.006950
2021-07-14 05:25:00.008  INFO 21716 --- [   scheduling-1] example.service.CronExampleService    : test info 2021-07-14T05:25:00.008971

 

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join