|
| 1 | +package org.baeldung.scheduling; |
| 2 | + |
| 3 | +import org.springframework.scheduling.annotation.Scheduled; |
| 4 | +import org.springframework.stereotype.Component; |
| 5 | + |
| 6 | +@Component("scheduledAnnotationExample") |
| 7 | +public class ScheduledAnnotationExample { |
| 8 | + |
| 9 | + @Scheduled(fixedDelay = 1000) |
| 10 | + public void scheduleFixedDelayTask() { |
| 11 | + System.out.println("Fixed delay task - " + System.currentTimeMillis() / 1000); |
| 12 | + } |
| 13 | + |
| 14 | + @Scheduled(fixedDelayString = "${fixedDelay.in.milliseconds}") |
| 15 | + public void scheduleFixedDelayTaskUsingExpression() { |
| 16 | + System.out.println("Fixed delay task - " + System.currentTimeMillis() / 1000); |
| 17 | + } |
| 18 | + |
| 19 | + @Scheduled(fixedDelay = 1000, initialDelay = 2000) |
| 20 | + public void scheduleFixedDelayWithInitialDelayTask() { |
| 21 | + System.out.println("Fixed delay task with one second initial delay - " + System.currentTimeMillis() / 1000); |
| 22 | + } |
| 23 | + |
| 24 | + @Scheduled(fixedRate = 1000) |
| 25 | + public void scheduleFixedRateTask() { |
| 26 | + System.out.println("Fixed rate task - " + System.currentTimeMillis() / 1000); |
| 27 | + } |
| 28 | + |
| 29 | + @Scheduled(fixedRateString = "${fixedRate.in.milliseconds}") |
| 30 | + public void scheduleFixedRateTaskUsingExpression() { |
| 31 | + System.out.println("Fixed rate task - " + System.currentTimeMillis() / 1000); |
| 32 | + } |
| 33 | + |
| 34 | + @Scheduled(fixedDelay = 1000, initialDelay = 100) |
| 35 | + public void scheduleFixedRateWithInitialDelayTask() { |
| 36 | + System.out.println("Fixed delay task with one second initial delay - " + System.currentTimeMillis() / 1000); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Scheduled task is executed at 10:15 AM on the 15th day of every month |
| 41 | + */ |
| 42 | + @Scheduled(cron = "0 15 10 15 * ?") |
| 43 | + public void scheduleTaskUsingCronExpression() { |
| 44 | + System.out.println("schedule tasks using cron expressions - " + System.currentTimeMillis() / 1000); |
| 45 | + } |
| 46 | + |
| 47 | + @Scheduled(cron = "${cron.expression}") |
| 48 | + public void scheduleTaskUsingExternalizedCronExpression() { |
| 49 | + System.out.println("schedule tasks using externalized cron expressions - " + System.currentTimeMillis() / 1000); |
| 50 | + } |
| 51 | + |
| 52 | +} |
0 commit comments