From ade55d89a477563349c4149059af191a8cb70ae9 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sun, 5 Dec 2021 17:59:58 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=9A=E7=A7=9F=E6=88=B7=EF=BC=8C=E6=8E=A5?= =?UTF-8?q?=E5=85=A5=20Spring=20Async=20=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../async/config/AsyncConfiguration.java | 9 ----- .../framework/async/package-info.java | 4 --- .../yudao-spring-boot-starter-job/pom.xml | 5 ++- .../config/YudaoAsyncAutoConfiguration.java | 36 +++++++++++++++++++ .../config/YudaoQuartzAutoConfiguration.java | 3 ++ .../yudao/framework/quartz/package-info.java | 4 ++- .../main/resources/META-INF/spring.factories | 3 +- .../《芋道 Spring Boot 异步任务入门》.md | 0 .../async/config/AsyncConfiguration.java | 9 ----- .../framework/async/package-info.java | 4 --- .../《芋道 Spring Boot 异步任务入门》.md | 1 - 11 files changed, 48 insertions(+), 30 deletions(-) delete mode 100644 yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/framework/async/config/AsyncConfiguration.java delete mode 100644 yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/framework/async/package-info.java create mode 100644 yudao-framework/yudao-spring-boot-starter-job/src/main/java/cn/iocoder/yudao/framework/quartz/config/YudaoAsyncAutoConfiguration.java rename {yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/framework/async => yudao-framework/yudao-spring-boot-starter-job}/《芋道 Spring Boot 异步任务入门》.md (100%) delete mode 100644 yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/framework/async/config/AsyncConfiguration.java delete mode 100644 yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/framework/async/package-info.java delete mode 100644 yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/framework/async/《芋道 Spring Boot 异步任务入门》.md diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/framework/async/config/AsyncConfiguration.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/framework/async/config/AsyncConfiguration.java deleted file mode 100644 index 720fc5a745..0000000000 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/framework/async/config/AsyncConfiguration.java +++ /dev/null @@ -1,9 +0,0 @@ -package cn.iocoder.yudao.adminserver.framework.async.config; - -import org.springframework.context.annotation.Configuration; -import org.springframework.scheduling.annotation.EnableAsync; - -@Configuration -@EnableAsync -public class AsyncConfiguration { -} diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/framework/async/package-info.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/framework/async/package-info.java deleted file mode 100644 index aac87cd3a4..0000000000 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/framework/async/package-info.java +++ /dev/null @@ -1,4 +0,0 @@ -/** - * 异步执行,基于 Spring @Async 实现 - */ -package cn.iocoder.yudao.adminserver.framework.async; diff --git a/yudao-framework/yudao-spring-boot-starter-job/pom.xml b/yudao-framework/yudao-spring-boot-starter-job/pom.xml index f54794731e..4a1cd04a6b 100644 --- a/yudao-framework/yudao-spring-boot-starter-job/pom.xml +++ b/yudao-framework/yudao-spring-boot-starter-job/pom.xml @@ -12,7 +12,10 @@ jar ${artifactId} - 定时任务,基于 Quartz 拓展 + 任务拓展 + 1. 定时任务,基于 Quartz 拓展 + 2. 异步任务,基于 Spring Async 拓展 + https://github.com/YunaiV/ruoyi-vue-pro diff --git a/yudao-framework/yudao-spring-boot-starter-job/src/main/java/cn/iocoder/yudao/framework/quartz/config/YudaoAsyncAutoConfiguration.java b/yudao-framework/yudao-spring-boot-starter-job/src/main/java/cn/iocoder/yudao/framework/quartz/config/YudaoAsyncAutoConfiguration.java new file mode 100644 index 0000000000..05b82b552c --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-job/src/main/java/cn/iocoder/yudao/framework/quartz/config/YudaoAsyncAutoConfiguration.java @@ -0,0 +1,36 @@ +package cn.iocoder.yudao.framework.quartz.config; + +import com.alibaba.ttl.TtlRunnable; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.config.BeanPostProcessor; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.annotation.EnableAsync; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; + +/** + * 异步任务 Configuration + */ +@Configuration +@EnableAsync +public class YudaoAsyncAutoConfiguration { + + @Bean + public BeanPostProcessor threadPoolTaskExecutorBeanPostProcessor() { + return new BeanPostProcessor() { + + @Override + public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { + if (!(bean instanceof ThreadPoolTaskExecutor)) { + return bean; + } + // 修改提交的任务,接入 TransmittableThreadLocal + ThreadPoolTaskExecutor executor = (ThreadPoolTaskExecutor) bean; + executor.setTaskDecorator(TtlRunnable::get); + return executor; + } + + }; + } + +} diff --git a/yudao-framework/yudao-spring-boot-starter-job/src/main/java/cn/iocoder/yudao/framework/quartz/config/YudaoQuartzAutoConfiguration.java b/yudao-framework/yudao-spring-boot-starter-job/src/main/java/cn/iocoder/yudao/framework/quartz/config/YudaoQuartzAutoConfiguration.java index 86c47fd0b6..144e4773d0 100644 --- a/yudao-framework/yudao-spring-boot-starter-job/src/main/java/cn/iocoder/yudao/framework/quartz/config/YudaoQuartzAutoConfiguration.java +++ b/yudao-framework/yudao-spring-boot-starter-job/src/main/java/cn/iocoder/yudao/framework/quartz/config/YudaoQuartzAutoConfiguration.java @@ -6,6 +6,9 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableScheduling; +/** + * 定时任务 Configuration + */ @Configuration @EnableScheduling // 开启 Spring 自带的定时任务 public class YudaoQuartzAutoConfiguration { diff --git a/yudao-framework/yudao-spring-boot-starter-job/src/main/java/cn/iocoder/yudao/framework/quartz/package-info.java b/yudao-framework/yudao-spring-boot-starter-job/src/main/java/cn/iocoder/yudao/framework/quartz/package-info.java index 69d681245b..cfd237b21a 100644 --- a/yudao-framework/yudao-spring-boot-starter-job/src/main/java/cn/iocoder/yudao/framework/quartz/package-info.java +++ b/yudao-framework/yudao-spring-boot-starter-job/src/main/java/cn/iocoder/yudao/framework/quartz/package-info.java @@ -1,5 +1,7 @@ /** - * 定时任务,采用 Quartz 实现进程内的任务执行。 + * 1. 定时任务,采用 Quartz 实现进程内的任务执行。 * 考虑到高可用,使用 Quartz 自带的 MySQL 集群方案。 + * + * 2. 异步任务,采用 Spring Async 异步执行。 */ package cn.iocoder.yudao.framework.quartz; diff --git a/yudao-framework/yudao-spring-boot-starter-job/src/main/resources/META-INF/spring.factories b/yudao-framework/yudao-spring-boot-starter-job/src/main/resources/META-INF/spring.factories index 553b6e3094..cecc4094c2 100644 --- a/yudao-framework/yudao-spring-boot-starter-job/src/main/resources/META-INF/spring.factories +++ b/yudao-framework/yudao-spring-boot-starter-job/src/main/resources/META-INF/spring.factories @@ -1,2 +1,3 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ - cn.iocoder.yudao.framework.quartz.config.YudaoQuartzAutoConfiguration + cn.iocoder.yudao.framework.quartz.config.YudaoQuartzAutoConfiguration,\ + cn.iocoder.yudao.framework.quartz.config.YudaoAsyncAutoConfiguration diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/framework/async/《芋道 Spring Boot 异步任务入门》.md b/yudao-framework/yudao-spring-boot-starter-job/《芋道 Spring Boot 异步任务入门》.md similarity index 100% rename from yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/framework/async/《芋道 Spring Boot 异步任务入门》.md rename to yudao-framework/yudao-spring-boot-starter-job/《芋道 Spring Boot 异步任务入门》.md diff --git a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/framework/async/config/AsyncConfiguration.java b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/framework/async/config/AsyncConfiguration.java deleted file mode 100644 index ed271220ca..0000000000 --- a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/framework/async/config/AsyncConfiguration.java +++ /dev/null @@ -1,9 +0,0 @@ -package cn.iocoder.yudao.userserver.framework.async.config; - -import org.springframework.context.annotation.Configuration; -import org.springframework.scheduling.annotation.EnableAsync; - -@Configuration -@EnableAsync -public class AsyncConfiguration { -} diff --git a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/framework/async/package-info.java b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/framework/async/package-info.java deleted file mode 100644 index a06351522b..0000000000 --- a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/framework/async/package-info.java +++ /dev/null @@ -1,4 +0,0 @@ -/** - * 异步执行,基于 Spring @Async 实现 - */ -package cn.iocoder.yudao.userserver.framework.async; diff --git a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/framework/async/《芋道 Spring Boot 异步任务入门》.md b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/framework/async/《芋道 Spring Boot 异步任务入门》.md deleted file mode 100644 index 5822b838c7..0000000000 --- a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/framework/async/《芋道 Spring Boot 异步任务入门》.md +++ /dev/null @@ -1 +0,0 @@ -