From 796fdf33abc2c867247ae698b082c30010e98127 Mon Sep 17 00:00:00 2001 From: "Yangkai.Shen" <237497819@qq.com> Date: Tue, 8 May 2018 10:41:02 +0800 Subject: [PATCH] =?UTF-8?q?spring=20boot=20=E4=BD=BF=E7=94=A8=20@EnableSch?= =?UTF-8?q?eduling=20=E5=92=8C=20@Scheduled=20=E5=AE=9E=E7=8E=B0=E5=AE=9A?= =?UTF-8?q?=E6=97=B6=E4=BB=BB=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spring-boot-demo-parent/pom.xml | 1 + spring-boot-demo-task-schedule/README.md | 84 +++++++++++++++++++ spring-boot-demo-task-schedule/pom.xml | 24 ++++++ ...SpringBootDemoTaskScheduleApplication.java | 12 +++ .../config/TaskConfig.java | 41 +++++++++ .../task/TaskJob.java | 50 +++++++++++ .../src/main/resources/application.yml | 0 ...gBootDemoTaskScheduleApplicationTests.java | 16 ++++ 8 files changed, 228 insertions(+) create mode 100644 spring-boot-demo-task-schedule/README.md create mode 100644 spring-boot-demo-task-schedule/pom.xml create mode 100644 spring-boot-demo-task-schedule/src/main/java/com/xkcoding/springbootdemotaskschedule/SpringBootDemoTaskScheduleApplication.java create mode 100644 spring-boot-demo-task-schedule/src/main/java/com/xkcoding/springbootdemotaskschedule/config/TaskConfig.java create mode 100644 spring-boot-demo-task-schedule/src/main/java/com/xkcoding/springbootdemotaskschedule/task/TaskJob.java create mode 100644 spring-boot-demo-task-schedule/src/main/resources/application.yml create mode 100644 spring-boot-demo-task-schedule/src/test/java/com/xkcoding/springbootdemotaskschedule/SpringBootDemoTaskScheduleApplicationTests.java diff --git a/spring-boot-demo-parent/pom.xml b/spring-boot-demo-parent/pom.xml index 898e09a..05f3768 100644 --- a/spring-boot-demo-parent/pom.xml +++ b/spring-boot-demo-parent/pom.xml @@ -31,6 +31,7 @@ ../spring-boot-demo-util ../spring-boot-demo-elasticsearch ../spring-boot-demo-template-beetl + ../spring-boot-demo-task-schedule diff --git a/spring-boot-demo-task-schedule/README.md b/spring-boot-demo-task-schedule/README.md new file mode 100644 index 0000000..98ccfdf --- /dev/null +++ b/spring-boot-demo-task-schedule/README.md @@ -0,0 +1,84 @@ +# spring-boot-demo-task-schedule + +依赖 [spring-boot-demo-parent](../spring-boot-demo-parent) + +## TaskConfig.java + +```java +/** + *

+ * 定时任务配置,配置线程池,使用不同线程执行任务,提升效率 + *

+ * + * @package: com.xkcoding.springbootdemotaskschedule.config + * @description: 定时任务配置,配置线程池,使用不同线程执行任务,提升效率 + * @author: yangkai.shen + * @date: Created in 2018/5/8 上午10:01 + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +@Configuration +@EnableScheduling +@ComponentScan(basePackages = "com.xkcoding.springbootdemotaskschedule.task") +public class TaskConfig implements SchedulingConfigurer { + + @Override + public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { + taskRegistrar.setScheduler(taskExecutor()); + } + + @Bean + public Executor taskExecutor() { + return Executors.newScheduledThreadPool(100); + } + +} +``` + +## TaskJob.java + +```java +/** + *

+ * 定时任务 + *

+ * + * @package: com.xkcoding.springbootdemotaskschedule.task + * @description: 定时任务 + * @author: yangkai.shen + * @date: Created in 2018/5/8 上午10:05 + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +@Component +@Slf4j +public class TaskJob { + + /** + * 每隔 10s 执行一次 + */ + @Scheduled(cron = "0/10 * * * * ?") + public void job1(){ + log.info("【job1】开始执行:{}",DateUtil.formatDateTime(new Date())); + } + + /** + * 间隔 2s 执行 + */ + @Scheduled(fixedRate = 2000) + public void job2(){ + log.info("【job2】开始执行:{}",DateUtil.formatDateTime(new Date())); + } + + /** + * 延迟 5s 后间隔 4s 执行 + */ + @Scheduled(fixedDelay = 4000,initialDelay = 5000) + public void job3(){ + log.info("【job3】开始执行:{}",DateUtil.formatDateTime(new Date())); + } +} +``` + diff --git a/spring-boot-demo-task-schedule/pom.xml b/spring-boot-demo-task-schedule/pom.xml new file mode 100644 index 0000000..1f0508f --- /dev/null +++ b/spring-boot-demo-task-schedule/pom.xml @@ -0,0 +1,24 @@ + + + 4.0.0 + + spring-boot-demo-task-schedule + 0.0.1-SNAPSHOT + jar + + spring-boot-demo-task-schedule + Demo project for Spring Boot + + + com.xkcoding + spring-boot-demo-parent + 0.0.1-SNAPSHOT + ../spring-boot-demo-parent/pom.xml + + + + spring-boot-demo-task-schedule + + + \ No newline at end of file diff --git a/spring-boot-demo-task-schedule/src/main/java/com/xkcoding/springbootdemotaskschedule/SpringBootDemoTaskScheduleApplication.java b/spring-boot-demo-task-schedule/src/main/java/com/xkcoding/springbootdemotaskschedule/SpringBootDemoTaskScheduleApplication.java new file mode 100644 index 0000000..20a0e77 --- /dev/null +++ b/spring-boot-demo-task-schedule/src/main/java/com/xkcoding/springbootdemotaskschedule/SpringBootDemoTaskScheduleApplication.java @@ -0,0 +1,12 @@ +package com.xkcoding.springbootdemotaskschedule; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringBootDemoTaskScheduleApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringBootDemoTaskScheduleApplication.class, args); + } +} diff --git a/spring-boot-demo-task-schedule/src/main/java/com/xkcoding/springbootdemotaskschedule/config/TaskConfig.java b/spring-boot-demo-task-schedule/src/main/java/com/xkcoding/springbootdemotaskschedule/config/TaskConfig.java new file mode 100644 index 0000000..d36a1f7 --- /dev/null +++ b/spring-boot-demo-task-schedule/src/main/java/com/xkcoding/springbootdemotaskschedule/config/TaskConfig.java @@ -0,0 +1,41 @@ +package com.xkcoding.springbootdemotaskschedule.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.annotation.EnableScheduling; +import org.springframework.scheduling.annotation.SchedulingConfigurer; +import org.springframework.scheduling.config.ScheduledTaskRegistrar; + +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; + +/** + *

+ * 定时任务配置,配置线程池,使用不同线程执行任务,提升效率 + *

+ * + * @package: com.xkcoding.springbootdemotaskschedule.config + * @description: 定时任务配置,配置线程池,使用不同线程执行任务,提升效率 + * @author: yangkai.shen + * @date: Created in 2018/5/8 上午10:01 + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +@Configuration +@EnableScheduling +@ComponentScan(basePackages = "com.xkcoding.springbootdemotaskschedule.task") +public class TaskConfig implements SchedulingConfigurer { + + @Override + public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { + taskRegistrar.setScheduler(taskExecutor()); + } + + @Bean + public Executor taskExecutor() { + return Executors.newScheduledThreadPool(100); + } + +} diff --git a/spring-boot-demo-task-schedule/src/main/java/com/xkcoding/springbootdemotaskschedule/task/TaskJob.java b/spring-boot-demo-task-schedule/src/main/java/com/xkcoding/springbootdemotaskschedule/task/TaskJob.java new file mode 100644 index 0000000..0ca05d5 --- /dev/null +++ b/spring-boot-demo-task-schedule/src/main/java/com/xkcoding/springbootdemotaskschedule/task/TaskJob.java @@ -0,0 +1,50 @@ +package com.xkcoding.springbootdemotaskschedule.task; + +import com.xiaoleilu.hutool.date.DateUtil; +import lombok.extern.slf4j.Slf4j; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +import java.util.Date; + +/** + *

+ * 定时任务 + *

+ * + * @package: com.xkcoding.springbootdemotaskschedule.task + * @description: 定时任务 + * @author: yangkai.shen + * @date: Created in 2018/5/8 上午10:05 + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +@Component +@Slf4j +public class TaskJob { + + /** + * 每隔 10s 执行一次 + */ + @Scheduled(cron = "0/10 * * * * ?") + public void job1(){ + log.info("【job1】开始执行:{}",DateUtil.formatDateTime(new Date())); + } + + /** + * 间隔 2s 执行 + */ + @Scheduled(fixedRate = 2000) + public void job2(){ + log.info("【job2】开始执行:{}",DateUtil.formatDateTime(new Date())); + } + + /** + * 延迟 5s 后间隔 4s 执行 + */ + @Scheduled(fixedDelay = 4000,initialDelay = 5000) + public void job3(){ + log.info("【job3】开始执行:{}",DateUtil.formatDateTime(new Date())); + } +} diff --git a/spring-boot-demo-task-schedule/src/main/resources/application.yml b/spring-boot-demo-task-schedule/src/main/resources/application.yml new file mode 100644 index 0000000..e69de29 diff --git a/spring-boot-demo-task-schedule/src/test/java/com/xkcoding/springbootdemotaskschedule/SpringBootDemoTaskScheduleApplicationTests.java b/spring-boot-demo-task-schedule/src/test/java/com/xkcoding/springbootdemotaskschedule/SpringBootDemoTaskScheduleApplicationTests.java new file mode 100644 index 0000000..f4d5943 --- /dev/null +++ b/spring-boot-demo-task-schedule/src/test/java/com/xkcoding/springbootdemotaskschedule/SpringBootDemoTaskScheduleApplicationTests.java @@ -0,0 +1,16 @@ +package com.xkcoding.springbootdemotaskschedule; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class SpringBootDemoTaskScheduleApplicationTests { + + @Test + public void contextLoads() { + } + +}