diff --git a/src/main/java/com/kwan/springbootkwan/SpringBootKwanApplication.java b/src/main/java/com/kwan/springbootkwan/SpringBootKwanApplication.java index 9a5eb6eada76c2a31b84d881a3e993a839376c78..0413b3592bf3bc2349229b0fe6b847b002c9ce8e 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 0000000000000000000000000000000000000000..674d6f87ec472a62dd2b054e0c4268ce8f66983e --- /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 0000000000000000000000000000000000000000..280c60c50a931a3314246310fb8f2740f881936e --- /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 0000000000000000000000000000000000000000..273ffcf5e635c7535bcc69422b3a1202b5db15c8 --- /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); + } +}