diff --git a/pom.xml b/pom.xml index 12942ca..1f5120d 100644 --- a/pom.xml +++ b/pom.xml @@ -28,6 +28,12 @@ spring-boot-starter-test test + + org.springframework.boot + spring-boot-configuration-processor + true + + @@ -45,6 +51,21 @@ mybatis-plus-boot-starter 3.5.3 + + org.projectlombok + lombok + + + net.sf.dozer + dozer + 5.5.1 + + + jcl-over-slf4j + org.slf4j + + + diff --git a/src/main/java/co/depsystem/application2/config/thread/AsynsThread.java b/src/main/java/co/depsystem/application2/config/thread/AsynsThread.java index 9d89a07..1a68b06 100644 --- a/src/main/java/co/depsystem/application2/config/thread/AsynsThread.java +++ b/src/main/java/co/depsystem/application2/config/thread/AsynsThread.java @@ -7,7 +7,50 @@ package co.depsystem.application2.config.thread; +import lombok.extern.slf4j.Slf4j; +import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.annotation.AsyncConfigurer; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; -public class AsynsThread { +import java.util.concurrent.Executor; +import java.util.concurrent.ThreadPoolExecutor; +/** + * 协程 + * 线程 + * + * @author adiao + */ +@Slf4j +@Configuration +public class AsynsThread implements AsyncConfigurer { + + private final ThreadConfig thread; + + public AsynsThread(ThreadConfig thread) { + this.thread = thread; + } + + @Override + public Executor getAsyncExecutor() { + + ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); + executor.setCorePoolSize(thread.getCorePoolSize()); + executor.setMaxPoolSize(thread.getMaxPoolSize()); + executor.setQueueCapacity(thread.getQueueCapacity()); + executor.setKeepAliveSeconds(thread.getKeepAliveSeconds()); + executor.setThreadNamePrefix("de-async-"); + executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); + executor.initialize(); + return executor; + } + + @Override + public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() { + return (throwable, method, objects) -> { + log.error("====" + throwable.getMessage() + "====", throwable); + log.error("exception method:" + method.getName()); + }; + } } diff --git a/src/main/java/co/depsystem/application2/config/thread/ThreadConfig.java b/src/main/java/co/depsystem/application2/config/thread/ThreadConfig.java new file mode 100644 index 0000000..386da09 --- /dev/null +++ b/src/main/java/co/depsystem/application2/config/thread/ThreadConfig.java @@ -0,0 +1,27 @@ +/** + * @author JOJO + * @class ThreadConfig + * @date 2023/3/6 + * @apiNote + */ + +package co.depsystem.application2.config.thread; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +@Data +@Component +@ConfigurationProperties("task.pool") +public class ThreadConfig { + + private int corePoolSize; + + private int maxPoolSize; + + private int keepAliveSeconds; + + private int queueCapacity; + +} diff --git a/src/main/java/co/depsystem/application2/config/thread/ThreadExecutorUtil.java b/src/main/java/co/depsystem/application2/config/thread/ThreadExecutorUtil.java new file mode 100644 index 0000000..2808fa3 --- /dev/null +++ b/src/main/java/co/depsystem/application2/config/thread/ThreadExecutorUtil.java @@ -0,0 +1,14 @@ +/** + * @author JOJO + * @class ThreadExecutorUtil + * @date 2023/3/6 + * @apiNote + */ + +package co.depsystem.application2.config.thread; + +public class ThreadExecutorUtil { + public static ThreadExecutorUtil start() { + ThreadConfig threadConfig = SpringContextHolder.getBean(AsynsThread.class) + } +} diff --git a/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/src/main/resources/META-INF/additional-spring-configuration-metadata.json new file mode 100644 index 0000000..9b1ee78 --- /dev/null +++ b/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -0,0 +1,8 @@ +{ + "properties": [ + { + "name": "task.pool.core-pool-size", + "type": "java.lang.String", + "description": "Description for task.pool.core-pool-size." + } +] } \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 4073fdf..4c3677c 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -7,3 +7,11 @@ spring.datasource.url=localhost:3306 spring.datasource.name=root spring.datasource.password=adiao +# 核心线程池大小 +task.pool.core-pool-size=10 +# 最大线程数 +task.pool.max-pool-size=10 +# 活跃时间 +task.pool.keep-alive-seconds=10 +# 队列容量 +task.pool.queue-capacity=10