From 8974d8f9207c980d25cda7f141a9ca4e1ddbe4e9 Mon Sep 17 00:00:00 2001 From: qinyingjie Date: Fri, 9 Dec 2022 14:03:08 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E6=B7=BB=E5=8A=A0=E5=AE=9A=E6=97=B6?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SpringBootKwanApplication.java | 2 ++ .../config/ScheduledTaskConfig.java | 25 ++++++++++++++++++ .../springbootkwan/schedule/ScheduleTest.java | 25 ++++++++++++++++++ .../schedule/ScheduleTestTwo.java | 26 +++++++++++++++++++ 4 files changed, 78 insertions(+) create mode 100644 src/main/java/com/kwan/springbootkwan/config/ScheduledTaskConfig.java create mode 100644 src/main/java/com/kwan/springbootkwan/schedule/ScheduleTest.java create mode 100644 src/main/java/com/kwan/springbootkwan/schedule/ScheduleTestTwo.java diff --git a/src/main/java/com/kwan/springbootkwan/SpringBootKwanApplication.java b/src/main/java/com/kwan/springbootkwan/SpringBootKwanApplication.java index 9a5eb6e..0413b35 100644 --- a/src/main/java/com/kwan/springbootkwan/SpringBootKwanApplication.java +++ b/src/main/java/com/kwan/springbootkwan/SpringBootKwanApplication.java @@ -2,7 +2,9 @@ package com.kwan.springbootkwan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.scheduling.annotation.EnableScheduling; +@EnableScheduling @SpringBootApplication public class SpringBootKwanApplication { diff --git a/src/main/java/com/kwan/springbootkwan/config/ScheduledTaskConfig.java b/src/main/java/com/kwan/springbootkwan/config/ScheduledTaskConfig.java new file mode 100644 index 0000000..674d6f8 --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/config/ScheduledTaskConfig.java @@ -0,0 +1,25 @@ +package com.kwan.springbootkwan.config; + + +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.annotation.SchedulingConfigurer; +import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; +import org.springframework.scheduling.config.ScheduledTaskRegistrar; + +/** + * scheduler配置类 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2022/12/9 14:01 + */ +@Configuration +public class ScheduledTaskConfig implements SchedulingConfigurer { + @Override + public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { + ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler(); + taskScheduler.setPoolSize(2); + taskScheduler.initialize(); + taskRegistrar.setTaskScheduler(taskScheduler); + } +} diff --git a/src/main/java/com/kwan/springbootkwan/schedule/ScheduleTest.java b/src/main/java/com/kwan/springbootkwan/schedule/ScheduleTest.java new file mode 100644 index 0000000..280c60c --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/schedule/ScheduleTest.java @@ -0,0 +1,25 @@ +package com.kwan.springbootkwan.schedule; + + +import lombok.extern.slf4j.Slf4j; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + + +/** + * schedule测试 10s执行一次 记得在启动类或者Configuration类上添加了@EnableScheduling注解。 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2022/12/9 13:58 + */ +@Component +@Slf4j +public class ScheduleTest { + + @Scheduled(cron = "0/10 * * * * ?") + public void execute() { + log.info("Scheduled task is running ... ..."); + } +} + diff --git a/src/main/java/com/kwan/springbootkwan/schedule/ScheduleTestTwo.java b/src/main/java/com/kwan/springbootkwan/schedule/ScheduleTestTwo.java new file mode 100644 index 0000000..273ffcf --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/schedule/ScheduleTestTwo.java @@ -0,0 +1,26 @@ +package com.kwan.springbootkwan.schedule; + + +import lombok.extern.slf4j.Slf4j; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +import java.util.concurrent.TimeUnit; + + +/** + * schedule测试 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2022/12/9 14:01 + */ +@Component +@Slf4j +public class ScheduleTestTwo { + @Scheduled(cron = "0/10 * * * * *") + public void second() throws InterruptedException { + log.info("Second scheduled task is running... ..."); + TimeUnit.SECONDS.sleep(5); + } +} -- GitLab