Κυριακή 5 Ιουλίου 2020

Create a Spring Scheduled task with @Scheduled cron and @EnableScheduling






Create a Spring @Scheduled task firing every 50 seconds









1. Spring configuration class - add @EnableScheduling



@Configuration @EnableScheduling public class YourConfigurationFile { // ...
}



2. Application yml



application: appSection: yourCron: "*/50 0 * * * *"



3. Service Implementation class 



import org.springframework.scheduling.annotation.Scheduled; // ... @Scheduled(cron = "${application.appSection.yourCron}") public void doSomething() { // This will fire every 50 seconds }








Extra - Cron expression examples from stackoverflow:



* "0 0 * * * *" = the top of every hour of every day.
* "*/10 * * * * *" = every ten seconds.
* "0 0 8-10 * * *" = 8, 9 and 10 o'clock of every day.
* "0 0 8,10 * * *" = 8 and 10 o'clock of every day.
* "0 0/30 8-10 * * *" = 8:00, 8:30, 9:00, 9:30 and 10 o'clock every day.
* "0 0 9-17 * * MON-FRI" = on the hour nine-to-five weekdays
* "0 0 0 25 12 ?" = every Christmas Day at midnight
https://stackoverflow.com/questions/26147044/spring-cron-expression-for-every-day-101am

Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου

What may be missing, or could get better?