From ead1136e3136d52f687ffb6ed02d7f30dca1a4f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=96=AF=E7=8B=82=E7=9A=84=E7=8B=AE=E5=AD=90li?= <15040126243@163.com> Date: Fri, 28 May 2021 14:24:45 +0800 Subject: [PATCH] =?UTF-8?q?add=20=E5=A2=9E=E5=8A=A0=20security=20=E6=9D=83?= =?UTF-8?q?=E9=99=90=E6=A1=86=E6=9E=B6=20@Async=20=E5=BC=82=E6=AD=A5?= =?UTF-8?q?=E6=B3=A8=E8=A7=A3=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ruoyi/framework/config/AsyncConfig.java | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 ruoyi-framework/src/main/java/com/ruoyi/framework/config/AsyncConfig.java diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/AsyncConfig.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/AsyncConfig.java new file mode 100644 index 00000000..56826020 --- /dev/null +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/AsyncConfig.java @@ -0,0 +1,41 @@ +package com.ruoyi.framework.config; + +import com.ruoyi.common.exception.CustomException; +import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.annotation.AsyncConfigurerSupport; +import org.springframework.scheduling.annotation.EnableAsync; +import org.springframework.security.concurrent.DelegatingSecurityContextExecutorService; + +import java.util.Arrays; +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; + +@EnableAsync +@Configuration +public class AsyncConfig extends AsyncConfigurerSupport { + + /** + * 异步执行需要使用权限框架自带的包装线程池 保证权限信息的传递 + */ + @Override + public Executor getAsyncExecutor() { + return new DelegatingSecurityContextExecutorService( + Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors())); + } + + /** + * 异步执行异常处理 + */ + @Override + public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() { + return (throwable, method, objects) -> { + throwable.printStackTrace(); + throw new CustomException( + "Exception message - " + throwable.getMessage() + + ", Method name - " + method.getName() + + ", Parameter value - " + Arrays.toString(objects)); + }; + } + +}