Skip to content

Commit

Permalink
优化动态线程池示例项目.
Browse files Browse the repository at this point in the history
  • Loading branch information
magestacks committed Mar 13, 2022
1 parent cb842fa commit c2c0689
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -25,44 +23,42 @@
@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;
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -25,21 +24,21 @@
public class RunStateHandlerTest {

@Resource
private DynamicThreadPoolWrapper messageCenterDynamicThreadPool;
private ThreadPoolExecutor messageConsumeDynamicThreadPool;

@Resource
private ThreadPoolExecutor dynamicThreadPoolExecutor;
private ThreadPoolExecutor messageProduceDynamicThreadPool;

@PostConstruct
@SuppressWarnings("all")
public void runStateHandlerTest() {
log.info("Test thread pool runtime state interface...");

// 启动动态线程池模拟运行任务
runTask(messageCenterDynamicThreadPool.getExecutor());
runTask(messageConsumeDynamicThreadPool);

// 启动动态线程池模拟运行任务
runTask(dynamicThreadPoolExecutor);
runTask(messageProduceDynamicThreadPool);
}

private void runTask(ExecutorService executorService) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void taskDecoratorTest() {
threadPoolExecutor.execute(() -> {
/**
* 此处打印不为空, taskDecorator 即为生效.
* taskDecorator 配置查看 {@link ThreadPoolConfig#messageCenterDynamicThreadPool()}
* taskDecorator 配置查看 {@link ThreadPoolConfig#messageConsumeDynamicThreadPool()}
*/
log.info("通过 taskDecorator MDC 传递上下文 :: {}", MDC.get(PLACEHOLDER));
});
Expand Down

0 comments on commit c2c0689

Please sign in to comment.