Optimize spring thread pool adaptation

pull/615/head
chen.ma 3 years ago
parent 58a944a248
commit 622e405481

@ -78,7 +78,12 @@ public class DynamicThreadPoolConfig {
return produceExecutor;
}
@Bean
/**
* Test spring {@link ThreadPoolTaskExecutor}, Thread-pool id: testSpringThreadPoolTaskExecutor
*
* @return
*/
// @Bean
@DynamicThreadPool
public ThreadPoolTaskExecutor testSpringThreadPoolTaskExecutor() {
ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();

@ -20,14 +20,15 @@ package cn.hippo4j.example.core.inittest;
import cn.hutool.core.thread.ThreadUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Random;
import java.util.concurrent.*;
import java.util.concurrent.Executor;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import static cn.hippo4j.common.constant.Constants.EXECUTE_TIMEOUT_TRACE;
@ -44,8 +45,8 @@ public class RunStateHandlerTest {
@Resource
private ThreadPoolExecutor messageProduceDynamicThreadPool;
@Resource
private ThreadPoolTaskExecutor testThreadPoolTaskExecutor;
/*@Resource
private ThreadPoolTaskExecutor testSpringThreadPoolTaskExecutor;*/
private final ThreadPoolExecutor runStateHandlerTestExecutor = new ThreadPoolExecutor(
4,
@ -68,7 +69,7 @@ public class RunStateHandlerTest {
// Start the dynamic thread pool to simulate running tasks
runTask(messageConsumeTtlDynamicThreadPool);
runTask(messageProduceDynamicThreadPool);
runTask(testThreadPoolTaskExecutor);
// runTask(testThreadPoolTaskExecutor);
// Dynamically register thread pool
ThreadPoolExecutor registerDynamicThreadPool = RegisterDynamicThreadPoolTest.registerDynamicThreadPool("auto-register-dynamic-thread-pool");
runTask(registerDynamicThreadPool);

@ -164,14 +164,15 @@ public final class DynamicThreadPoolPostProcessor implements BeanPostProcessor {
String threadNamePrefix = executorProperties.getThreadNamePrefix();
ExecutorProperties newExecutorProperties = buildExecutorProperties(executorProperties);
ThreadPoolExecutor newDynamicPoolExecutor = ThreadPoolBuilder.builder()
.dynamicPool()
.threadPoolId(executorProperties.getThreadPoolId())
.threadFactory(StringUtil.isNotBlank(threadNamePrefix) ? threadNamePrefix : executorProperties.getThreadPoolId())
.poolThreadSize(newExecutorProperties.getCorePoolSize(), newExecutorProperties.getMaximumPoolSize())
.workQueue(BlockingQueueTypeEnum.createBlockingQueue(newExecutorProperties.getBlockingQueue(), newExecutorProperties.getQueueCapacity()))
.threadFactory(StringUtil.isNotBlank(threadNamePrefix) ? threadNamePrefix : executorProperties.getThreadPoolId())
.executeTimeOut(newExecutorProperties.getExecuteTimeOut())
.keepAliveTime(newExecutorProperties.getKeepAliveTime(), TimeUnit.SECONDS)
.rejected(RejectedPolicyTypeEnum.createPolicy(newExecutorProperties.getRejectedHandler()))
.allowCoreThreadTimeOut(newExecutorProperties.getAllowCoreThreadTimeOut())
.dynamicPool()
.build();
return newDynamicPoolExecutor;
}

Loading…
Cancel
Save