From 553151432576c29487dc1d8d01c4f03c2b5e8e05 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: Sun, 27 Feb 2022 22:59:47 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E5=A2=9E=E5=8A=A0=20@Async=20?= =?UTF-8?q?=E5=BC=82=E6=AD=A5=E6=B3=A8=E8=A7=A3=20=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E7=BA=BF=E7=A8=8B=E6=B1=A0=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ruoyi/common/core/config/AsyncConfig.java | 93 +++++++++++++++++++ .../main/resources/META-INF/spring.factories | 3 +- 2 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/config/AsyncConfig.java diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/config/AsyncConfig.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/config/AsyncConfig.java new file mode 100644 index 00000000..02b18d04 --- /dev/null +++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/config/AsyncConfig.java @@ -0,0 +1,93 @@ +package com.ruoyi.common.core.config; + +import cn.hutool.core.util.ArrayUtil; +import com.ruoyi.common.core.exception.ServiceException; +import com.ruoyi.common.core.utils.SpringUtils; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.concurrent.BasicThreadFactory; +import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.annotation.AsyncConfigurerSupport; +import org.springframework.scheduling.annotation.EnableAsync; + +import java.util.Arrays; +import java.util.concurrent.*; + +/** + * 异步配置 + * + * @author Lion Li + */ +@Slf4j +@EnableAsync +@Configuration +public class AsyncConfig extends AsyncConfigurerSupport { + + private static final int CORE_POOL_SIZE = 10; + + /** + * 执行周期性或定时任务 + */ + @Bean(name = "scheduledExecutorService") + public ScheduledExecutorService scheduledExecutorService() { + return new ScheduledThreadPoolExecutor(CORE_POOL_SIZE, + new BasicThreadFactory.Builder().namingPattern("schedule-pool-%d").daemon(true).build(), + new ThreadPoolExecutor.CallerRunsPolicy()) { + @Override + protected void afterExecute(Runnable r, Throwable t) { + super.afterExecute(r, t); + printException(r, t); + } + }; + } + + /** + * 自定义 @Async 注解使用系统线程池 + */ + @Override + public Executor getAsyncExecutor() { + return SpringUtils.getBean("scheduledExecutorService"); + } + + /** + * 异步执行异常处理 + */ + @Override + public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() { + return (throwable, method, objects) -> { + throwable.printStackTrace(); + StringBuilder sb = new StringBuilder(); + sb.append("Exception message - ").append(throwable.getMessage()) + .append(", Method name - ").append(method.getName()); + if (ArrayUtil.isNotEmpty(objects)) { + sb.append(", Parameter value - ").append(Arrays.toString(objects)); + } + throw new ServiceException(sb.toString()); + }; + } + + /** + * 打印线程异常信息 + */ + public void printException(Runnable r, Throwable t) { + if (t == null && r instanceof Future) { + try { + Future future = (Future) r; + if (future.isDone()) { + future.get(); + } + } catch (CancellationException ce) { + t = ce; + } catch (ExecutionException ee) { + t = ee.getCause(); + } catch (InterruptedException ie) { + Thread.currentThread().interrupt(); + } + } + if (t != null) { + log.error(t.getMessage(), t); + } + } + +} diff --git a/ruoyi-common/ruoyi-common-core/src/main/resources/META-INF/spring.factories b/ruoyi-common/ruoyi-common-core/src/main/resources/META-INF/spring.factories index 2cceb286..2241e9b8 100644 --- a/ruoyi-common/ruoyi-common-core/src/main/resources/META-INF/spring.factories +++ b/ruoyi-common/ruoyi-common-core/src/main/resources/META-INF/spring.factories @@ -2,6 +2,7 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.ruoyi.common.core.utils.SpringUtils,\ com.ruoyi.common.core.config.ApplicationConfig,\ com.ruoyi.common.core.config.JacksonConfig,\ - com.ruoyi.common.core.config.ValidatorConfig + com.ruoyi.common.core.config.ValidatorConfig,\ + com.ruoyi.common.core.config.AsyncConfig