适配低版本 SpringBoot Binder 未定义问题. (#136)

pull/140/head
chen.ma 3 years ago
parent dae61975e9
commit 5bc745a228

@ -13,12 +13,7 @@ import cn.hippo4j.core.starter.parser.ConfigParserHandler;
import cn.hippo4j.core.starter.support.GlobalCoreThreadPoolManage;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.context.properties.source.ConfigurationPropertySource;
import org.springframework.boot.context.properties.source.MapConfigurationPropertySource;
import java.io.IOException;
import java.util.List;
@ -30,8 +25,6 @@ import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import static cn.hippo4j.core.starter.config.BootstrapCoreProperties.PREFIX;
/**
* Abstract core thread pool dynamic refresh.
*
@ -62,10 +55,7 @@ public abstract class AbstractCoreThreadPoolDynamicRefresh implements ThreadPool
return;
}
ConfigurationPropertySource sources = new MapConfigurationPropertySource(configInfo);
Binder binder = new Binder(sources);
BootstrapCoreProperties bindableCoreProperties = binder.bind(PREFIX, Bindable.ofInstance(bootstrapCoreProperties)).get();
BootstrapCoreProperties bindableCoreProperties = BootstrapCorePropertiesBinderAdapt.bootstrapCorePropertiesBinder(configInfo, bootstrapCoreProperties);
List<ExecutorProperties> executors = bindableCoreProperties.getExecutors();
for (ExecutorProperties properties : executors) {
String threadPoolId = properties.getThreadPoolId();

@ -0,0 +1,109 @@
package cn.hippo4j.core.starter.refresher;
import cn.hippo4j.common.toolkit.CollectionUtil;
import cn.hippo4j.common.toolkit.StringUtil;
import cn.hippo4j.core.starter.config.BootstrapCoreProperties;
import cn.hippo4j.core.starter.config.ExecutorProperties;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.context.properties.source.ConfigurationPropertySource;
import org.springframework.boot.context.properties.source.MapConfigurationPropertySource;
import java.util.List;
import java.util.Map;
import static cn.hippo4j.core.starter.config.BootstrapCoreProperties.PREFIX;
/**
* Bootstrap core properties binder adapt.
*
* @author chen.ma
* @date 2022/3/9 19:04
*/
public class BootstrapCorePropertiesBinderAdapt {
/**
* Bootstrap core properties binder.
*
* @param configInfo
* @param bootstrapCoreProperties
* @return
*/
public static BootstrapCoreProperties bootstrapCorePropertiesBinder(Map<Object, Object> configInfo, BootstrapCoreProperties bootstrapCoreProperties) {
BootstrapCoreProperties bindableCoreProperties = null;
try {
ConfigurationPropertySource sources = new MapConfigurationPropertySource(configInfo);
Binder binder = new Binder(sources);
bindableCoreProperties = binder.bind(PREFIX, Bindable.ofInstance(bootstrapCoreProperties)).get();
} catch (Exception ex) {
try {
Class.forName("org.springframework.boot.context.properties.bind.Binder");
} catch (ClassNotFoundException notEx) {
bindableCoreProperties = adapt(configInfo);
}
}
return bindableCoreProperties;
}
/**
* SpringBoot 1.5.x, , PR.
*
* @param configInfo
* @return
*/
private static BootstrapCoreProperties adapt(Map<Object, Object> configInfo) {
BootstrapCoreProperties bindableCoreProperties;
try {
// filter
Map<Object, Object> targetMap = Maps.newHashMap();
configInfo.forEach((key, val) -> {
if (key != null && StringUtil.isNotBlank((String) key) && ((String) key).indexOf(PREFIX + ".executors") != -1) {
String targetKey = key.toString().replace(PREFIX + ".", "");
targetMap.put(targetKey, val);
}
});
// convert
List<ExecutorProperties> executorPropertiesList = Lists.newArrayList();
for (int i = 0; i < Integer.MAX_VALUE; i++) {
Map<String, Object> tarterSingleMap = Maps.newHashMap();
for (Map.Entry entry : targetMap.entrySet()) {
String key = entry.getKey().toString();
if (key.indexOf(i + "") != -1) {
key = key.replace("executors[" + i + "].", "");
String[] keySplit = key.split("-");
if (keySplit != null && keySplit.length > 0) {
key = key.replace("-", "_");
}
tarterSingleMap.put(key, entry.getValue());
}
}
if (CollectionUtil.isEmpty(tarterSingleMap)) {
break;
}
ExecutorProperties executorProperties = BeanUtil.mapToBean(tarterSingleMap, ExecutorProperties.class, true, CopyOptions.create());
if (executorProperties != null) {
executorPropertiesList.add(executorProperties);
}
}
bindableCoreProperties = new BootstrapCoreProperties();
bindableCoreProperties.setExecutors(executorPropertiesList);
} catch (Exception ex) {
throw ex;
}
return bindableCoreProperties;
}
}
Loading…
Cancel
Save