From 1dcf51350e3af0d5e97fb506933909986d57f321 Mon Sep 17 00:00:00 2001 From: "Yangkai.Shen" <237497819@qq.com> Date: Thu, 22 Nov 2018 19:33:16 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20spring-boot-demo-task=20=E5=AE=8C?= =?UTF-8?q?=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spring-boot-demo-task/README.md | 185 ++++++++++++++++++ spring-boot-demo-task/pom.xml | 18 +- .../task/SpringBootDemoTaskApplication.java | 13 ++ .../com/xkcoding/task/config/TaskConfig.java | 46 +++++ .../java/com/xkcoding/task/job/TaskJob.java | 52 +++++ .../src/main/resources/application.properties | 0 .../src/main/resources/application.yml | 11 ++ 7 files changed, 324 insertions(+), 1 deletion(-) create mode 100644 spring-boot-demo-task/README.md create mode 100644 spring-boot-demo-task/src/main/java/com/xkcoding/task/config/TaskConfig.java create mode 100644 spring-boot-demo-task/src/main/java/com/xkcoding/task/job/TaskJob.java delete mode 100644 spring-boot-demo-task/src/main/resources/application.properties create mode 100644 spring-boot-demo-task/src/main/resources/application.yml diff --git a/spring-boot-demo-task/README.md b/spring-boot-demo-task/README.md new file mode 100644 index 0000000..280c8d0 --- /dev/null +++ b/spring-boot-demo-task/README.md @@ -0,0 +1,185 @@ +# spring-boot-demo-task + +> 此 demo 主要演示了 Spring Boot 如何快速实现定时任务。 + +## pom.xml + +```xml + + + 4.0.0 + + spring-boot-demo-task + 1.0.0-SNAPSHOT + jar + + spring-boot-demo-task + Demo project for Spring Boot + + + com.xkcoding + spring-boot-demo + 1.0.0-SNAPSHOT + + + + UTF-8 + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.apache.commons + commons-lang3 + + + + org.projectlombok + lombok + true + + + + cn.hutool + hutool-all + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + spring-boot-demo-task + + + org.springframework.boot + spring-boot-maven-plugin + + + + + +``` + +## TaskConfig.java + +> 此处等同于在配置文件配置 +> +> ```properties +> spring.task.scheduling.pool.size=20 +> spring.task.scheduling.thread-name-prefix=Job-Thread- +> ``` + +```java +/** + *

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

+ * + * @package: com.xkcoding.task.config + * @description: 定时任务配置,配置线程池,使用不同线程执行任务,提升效率 + * @author: yangkai.shen + * @date: Created in 2018/11/22 19:02 + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +@Configuration +@EnableScheduling +@ComponentScan(basePackages = {"com.xkcoding.task.job"}) +public class TaskConfig implements SchedulingConfigurer { + @Override + public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { + taskRegistrar.setScheduler(taskExecutor()); + } + + /** + * 这里等同于配置文件配置 + * {@code spring.task.scheduling.pool.size=20} - Maximum allowed number of threads. + * {@code spring.task.scheduling.thread-name-prefix=Job-Thread- } - Prefix to use for the names of newly created threads. + * {@link org.springframework.boot.autoconfigure.task.TaskSchedulingProperties} + */ + @Bean + public Executor taskExecutor() { + return new ScheduledThreadPoolExecutor(20, new BasicThreadFactory.Builder().namingPattern("Job-Thread-%d").build()); + } +} +``` + +## TaskJob.java + +```java +/** + *

+ * 定时任务 + *

+ * + * @package: com.xkcoding.task.job + * @description: 定时任务 + * @author: yangkai.shen + * @date: Created in 2018/11/22 19:09 + * @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())); + } +} +``` + +## application.yml + +```yaml +server: + port: 8080 + servlet: + context-path: /demo +# 下面的配置等同于 TaskConfig +#spring: +# task: +# scheduling: +# pool: +# size: 20 +# thread-name-prefix: Job-Thread- +``` + +## 参考 + +- Spring Boot官方文档:https://docs.spring.io/spring-boot/docs/2.1.0.RELEASE/reference/htmlsingle/#boot-features-task-execution-scheduling \ No newline at end of file diff --git a/spring-boot-demo-task/pom.xml b/spring-boot-demo-task/pom.xml index ef03feb..9405fc6 100644 --- a/spring-boot-demo-task/pom.xml +++ b/spring-boot-demo-task/pom.xml @@ -25,7 +25,23 @@ org.springframework.boot - spring-boot-starter + spring-boot-starter-web + + + + org.apache.commons + commons-lang3 + + + + org.projectlombok + lombok + true + + + + cn.hutool + hutool-all diff --git a/spring-boot-demo-task/src/main/java/com/xkcoding/task/SpringBootDemoTaskApplication.java b/spring-boot-demo-task/src/main/java/com/xkcoding/task/SpringBootDemoTaskApplication.java index ec1b3cf..55687ad 100644 --- a/spring-boot-demo-task/src/main/java/com/xkcoding/task/SpringBootDemoTaskApplication.java +++ b/spring-boot-demo-task/src/main/java/com/xkcoding/task/SpringBootDemoTaskApplication.java @@ -3,6 +3,19 @@ package com.xkcoding.task; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +/** + *

+ * 启动器 + *

+ * + * @package: com.xkcoding.task + * @description: 启动器 + * @author: yangkai.shen + * @date: Created in 2018/11/22 19:00 + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ @SpringBootApplication public class SpringBootDemoTaskApplication { diff --git a/spring-boot-demo-task/src/main/java/com/xkcoding/task/config/TaskConfig.java b/spring-boot-demo-task/src/main/java/com/xkcoding/task/config/TaskConfig.java new file mode 100644 index 0000000..43ec481 --- /dev/null +++ b/spring-boot-demo-task/src/main/java/com/xkcoding/task/config/TaskConfig.java @@ -0,0 +1,46 @@ +package com.xkcoding.task.config; + +import org.apache.commons.lang3.concurrent.BasicThreadFactory; +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.ScheduledThreadPoolExecutor; + +/** + *

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

+ * + * @package: com.xkcoding.task.config + * @description: 定时任务配置,配置线程池,使用不同线程执行任务,提升效率 + * @author: yangkai.shen + * @date: Created in 2018/11/22 19:02 + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +@Configuration +@EnableScheduling +@ComponentScan(basePackages = {"com.xkcoding.task.job"}) +public class TaskConfig implements SchedulingConfigurer { + @Override + public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { + taskRegistrar.setScheduler(taskExecutor()); + } + + /** + * 这里等同于配置文件配置 + * {@code spring.task.scheduling.pool.size=20} - Maximum allowed number of threads. + * {@code spring.task.scheduling.thread-name-prefix=Job-Thread- } - Prefix to use for the names of newly created threads. + * {@link org.springframework.boot.autoconfigure.task.TaskSchedulingProperties} + */ + @Bean + public Executor taskExecutor() { + return new ScheduledThreadPoolExecutor(20, new BasicThreadFactory.Builder().namingPattern("Job-Thread-%d").build()); + } +} diff --git a/spring-boot-demo-task/src/main/java/com/xkcoding/task/job/TaskJob.java b/spring-boot-demo-task/src/main/java/com/xkcoding/task/job/TaskJob.java new file mode 100644 index 0000000..dfbc912 --- /dev/null +++ b/spring-boot-demo-task/src/main/java/com/xkcoding/task/job/TaskJob.java @@ -0,0 +1,52 @@ +package com.xkcoding.task.job; + +import cn.hutool.core.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.task.job + * @description: 定时任务 + * @author: yangkai.shen + * @date: Created in 2018/11/22 19:09 + * @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())); + } +} \ No newline at end of file diff --git a/spring-boot-demo-task/src/main/resources/application.properties b/spring-boot-demo-task/src/main/resources/application.properties deleted file mode 100644 index e69de29..0000000 diff --git a/spring-boot-demo-task/src/main/resources/application.yml b/spring-boot-demo-task/src/main/resources/application.yml new file mode 100644 index 0000000..5105cc3 --- /dev/null +++ b/spring-boot-demo-task/src/main/resources/application.yml @@ -0,0 +1,11 @@ +server: + port: 8080 + servlet: + context-path: /demo +# 下面的配置等同于 TaskConfig +#spring: +# task: +# scheduling: +# pool: +# size: 20 +# thread-name-prefix: Job-Thread- \ No newline at end of file