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;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.concurrent.ThreadPoolExecutor;
import static com.github.dynamic.threadpool.example.constant.GlobalTestConstant.*;
/**
* Thread pool config.
*
@ -15,19 +22,27 @@ import org.springframework.context.annotation.Configuration;
@Configuration
public class ThreadPoolConfig {
/**
* {@link DynamicThreadPoolWrap} Server .
*
* @return
*/
@Bean
public DynamicThreadPoolWrap messageCenterConsumeThreadPool() {
return new DynamicThreadPoolWrap("message-consume");
}
@Bean
public DynamicThreadPoolWrap messageCenterProduceThreadPool() {
return new DynamicThreadPoolWrap("message-produce");
return new DynamicThreadPoolWrap(MESSAGE_CONSUME);
}
/**
* {@link DynamicThreadPool} {@link CustomThreadPoolExecutor} Server .
* <p>
* 线, IOC {@link CustomThreadPoolExecutor}
*
* @return
*/
@Bean
public DynamicThreadPoolWrap customPool() {
return new DynamicThreadPoolWrap("custom-pool");
@DynamicThreadPool
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.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.
@ -28,7 +28,7 @@ public class AlarmSendMessageTest {
public void alarmSendMessageTest() {
ScheduledExecutorService scheduledThreadPool = Executors.newSingleThreadScheduledExecutor();
scheduledThreadPool.scheduleWithFixedDelay(() -> {
DynamicThreadPoolWrap executorService = GlobalThreadPoolManage.getExecutorService(TEST_THREAD_POOL_ID);
DynamicThreadPoolWrap executorService = GlobalThreadPoolManage.getExecutorService(MESSAGE_PRODUCE);
ThreadPoolExecutor poolExecutor = executorService.getPool();
try {
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.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.
@ -30,7 +30,7 @@ public class RunStateHandlerTest {
ScheduledExecutorService scheduledThreadPool = Executors.newSingleThreadScheduledExecutor();
scheduledThreadPool.scheduleAtFixedRate(() -> {
DynamicThreadPoolWrap executorService = GlobalThreadPoolManage.getExecutorService(TEST_THREAD_POOL_ID);
DynamicThreadPoolWrap executorService = GlobalThreadPoolManage.getExecutorService(MESSAGE_PRODUCE);
ThreadPoolExecutor pool = executorService.getPool();
try {
pool.execute(() -> {

Loading…
Cancel
Save