动态线程池添加测试用例.

pull/12/head
chen.ma 3 years ago
parent 7f4763ff51
commit 4c6fa752b9

@ -1,5 +1,6 @@
package cn.hippo4j.example.config;
import cn.hippo4j.example.inittest.TaskDecoratorTest;
import cn.hippo4j.starter.core.DynamicThreadPool;
import cn.hippo4j.starter.core.DynamicThreadPoolExecutor;
import cn.hippo4j.starter.toolkit.thread.ThreadPoolBuilder;
@ -10,7 +11,8 @@ import org.springframework.context.annotation.Configuration;
import java.util.concurrent.ThreadPoolExecutor;
import static cn.hippo4j.example.constant.GlobalTestConstant.*;
import static cn.hippo4j.example.constant.GlobalTestConstant.MESSAGE_CONSUME;
import static cn.hippo4j.example.constant.GlobalTestConstant.MESSAGE_PRODUCE;
/**
* Thread pool config.
@ -49,6 +51,13 @@ public class ThreadPoolConfig {
return ThreadPoolBuilder.builder()
.threadFactory(MESSAGE_PRODUCE)
.dynamicPool()
/**
* 线.
* , {@link TaskDecoratorTest}
*/
.waitForTasksToCompleteOnShutdown(true)
.awaitTerminationMillis(5000)
.taskDecorator(new TaskDecoratorTest.ContextCopyingDecorator())
.build();
}

@ -0,0 +1,61 @@
package cn.hippo4j.example.inittest;
import cn.hippo4j.example.constant.GlobalTestConstant;
import cn.hippo4j.starter.core.GlobalThreadPoolManage;
import cn.hippo4j.starter.wrapper.DynamicThreadPoolWrapper;
import cn.hutool.core.thread.ThreadUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.core.task.TaskDecorator;
import org.springframework.stereotype.Component;
import java.util.concurrent.ThreadPoolExecutor;
/**
* TaskDecorator test.
*
* @author chen.ma
* @date 2021/11/28 13:01
*/
@Slf4j
@Component
public class TaskDecoratorTest {
public static final String PLACEHOLDER = "site";
// @PostConstruct
public void taskDecoratorTest() {
new Thread(() -> {
MDC.put(PLACEHOLDER, "查看官网: https://www.hippox.cn");
ThreadUtil.sleep(5000);
DynamicThreadPoolWrapper poolWrapper = GlobalThreadPoolManage.getExecutorService(GlobalTestConstant.MESSAGE_PRODUCE);
ThreadPoolExecutor threadPoolExecutor = poolWrapper.getExecutor();
threadPoolExecutor.execute(() -> {
/**
* , taskDecorator .
* taskDecorator {@link ThreadPoolConfig#messageCenterDynamicThreadPool()}
*/
log.info("通过 taskDecorator MDC 传递上下文 :: {}", MDC.get(PLACEHOLDER));
});
}).start();
}
public static class ContextCopyingDecorator implements TaskDecorator {
@Override
public Runnable decorate(Runnable runnable) {
String placeholderVal = MDC.get(PLACEHOLDER);
// other context...
return () -> {
try {
MDC.put(PLACEHOLDER, placeholderVal);
runnable.run();
} finally {
MDC.clear();
}
};
}
}
}
Loading…
Cancel
Save