From e8485921bb369bdbcdd2252f1ac7f60e63fa896b Mon Sep 17 00:00:00 2001 From: weihubeats Date: Sun, 2 Oct 2022 16:16:27 +0800 Subject: [PATCH] Simplified DynamicThreadPool usage (#757) * Simplified DynamicThreadPool usage * Simplified DynamicThreadPool usage --- .../executor/SpringDynamicThreadPool.java | 36 +++++++++++++++++++ .../config/etcd/config/ThreadPoolConfig.java | 6 ++-- .../core/config/DynamicThreadPoolConfig.java | 18 +++++----- 3 files changed, 46 insertions(+), 14 deletions(-) create mode 100644 hippo4j-core/src/main/java/cn/hippo4j/core/executor/SpringDynamicThreadPool.java diff --git a/hippo4j-core/src/main/java/cn/hippo4j/core/executor/SpringDynamicThreadPool.java b/hippo4j-core/src/main/java/cn/hippo4j/core/executor/SpringDynamicThreadPool.java new file mode 100644 index 00000000..4c482897 --- /dev/null +++ b/hippo4j-core/src/main/java/cn/hippo4j/core/executor/SpringDynamicThreadPool.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cn.hippo4j.core.executor; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.springframework.context.annotation.Bean; + +/** + *@author : wh + *@date : 2022/10/2 16:10 + *@description: + */ +@Target(ElementType.METHOD) +@Retention(RetentionPolicy.RUNTIME) +@Bean +public @interface SpringDynamicThreadPool { +} diff --git a/hippo4j-example/hippo4j-config-etcd-spring-boot-starter-example/src/main/java/cn/hippo4j/example/config/etcd/config/ThreadPoolConfig.java b/hippo4j-example/hippo4j-config-etcd-spring-boot-starter-example/src/main/java/cn/hippo4j/example/config/etcd/config/ThreadPoolConfig.java index f8fcf79a..db3f1ad2 100644 --- a/hippo4j-example/hippo4j-config-etcd-spring-boot-starter-example/src/main/java/cn/hippo4j/example/config/etcd/config/ThreadPoolConfig.java +++ b/hippo4j-example/hippo4j-config-etcd-spring-boot-starter-example/src/main/java/cn/hippo4j/example/config/etcd/config/ThreadPoolConfig.java @@ -19,10 +19,9 @@ package cn.hippo4j.example.config.etcd.config; import java.util.concurrent.ThreadPoolExecutor; -import cn.hippo4j.core.executor.DynamicThreadPool; +import cn.hippo4j.core.executor.SpringDynamicThreadPool; import cn.hippo4j.core.executor.support.ThreadPoolBuilder; -import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** @@ -33,8 +32,7 @@ import org.springframework.context.annotation.Configuration; @Configuration public class ThreadPoolConfig { - @Bean - @DynamicThreadPool + @SpringDynamicThreadPool public ThreadPoolExecutor messageConsumeDynamicExecutor() { String threadPoolId = "message-consume"; return ThreadPoolBuilder.builderDynamicPoolById(threadPoolId); diff --git a/hippo4j-example/hippo4j-example-core/src/main/java/cn/hippo4j/example/core/config/DynamicThreadPoolConfig.java b/hippo4j-example/hippo4j-example-core/src/main/java/cn/hippo4j/example/core/config/DynamicThreadPoolConfig.java index c581863a..f59432ec 100644 --- a/hippo4j-example/hippo4j-example-core/src/main/java/cn/hippo4j/example/core/config/DynamicThreadPoolConfig.java +++ b/hippo4j-example/hippo4j-example-core/src/main/java/cn/hippo4j/example/core/config/DynamicThreadPoolConfig.java @@ -17,19 +17,19 @@ package cn.hippo4j.example.core.config; -import cn.hippo4j.core.executor.DynamicThreadPool; +import java.util.concurrent.Executor; +import java.util.concurrent.ThreadPoolExecutor; + +import cn.hippo4j.core.executor.SpringDynamicThreadPool; import cn.hippo4j.core.executor.support.ThreadPoolBuilder; import cn.hippo4j.example.core.handler.TaskTraceBuilderHandler; import cn.hippo4j.example.core.inittest.TaskDecoratorTest; import com.alibaba.ttl.threadpool.TtlExecutors; import lombok.extern.slf4j.Slf4j; -import org.springframework.context.annotation.Bean; + import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; -import java.util.concurrent.Executor; -import java.util.concurrent.ThreadPoolExecutor; - import static cn.hippo4j.common.constant.Constants.AVAILABLE_PROCESSORS; import static cn.hippo4j.example.core.constant.GlobalTestConstant.MESSAGE_CONSUME; import static cn.hippo4j.example.core.constant.GlobalTestConstant.MESSAGE_PRODUCE; @@ -41,8 +41,7 @@ import static cn.hippo4j.example.core.constant.GlobalTestConstant.MESSAGE_PRODUC @Configuration public class DynamicThreadPoolConfig { - @Bean - @DynamicThreadPool + @SpringDynamicThreadPool public Executor messageConsumeTtlDynamicThreadPool() { String threadPoolId = MESSAGE_CONSUME; ThreadPoolExecutor customExecutor = ThreadPoolBuilder.builder() @@ -59,8 +58,7 @@ public class DynamicThreadPoolConfig { return ttlExecutor; } - @Bean - @DynamicThreadPool + @SpringDynamicThreadPool public ThreadPoolExecutor messageProduceDynamicThreadPool() { String threadPoolId = MESSAGE_PRODUCE; ThreadPoolExecutor produceExecutor = ThreadPoolBuilder.builder() @@ -84,7 +82,7 @@ public class DynamicThreadPoolConfig { * @return */ // @Bean - @DynamicThreadPool + @SpringDynamicThreadPool public ThreadPoolTaskExecutor testSpringThreadPoolTaskExecutor() { ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor(); threadPoolTaskExecutor.setThreadNamePrefix("test-spring-task-executor_");