定义及完善动态线程池工具类.

pull/28/head
chen.ma 3 years ago
parent f0f440999d
commit 7e708d2f67

@ -3,6 +3,8 @@ package cn.hippo4j.common.toolkit;
import cn.hippo4j.common.constant.Constants;
import org.springframework.util.StringUtils;
import static cn.hippo4j.common.constant.Constants.GROUP_KEY_DELIMITER;
/**
* Group key.
*
@ -19,6 +21,18 @@ public class GroupKey {
return doGetKey(dataId, group, datumStr);
}
public static String getKey(String... params) {
StringBuilder groupKey = new StringBuilder();
groupKey.append(params[0]).append(GROUP_KEY_DELIMITER);
for (int i = 1; i < params.length - 1; i++) {
groupKey.append(params[i]).append(GROUP_KEY_DELIMITER);
}
groupKey.append(params[params.length]);
return groupKey.toString();
}
public static String getKeyTenant(String dataId, String group, String tenant) {
return doGetKey(dataId, group, tenant);
}
@ -26,10 +40,10 @@ public class GroupKey {
private static String doGetKey(String dataId, String group, String datumStr) {
StringBuilder sb = new StringBuilder();
urlEncode(dataId, sb);
sb.append('+');
sb.append(GROUP_KEY_DELIMITER);
urlEncode(group, sb);
if (!StringUtils.isEmpty(datumStr)) {
sb.append('+');
sb.append(GROUP_KEY_DELIMITER);
urlEncode(datumStr, sb);
}

@ -1,9 +1,17 @@
package cn.hippo4j.starter.toolkit;
import cn.hippo4j.common.config.ApplicationContextHolder;
import cn.hippo4j.starter.config.BootstrapProperties;
import cn.hippo4j.starter.toolkit.inet.InetUtils;
import cn.hutool.core.util.StrUtil;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import lombok.SneakyThrows;
import org.springframework.core.env.ConfigurableEnvironment;
import java.util.ArrayList;
import static cn.hippo4j.common.constant.Constants.GROUP_KEY_DELIMITER;
import static cn.hippo4j.common.constant.Constants.IDENTIFY_SLICER_SYMBOL;
import static cn.hippo4j.starter.config.DynamicThreadPoolAutoConfiguration.CLIENT_IDENTIFICATION_VALUE;
@ -15,6 +23,8 @@ import static cn.hippo4j.starter.config.DynamicThreadPoolAutoConfiguration.CLIEN
*/
public class IdentifyUtil {
private static String identify;
/**
* Generate identify.
*
@ -22,7 +32,10 @@ public class IdentifyUtil {
* @param hippo4JInetUtils
* @return
*/
public static String generate(ConfigurableEnvironment environment, InetUtils hippo4JInetUtils) {
public static synchronized String generate(ConfigurableEnvironment environment, InetUtils hippo4JInetUtils) {
if (StrUtil.isNotBlank(identify)) {
return identify;
}
String ip = hippo4JInetUtils.findFirstNonLoopbackHostInfo().getIpAddress();
String port = environment.getProperty("server.port");
String identification = StrUtil.builder(ip,
@ -32,7 +45,45 @@ public class IdentifyUtil {
CLIENT_IDENTIFICATION_VALUE
).toString();
identify = identification;
return identification;
}
/**
* Get identify.
*
* @return
*/
@SneakyThrows
public static String getIdentify() {
while (StrUtil.isBlank(identify)) {
ConfigurableEnvironment environment = ApplicationContextHolder.getBean(ConfigurableEnvironment.class);
InetUtils inetUtils = ApplicationContextHolder.getBean(InetUtils.class);
if (environment != null && inetUtils != null) {
String identify = generate(environment, inetUtils);
return identify;
}
Thread.sleep(500);
}
return identify;
}
/**
* Get thread pool identify.
*
* @param threadPoolId
* @param properties
* @return
*/
public static String getThreadPoolIdentify(String threadPoolId, BootstrapProperties properties) {
ArrayList<String> params = Lists.newArrayList(
threadPoolId, properties.getItemId(), properties.getNamespace(), getIdentify()
);
return Joiner.on(GROUP_KEY_DELIMITER).join(params);
}
}

@ -0,0 +1,25 @@
package cn.hippo4j.starter.toolkit.thread;
/**
* Thread util.
*
* @author chen.ma
* @date 2021/12/6 23:34
*/
public class ThreadUtil {
/**
* 线.
*
* @param runnable {@link Runnable}
* @param name 线
* @param isDaemon 线
* @return {@link Thread}
*/
public static Thread newThread(Runnable runnable, String name, boolean isDaemon) {
Thread t = new Thread(null, runnable, name);
t.setDaemon(isDaemon);
return t;
}
}
Loading…
Cancel
Save