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

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

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

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

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

Loading…
Cancel
Save