feat: 功能持续更新.

pull/161/head
chen.ma 4 years ago
parent e21ac470b0
commit e02e528a88

@ -1,4 +1,4 @@
package io.dynamic.threadpool.starter.core; package io.dynamic.threadpool.starter.banner;
import org.springframework.boot.ansi.AnsiColor; import org.springframework.boot.ansi.AnsiColor;
import org.springframework.boot.ansi.AnsiOutput; import org.springframework.boot.ansi.AnsiOutput;
@ -14,25 +14,27 @@ public class DynamicThreadPoolBanner {
private static final String DYNAMIC_THREAD_POOL = " :: Dynamic ThreadPool :: "; private static final String DYNAMIC_THREAD_POOL = " :: Dynamic ThreadPool :: ";
private static final int STRAP_LINE_SIZE = 42; private static final int STRAP_LINE_SIZE = 50;
public static void printBanner() { public static void printBanner(boolean isBanner) {
String banner = "\n___ _ _____ ___ \n" + String banner = "\n___ _ _____ ___ \n" +
"| \\ _ _ _ _ __ _ _ __ (_)__ |_ _| _ \\\n" + "| \\ _ _ _ _ __ _ _ __ (_)__ |_ _| _ \\\n" +
"| |) | || | ' \\/ _` | ' \\| / _| | | | _/\n" + "| |) | || | ' \\/ _` | ' \\| / _| | | | _/\n" +
"|___/ \\_, |_||_\\__,_|_|_|_|_\\__| |_| |_| \n" + "|___/ \\_, |_||_\\__,_|_|_|_|_\\__| |_| |_| \n" +
" |__/ \n"; " |__/ \n";
String version = getVersion(); if (isBanner) {
version = (version != null) ? " (v" + version + ")" : "no version."; String version = getVersion();
version = (version != null) ? " (v" + version + ")" : "no version.";
StringBuilder padding = new StringBuilder(); StringBuilder padding = new StringBuilder();
while (padding.length() < STRAP_LINE_SIZE - (version.length() + DYNAMIC_THREAD_POOL.length())) { while (padding.length() < STRAP_LINE_SIZE - (version.length() + DYNAMIC_THREAD_POOL.length())) {
padding.append(" "); padding.append(" ");
} }
System.out.println(AnsiOutput.toString(banner, AnsiColor.GREEN, DYNAMIC_THREAD_POOL, AnsiColor.DEFAULT, System.out.println(AnsiOutput.toString(banner, AnsiColor.GREEN, DYNAMIC_THREAD_POOL, AnsiColor.DEFAULT,
padding.toString(), AnsiStyle.FAINT, version, "\n")); padding.toString(), AnsiStyle.FAINT, version, "\n"));
}
} }
public static String getVersion() { public static String getVersion() {

@ -37,5 +37,10 @@ public class DynamicThreadPoolProperties {
/** /**
* 线 * 线
*/ */
private String enabled; private boolean enabled;
/**
* banner
*/
private boolean banner;
} }

@ -10,7 +10,7 @@ import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
/** /**
* CacheData. * Cache Data.
* *
* @author chen.ma * @author chen.ma
* @date 2021/6/22 20:46 * @date 2021/6/22 20:46

@ -9,6 +9,7 @@ import io.dynamic.threadpool.common.toolkit.GroupKey;
import io.dynamic.threadpool.common.web.base.Result; import io.dynamic.threadpool.common.web.base.Result;
import io.dynamic.threadpool.starter.core.CacheData; import io.dynamic.threadpool.starter.core.CacheData;
import io.dynamic.threadpool.starter.remote.HttpAgent; import io.dynamic.threadpool.starter.remote.HttpAgent;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@ -98,12 +99,19 @@ public class ClientWorker {
class LongPollingRunnable implements Runnable { class LongPollingRunnable implements Runnable {
@Override @Override
@SneakyThrows
public void run() { public void run() {
List<CacheData> cacheDataList = new ArrayList(); List<CacheData> cacheDataList = new ArrayList();
List<CacheData> queryCacheDataList = cacheMap.entrySet() List<CacheData> queryCacheDataList = cacheMap.entrySet()
.stream().map(each -> each.getValue()).collect(Collectors.toList()); .stream().map(each -> each.getValue()).collect(Collectors.toList());
List<String> changedTpIds = checkUpdateDataIds(queryCacheDataList); List<String> changedTpIds = null;
try {
changedTpIds = checkUpdateDataIds(queryCacheDataList);
} catch (Exception ex) {
log.error("[Long polling] Error. exception message :: {}, Thread sleep 30 s.", ex.getMessage(), ex);
Thread.sleep(30000);
}
if (!CollectionUtils.isEmpty(changedTpIds)) { if (!CollectionUtils.isEmpty(changedTpIds)) {
log.info("[dynamic threadPool] tpIds changed :: {}", changedTpIds); log.info("[dynamic threadPool] tpIds changed :: {}", changedTpIds);
for (String each : changedTpIds) { for (String each : changedTpIds) {
@ -281,6 +289,7 @@ public class ClientWorker {
cacheData = new CacheData(namespace, itemId, tpId); cacheData = new CacheData(namespace, itemId, tpId);
CacheData lastCacheData = cacheMap.putIfAbsent(tpId, cacheData); CacheData lastCacheData = cacheMap.putIfAbsent(tpId, cacheData);
if (lastCacheData == null) { if (lastCacheData == null) {
// TODO 连接不到 server 端报错
String serverConfig = getServerConfig(namespace, itemId, tpId, 3000L); String serverConfig = getServerConfig(namespace, itemId, tpId, 3000L);
PoolParameterInfo poolInfo = JSON.parseObject(serverConfig, PoolParameterInfo.class); PoolParameterInfo poolInfo = JSON.parseObject(serverConfig, PoolParameterInfo.class);
cacheData.setContent(ContentUtil.getPoolContent(poolInfo)); cacheData.setContent(ContentUtil.getPoolContent(poolInfo));

@ -5,14 +5,15 @@ import io.dynamic.threadpool.common.config.ApplicationContextHolder;
import io.dynamic.threadpool.common.constant.Constants; import io.dynamic.threadpool.common.constant.Constants;
import io.dynamic.threadpool.common.model.PoolParameterInfo; import io.dynamic.threadpool.common.model.PoolParameterInfo;
import io.dynamic.threadpool.common.web.base.Result; import io.dynamic.threadpool.common.web.base.Result;
import io.dynamic.threadpool.starter.banner.DynamicThreadPoolBanner;
import io.dynamic.threadpool.starter.common.CommonThreadPool; import io.dynamic.threadpool.starter.common.CommonThreadPool;
import io.dynamic.threadpool.starter.config.DynamicThreadPoolProperties; import io.dynamic.threadpool.starter.config.DynamicThreadPoolProperties;
import io.dynamic.threadpool.starter.core.DynamicThreadPoolBanner;
import io.dynamic.threadpool.starter.core.GlobalThreadPoolManage; import io.dynamic.threadpool.starter.core.GlobalThreadPoolManage;
import io.dynamic.threadpool.starter.remote.HttpAgent; import io.dynamic.threadpool.starter.remote.HttpAgent;
import io.dynamic.threadpool.starter.remote.ServerHttpAgent; import io.dynamic.threadpool.starter.remote.ServerHttpAgent;
import io.dynamic.threadpool.starter.toolkit.BlockingQueueUtil; import io.dynamic.threadpool.starter.toolkit.BlockingQueueUtil;
import io.dynamic.threadpool.starter.wrap.DynamicThreadPoolWrap; import io.dynamic.threadpool.starter.wrap.DynamicThreadPoolWrap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
@ -28,18 +29,19 @@ import java.util.concurrent.TimeUnit;
* @author chen.ma * @author chen.ma
* @date 2021/6/20 16:34 * @date 2021/6/20 16:34
*/ */
@Slf4j
public class ThreadPoolRunListener { public class ThreadPoolRunListener {
private final DynamicThreadPoolProperties dynamicThreadPoolProperties; private final DynamicThreadPoolProperties properties;
public ThreadPoolRunListener(DynamicThreadPoolProperties properties) { public ThreadPoolRunListener(DynamicThreadPoolProperties properties) {
this.dynamicThreadPoolProperties = properties; this.properties = properties;
} }
@Order(1024) @Order(1024)
@PostConstruct @PostConstruct
public void run() { public void run() {
DynamicThreadPoolBanner.printBanner(); DynamicThreadPoolBanner.printBanner(properties.isBanner());
Map<String, DynamicThreadPoolWrap> executorMap = Map<String, DynamicThreadPoolWrap> executorMap =
ApplicationContextHolder.getBeansOfType(DynamicThreadPoolWrap.class); ApplicationContextHolder.getBeansOfType(DynamicThreadPoolWrap.class);
@ -47,19 +49,25 @@ public class ThreadPoolRunListener {
executorMap.forEach((key, val) -> { executorMap.forEach((key, val) -> {
Map<String, String> queryStrMap = new HashMap(3); Map<String, String> queryStrMap = new HashMap(3);
queryStrMap.put("tpId", val.getTpId()); queryStrMap.put("tpId", val.getTpId());
queryStrMap.put("itemId", dynamicThreadPoolProperties.getItemId()); queryStrMap.put("itemId", properties.getItemId());
queryStrMap.put("namespace", dynamicThreadPoolProperties.getNamespace()); queryStrMap.put("namespace", properties.getNamespace());
PoolParameterInfo ppi = null; PoolParameterInfo ppi = new PoolParameterInfo();
HttpAgent httpAgent = new ServerHttpAgent(dynamicThreadPoolProperties); HttpAgent httpAgent = new ServerHttpAgent(properties);
Result result = httpAgent.httpGet(Constants.CONFIG_CONTROLLER_PATH, null, queryStrMap, 3000L); Result result = null;
if (result.isSuccess() && (ppi = JSON.toJavaObject((JSON) result.getData(), PoolParameterInfo.class)) != null) { try {
// 使用相关参数创建线程池 result = httpAgent.httpGet(Constants.CONFIG_CONTROLLER_PATH, null, queryStrMap, 3000L);
TimeUnit unit = TimeUnit.SECONDS; if (result.isSuccess() && (ppi = JSON.toJavaObject((JSON) result.getData(), PoolParameterInfo.class)) != null) {
BlockingQueue workQueue = BlockingQueueUtil.createBlockingQueue(ppi.getQueueType(), ppi.getCapacity()); // 使用相关参数创建线程池
ThreadPoolExecutor resultTpe = new ThreadPoolExecutor(ppi.getCoreSize(), ppi.getMaxSize(), ppi.getKeepAliveTime(), unit, workQueue); TimeUnit unit = TimeUnit.SECONDS;
val.setPool(resultTpe); BlockingQueue workQueue = BlockingQueueUtil.createBlockingQueue(ppi.getQueueType(), ppi.getCapacity());
} else if (val.getPool() == null) { ThreadPoolExecutor resultTpe = new ThreadPoolExecutor(ppi.getCoreSize(), ppi.getMaxSize(), ppi.getKeepAliveTime(), unit, workQueue);
val.setPool(resultTpe);
} else if (val.getPool() == null) {
val.setPool(CommonThreadPool.getInstance(val.getTpId()));
}
} catch (Exception ex) {
log.error(ex.getMessage(), ex);
val.setPool(CommonThreadPool.getInstance(val.getTpId())); val.setPool(CommonThreadPool.getInstance(val.getTpId()));
} }

@ -8,8 +8,8 @@
}, },
{ {
"name": "spring.dynamic.thread-pool.enabled", "name": "spring.dynamic.thread-pool.enabled",
"type": "java.lang.String", "type": "java.lang.Boolean",
"defaultValue": "false", "defaultValue": false,
"description": "dynamic thread-pool enabled." "description": "dynamic thread-pool enabled."
}, },
{ {
@ -22,6 +22,12 @@
"name": "spring.dynamic.thread-pool.item-id", "name": "spring.dynamic.thread-pool.item-id",
"type": "java.lang.String", "type": "java.lang.String",
"description": "dynamic thread-pool item-id." "description": "dynamic thread-pool item-id."
},
{
"name": "spring.dynamic.thread-pool.banner",
"type": "java.lang.Boolean",
"defaultValue": true,
"description": "dynamic thread-pool banner."
} }
] ]
} }
Loading…
Cancel
Save