Example code for the use of Spring Boot timed task

  • 2020-07-21 07:53:35
  • OfStack

Configuring timing tasks in Spring Boot is extremely simple... Here's the example code:


import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
 * Created by winner_0715 on 2017/4/18.
 */
@Configuration
@EnableScheduling
public class SchedulingConfig {
  //  every 5 Seconds to perform 1 time 
  @Scheduled(cron = "0/5 * * * * ?")
  public void scheduler() {
    System.out.println("SchedulingConfig.scheduler()" + 
        new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
  }
}

Related articles: