How can you configure scheduled tasks in Spring Boot?

In Spring Boot, there are two ways to configure scheduled tasks.

  1. Scheduled tasks.
  2. Scheduled at scheduled時間
  3. Execute this task every 5 seconds.
  4. Activate scheduling

The sample code is as follows:

@SpringBootApplication
@EnableScheduling
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Scheduled(cron = "0/5 * * * * *")
    public void task() {
        // 定时任务逻辑
        System.out.println("定时任务执行...");
    }
}
  1. able to be executed
  2. Able to be called or contacted.
  3. executes the function
  4. Bean
  5. Someone or something that is able to be run or put into motion.
  6. able to be called

The code example is as follows:

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public Runnable task() {
        return () -> {
            // 定时任务逻辑
            System.out.println("定时任务执行...");
        };
    }
}

It is important to note that both of the above approaches require adding the corresponding annotation to the Spring Boot startup class to enable support for scheduled tasks.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds