优化动态线程池示例项目.

pull/142/head
chen.ma 3 years ago
parent cb842fa083
commit c2c06899e9

@ -1,8 +1,6 @@
package cn.hippo4j.example.core.config; package cn.hippo4j.example.core.config;
import cn.hippo4j.core.executor.DynamicThreadPool; import cn.hippo4j.core.executor.DynamicThreadPool;
import cn.hippo4j.core.executor.DynamicThreadPoolExecutor;
import cn.hippo4j.core.executor.DynamicThreadPoolWrapper;
import cn.hippo4j.core.executor.support.ThreadPoolBuilder; import cn.hippo4j.core.executor.support.ThreadPoolBuilder;
import cn.hippo4j.example.core.handler.TaskTraceBuilderHandler; import cn.hippo4j.example.core.handler.TaskTraceBuilderHandler;
import cn.hippo4j.example.core.inittest.TaskDecoratorTest; import cn.hippo4j.example.core.inittest.TaskDecoratorTest;
@ -25,44 +23,42 @@ import static cn.hippo4j.example.core.constant.GlobalTestConstant.MESSAGE_PRODUC
@Configuration @Configuration
public class ThreadPoolConfig { public class ThreadPoolConfig {
/**
* {@link DynamicThreadPoolWrapper} Server .
*
* @return
*/
@Bean @Bean
public DynamicThreadPoolWrapper messageCenterDynamicThreadPool() { @DynamicThreadPool
public ThreadPoolExecutor messageConsumeDynamicThreadPool() {
String threadPoolId = MESSAGE_CONSUME;
ThreadPoolExecutor customExecutor = ThreadPoolBuilder.builder() ThreadPoolExecutor customExecutor = ThreadPoolBuilder.builder()
.dynamicPool() .dynamicPool()
.threadFactory(threadPoolId)
.threadPoolId(threadPoolId)
.executeTimeOut(800L) .executeTimeOut(800L)
.waitForTasksToCompleteOnShutdown(true)
.awaitTerminationMillis(5000L)
.taskDecorator(new TaskTraceBuilderHandler()) .taskDecorator(new TaskTraceBuilderHandler())
.threadFactory(MESSAGE_CONSUME)
.build(); .build();
return new DynamicThreadPoolWrapper(MESSAGE_CONSUME, customExecutor); return customExecutor;
} }
/**
* {@link DynamicThreadPool} {@link DynamicThreadPoolExecutor} Server .
* <p>
* 线, IOC {@link DynamicThreadPoolExecutor}
*
* @return
*/
@Bean @Bean
@DynamicThreadPool @DynamicThreadPool
public ThreadPoolExecutor dynamicThreadPoolExecutor() { public ThreadPoolExecutor messageProduceDynamicThreadPool() {
return ThreadPoolBuilder.builder() String threadPoolId = MESSAGE_PRODUCE;
.threadFactory(MESSAGE_PRODUCE) ThreadPoolExecutor produceExecutor = ThreadPoolBuilder.builder()
.dynamicPool() .dynamicPool()
.threadFactory(threadPoolId)
.threadPoolId(threadPoolId)
.executeTimeOut(900L)
.waitForTasksToCompleteOnShutdown(true)
.awaitTerminationMillis(5000L)
/** /**
* 线. * 线.
* , {@link TaskDecoratorTest} * , {@link TaskDecoratorTest}
*/ */
.waitForTasksToCompleteOnShutdown(true)
.awaitTerminationMillis(5000)
.taskDecorator(new TaskDecoratorTest.ContextCopyingDecorator()) .taskDecorator(new TaskDecoratorTest.ContextCopyingDecorator())
.build(); .build();
return produceExecutor;
} }
} }

@ -1,6 +1,5 @@
package cn.hippo4j.example.core.inittest; package cn.hippo4j.example.core.inittest;
import cn.hippo4j.core.executor.DynamicThreadPoolWrapper;
import cn.hutool.core.thread.ThreadUtil; import cn.hutool.core.thread.ThreadUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC; import org.slf4j.MDC;
@ -25,10 +24,10 @@ import static cn.hippo4j.common.constant.Constants.EXECUTE_TIMEOUT_TRACE;
public class RunStateHandlerTest { public class RunStateHandlerTest {
@Resource @Resource
private DynamicThreadPoolWrapper messageCenterDynamicThreadPool; private ThreadPoolExecutor messageConsumeDynamicThreadPool;
@Resource @Resource
private ThreadPoolExecutor dynamicThreadPoolExecutor; private ThreadPoolExecutor messageProduceDynamicThreadPool;
@PostConstruct @PostConstruct
@SuppressWarnings("all") @SuppressWarnings("all")
@ -36,10 +35,10 @@ public class RunStateHandlerTest {
log.info("Test thread pool runtime state interface..."); log.info("Test thread pool runtime state interface...");
// 启动动态线程池模拟运行任务 // 启动动态线程池模拟运行任务
runTask(messageCenterDynamicThreadPool.getExecutor()); runTask(messageConsumeDynamicThreadPool);
// 启动动态线程池模拟运行任务 // 启动动态线程池模拟运行任务
runTask(dynamicThreadPoolExecutor); runTask(messageProduceDynamicThreadPool);
} }
private void runTask(ExecutorService executorService) { private void runTask(ExecutorService executorService) {

@ -36,7 +36,7 @@ public class TaskDecoratorTest {
threadPoolExecutor.execute(() -> { threadPoolExecutor.execute(() -> {
/** /**
* , taskDecorator . * , taskDecorator .
* taskDecorator {@link ThreadPoolConfig#messageCenterDynamicThreadPool()} * taskDecorator {@link ThreadPoolConfig#messageConsumeDynamicThreadPool()}
*/ */
log.info("通过 taskDecorator MDC 传递上下文 :: {}", MDC.get(PLACEHOLDER)); log.info("通过 taskDecorator MDC 传递上下文 :: {}", MDC.get(PLACEHOLDER));
}); });

Loading…
Cancel
Save