Home  >  Article  >  Java  >  How Springboot implements scheduled tasks

How Springboot implements scheduled tasks

WBOY
WBOYforward
2023-06-03 11:55:251050browse

Timing tasks

The timing here is marked as annotation on the method. If you want to modify the time of the generated environment, it is not very flexible. Quartz boot will be added later and the database will be used. Principles of configuration and reflection.

Note: Java's cron expression is different from Linux's. Please note that Java is 6 bits and Linux is 5 bits.

Startup class

@SpringBootApplication
@EnableScheduling
public class Oss6Application {
  public static void main(String[] args) {
    SpringApplication.run(Oss6Application.class, args);
  }
}

Service class

@Service
public class ScheduledService {

  /**
   * second(秒), minute(分), hour(时), day of month(日), month(月), day of week(周几).
   * 0 * * * * MON-FRI
   * 【0 0/5 14,18 * * ?】 每天14点整,和18点整,每隔5分钟执行一次
   * 【0 15 10 ? * 1-6】 每个月的周一至周六10:15分执行一次
   * 【0 0 2 ? * 6L】每个月的最后一个周六凌晨2点执行一次
   * 【0 0 2 LW * ?】每个月的最后一个工作日凌晨2点执行一次
   * 【0 0 2-4 ? * 1#1】每个月的第一个周一凌晨2点到4点期间,每个整点都执行一次;
   */
  // @Scheduled(cron = "0 * * * * MON-SAT")
  //@Scheduled(cron = "0,1,2,3,4 * * * * MON-SAT")
  // @Scheduled(cron = "0-4 * * * * MON-SAT")
  @Scheduled(cron = "0/4 * * * * MON-SAT") //每4秒执行一次
  public void hello(){
    System.out.println("hello ... ");
  }
}

The above is the detailed content of How Springboot implements scheduled tasks. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete