Home > Java > javaTutorial > body text

How Springboot implements scheduled tasks

WBOY
Release: 2023-06-03 11:55:25
forward
1049 people have browsed it

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);
  }
}
Copy after login

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 ... ");
  }
}
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template