10.03.2013

Spring Scheduling

Use of annotations -  @EnableScheduling and @Scheduled
@EnableScheduling - Enables Spring's scheduled task execution capability. Declare this annotation on @Configuration classes and this enables detection of @Scheduled
@Configuration
 @EnableScheduling
 public class AppConfig {
     // various @Bean definitions
 }
 
@Scheduled - Three flavors 
@Scheduled(fixedDelay =30000)
public void execute() {//Do Something here }
@Scheduled(fixedRate=30000)
public void execute() {//Do Something here }
@Scheduled(cron="0 0 * * * *")
public void execute() {//Do Something here} 
 



Cron expressions

  • "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/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
  • "0 30 14 * * ?"=every day at 2:30 PM

No comments:

Post a Comment