Refactoring the sample module tester.

pull/161/head
chen.ma 3 years ago
parent 984a7d44f2
commit 463407c19f

@ -1,17 +1,10 @@
package com.github.dynamic.threadpool.example.config; package com.github.dynamic.threadpool.example.config;
import com.github.dynamic.threadpool.starter.core.GlobalThreadPoolManage;
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.Random;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/** /**
* 线 * 线
* *
@ -22,57 +15,19 @@ import java.util.concurrent.TimeUnit;
@Configuration @Configuration
public class ThreadPoolConfig { public class ThreadPoolConfig {
private String messageConsumePrefix = "message-consume";
private String messageProducePrefix = "message-produce";
private String customPoolPrefix = "custom-pool";
@Bean @Bean
public DynamicThreadPoolWrap messageCenterConsumeThreadPool() { public DynamicThreadPoolWrap messageCenterConsumeThreadPool() {
return new DynamicThreadPoolWrap(messageConsumePrefix); return new DynamicThreadPoolWrap("message-consume");
} }
@Bean @Bean
public DynamicThreadPoolWrap messageCenterProduceThreadPool() { public DynamicThreadPoolWrap messageCenterProduceThreadPool() {
return new DynamicThreadPoolWrap(messageProducePrefix); return new DynamicThreadPoolWrap("message-produce");
} }
@Bean @Bean
public DynamicThreadPoolWrap customPool() { public DynamicThreadPoolWrap customPool() {
return new DynamicThreadPoolWrap(customPoolPrefix); return new DynamicThreadPoolWrap("custom-pool");
} }
// @PostConstruct
@SuppressWarnings("all")
public void testExecuteTask() {
log.info("测试线程池运行时状态接口, 30s 后开始触发拒绝策略...");
ScheduledExecutorService scheduledThreadPool = Executors.newSingleThreadScheduledExecutor();
scheduledThreadPool.scheduleAtFixedRate(() -> {
DynamicThreadPoolWrap executorService = GlobalThreadPoolManage.getExecutorService(customPoolPrefix);
ThreadPoolExecutor pool = executorService.getPool();
try {
pool.execute(() -> {
log.info("线程池名称 :: {}, 正在执行即将进入阻塞...", Thread.currentThread().getName());
try {
int maxRandom = 10;
int temp = 2;
Random random = new Random();
// 这里为了赋值线程池 completedTaskCount
if (random.nextInt(maxRandom) % temp == 0) {
Thread.sleep(10241024);
} else {
Thread.sleep(3000);
}
} catch (InterruptedException e) {
// ignore
}
});
} catch (Exception ex) {
// ignore
}
}, 5, 2, TimeUnit.SECONDS);
}
} }

@ -0,0 +1,41 @@
package com.github.dynamic.threadpool.example.inittest;
import cn.hutool.core.thread.ThreadUtil;
import com.github.dynamic.threadpool.starter.core.GlobalThreadPoolManage;
import com.github.dynamic.threadpool.starter.wrap.DynamicThreadPoolWrap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.util.concurrent.Executors;
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;
/**
* Test alarm send message.
*
* @author chen.ma
* @date 2021/8/15 21:03
*/
@Slf4j
@Component
public class AlarmSendMessageTest {
// @PostConstruct
@SuppressWarnings("all")
public void alarmSendMessageTest() {
ScheduledExecutorService scheduledThreadPool = Executors.newSingleThreadScheduledExecutor();
scheduledThreadPool.scheduleWithFixedDelay(() -> {
DynamicThreadPoolWrap executorService = GlobalThreadPoolManage.getExecutorService(TEST_THREAD_POOL_ID);
ThreadPoolExecutor poolExecutor = executorService.getPool();
try {
poolExecutor.execute(() -> ThreadUtil.sleep(10240124));
} catch (Exception ex) {
log.error("抛出拒绝策略", ex.getMessage());
}
}, 3, 1, TimeUnit.SECONDS);
}
}

@ -0,0 +1,13 @@
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";
}

@ -0,0 +1,58 @@
package com.github.dynamic.threadpool.example.inittest;
import com.github.dynamic.threadpool.starter.core.GlobalThreadPoolManage;
import com.github.dynamic.threadpool.starter.wrap.DynamicThreadPoolWrap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.util.Random;
import java.util.concurrent.Executors;
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;
/**
* Test run time metrics.
*
* @author chen.ma
* @date 2021/8/15 21:00
*/
@Slf4j
@Component
public class RunStateHandlerTest {
// @PostConstruct
@SuppressWarnings("all")
public void runStateHandlerTest() {
log.info("测试线程池运行时状态接口, 30s 后开始触发拒绝策略...");
ScheduledExecutorService scheduledThreadPool = Executors.newSingleThreadScheduledExecutor();
scheduledThreadPool.scheduleAtFixedRate(() -> {
DynamicThreadPoolWrap executorService = GlobalThreadPoolManage.getExecutorService(TEST_THREAD_POOL_ID);
ThreadPoolExecutor pool = executorService.getPool();
try {
pool.execute(() -> {
log.info("线程池名称 :: {}, 正在执行即将进入阻塞...", Thread.currentThread().getName());
try {
int maxRandom = 10;
int temp = 2;
Random random = new Random();
// 这里为了赋值线程池 completedTaskCount
if (random.nextInt(maxRandom) % temp == 0) {
Thread.sleep(10241024);
} else {
Thread.sleep(3000);
}
} catch (InterruptedException e) {
// ignore
}
});
} catch (Exception ex) {
// ignore
}
}, 5, 2, TimeUnit.SECONDS);
}
}
Loading…
Cancel
Save