Supplementary dynamic thread pool example.

pull/161/head
chen.ma 3 years ago
parent d329900599
commit fedf02f011

@ -1,10 +1,17 @@
package com.github.dynamic.threadpool.example.config; package com.github.dynamic.threadpool.example.config;
import com.github.dynamic.threadpool.starter.core.DynamicThreadPool;
import com.github.dynamic.threadpool.starter.toolkit.thread.CustomThreadPoolExecutor;
import com.github.dynamic.threadpool.starter.toolkit.thread.ThreadPoolBuilder;
import com.github.dynamic.threadpool.starter.wrap.DynamicThreadPoolWrap; import com.github.dynamic.threadpool.starter.wrap.DynamicThreadPoolWrap;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import java.util.concurrent.ThreadPoolExecutor;
import static com.github.dynamic.threadpool.example.constant.GlobalTestConstant.*;
/** /**
* Thread pool config. * Thread pool config.
* *
@ -15,19 +22,27 @@ import org.springframework.context.annotation.Configuration;
@Configuration @Configuration
public class ThreadPoolConfig { public class ThreadPoolConfig {
/**
* {@link DynamicThreadPoolWrap} Server .
*
* @return
*/
@Bean @Bean
public DynamicThreadPoolWrap messageCenterConsumeThreadPool() { public DynamicThreadPoolWrap messageCenterConsumeThreadPool() {
return new DynamicThreadPoolWrap("message-consume"); return new DynamicThreadPoolWrap(MESSAGE_CONSUME);
}
@Bean
public DynamicThreadPoolWrap messageCenterProduceThreadPool() {
return new DynamicThreadPoolWrap("message-produce");
} }
/**
* {@link DynamicThreadPool} {@link CustomThreadPoolExecutor} Server .
* <p>
* 线, IOC {@link CustomThreadPoolExecutor}
*
* @return
*/
@Bean @Bean
public DynamicThreadPoolWrap customPool() { @DynamicThreadPool
return new DynamicThreadPoolWrap("custom-pool"); public ThreadPoolExecutor customThreadPoolExecutor() {
return ThreadPoolBuilder.builder().threadFactory(MESSAGE_PRODUCE).threadPoolId(MESSAGE_PRODUCE).isCustomPool(true).build();
} }
} }

@ -0,0 +1,16 @@
package com.github.dynamic.threadpool.example.constant;
/**
* Global test variables.
*
* @author chen.ma
* @date 2021/8/15 21:06
*/
public class GlobalTestConstant {
public static final String CUSTOM_POOL = "custom-pool";
public static final String MESSAGE_PRODUCE = "message-produce";
public static final String MESSAGE_CONSUME = "message-consume";
}

@ -11,7 +11,7 @@ import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import static com.github.dynamic.threadpool.example.inittest.GlobalTestConstant.TEST_THREAD_POOL_ID; import static com.github.dynamic.threadpool.example.constant.GlobalTestConstant.MESSAGE_PRODUCE;
/** /**
* Test alarm send message. * Test alarm send message.
@ -28,7 +28,7 @@ public class AlarmSendMessageTest {
public void alarmSendMessageTest() { public void alarmSendMessageTest() {
ScheduledExecutorService scheduledThreadPool = Executors.newSingleThreadScheduledExecutor(); ScheduledExecutorService scheduledThreadPool = Executors.newSingleThreadScheduledExecutor();
scheduledThreadPool.scheduleWithFixedDelay(() -> { scheduledThreadPool.scheduleWithFixedDelay(() -> {
DynamicThreadPoolWrap executorService = GlobalThreadPoolManage.getExecutorService(TEST_THREAD_POOL_ID); DynamicThreadPoolWrap executorService = GlobalThreadPoolManage.getExecutorService(MESSAGE_PRODUCE);
ThreadPoolExecutor poolExecutor = executorService.getPool(); ThreadPoolExecutor poolExecutor = executorService.getPool();
try { try {
poolExecutor.execute(() -> ThreadUtil.sleep(10240124)); poolExecutor.execute(() -> ThreadUtil.sleep(10240124));

@ -1,13 +0,0 @@
package com.github.dynamic.threadpool.example.inittest;
/**
* Global test variables.
*
* @author chen.ma
* @date 2021/8/15 21:06
*/
public class GlobalTestConstant {
public static final String TEST_THREAD_POOL_ID = "custom-pool";
}

@ -11,7 +11,7 @@ import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import static com.github.dynamic.threadpool.example.inittest.GlobalTestConstant.TEST_THREAD_POOL_ID; import static com.github.dynamic.threadpool.example.constant.GlobalTestConstant.MESSAGE_PRODUCE;
/** /**
* Test run time metrics. * Test run time metrics.
@ -30,7 +30,7 @@ public class RunStateHandlerTest {
ScheduledExecutorService scheduledThreadPool = Executors.newSingleThreadScheduledExecutor(); ScheduledExecutorService scheduledThreadPool = Executors.newSingleThreadScheduledExecutor();
scheduledThreadPool.scheduleAtFixedRate(() -> { scheduledThreadPool.scheduleAtFixedRate(() -> {
DynamicThreadPoolWrap executorService = GlobalThreadPoolManage.getExecutorService(TEST_THREAD_POOL_ID); DynamicThreadPoolWrap executorService = GlobalThreadPoolManage.getExecutorService(MESSAGE_PRODUCE);
ThreadPoolExecutor pool = executorService.getPool(); ThreadPoolExecutor pool = executorService.getPool();
try { try {
pool.execute(() -> { pool.execute(() -> {

Loading…
Cancel
Save