Support batch update fizz configs

This commit is contained in:
hongqiaowei
2022-03-04 15:54:08 +08:00
parent 5df2cf3834
commit 69db0b131b
2 changed files with 21 additions and 54 deletions

View File

@@ -44,10 +44,7 @@ import we.context.event.FizzRefreshEvent;
import we.global_resource.GlobalResource;
import we.util.*;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.*;
/**
* @author hongqiaowei
@@ -152,44 +149,23 @@ public class FizzBeanFactoryPostProcessor implements BeanFactoryPostProcessor, E
msg -> {
String message = msg.getMessage();
try {
/*Map<String, String> changes = JacksonUtils.readValue(message, new TypeReference<Map<String, String>>(){});
boolean defaultConfigEnable = false;
String defaultConfigEnableStr = changes.get("fizz.default-config.enable");
if (defaultConfigEnableStr == null) {
defaultConfigEnable = environment.getProperty("fizz.default-config.enable", Boolean.class, false);
} else {
defaultConfigEnable = Boolean.parseBoolean(defaultConfigEnableStr);
}
boolean finalDefaultConfigEnable = defaultConfigEnable;
changes.forEach(
(property, value) -> {
if (StringUtils.isBlank(value)) {
if (finalDefaultConfigEnable) {
Object v = FizzConfigConfiguration.DEFAULT_CONFIG_MAP.get(property);
if (v == null) {
sources.remove(property);
} else {
sources.put(property, v);
}
} else {
sources.remove(property);
}
} else {
sources.put(property, value);
}
}
);*/
Map<String, Object> change = JacksonUtils.readValue(message, new TypeReference<Map<String, Object>>(){});
int isDeleted = (int) change.remove("isDeleted");
Map.Entry<String, Object> propertyValue = change.entrySet().iterator().next();
String property = propertyValue.getKey();
if (isDeleted == 1) {
sources.remove(property);
} else {
sources.put(property, propertyValue.getValue());
Map<String, Object> changedPropertyValueMap = new HashMap<>();
List<Map<String, Object>> changes = JacksonUtils.readValue(message, new TypeReference<List<Map<String, Object>>>(){});
for (Map<String, Object> change : changes) {
int isDeleted = (int) change.remove("isDeleted");
Map.Entry<String, Object> propertyValue = change.entrySet().iterator().next();
String property = propertyValue.getKey();
Object v = null;
if (isDeleted == 1) {
sources.remove(property);
} else {
v = propertyValue.getValue();
sources.put(property, v);
}
changedPropertyValueMap.put(property, v);
}
LOGGER.info("new fizz configs: {}", JacksonUtils.writeValueAsString(sources));
FizzRefreshEvent refreshEvent = new FizzRefreshEvent(applicationContext, FizzRefreshEvent.ENV_CHANGE, change);
FizzRefreshEvent refreshEvent = new FizzRefreshEvent(applicationContext, FizzRefreshEvent.ENV_CHANGE, changedPropertyValueMap);
applicationContext.publishEvent(refreshEvent);
} catch (Throwable t) {
LOGGER.error("update fizz config {} error", message, t);

View File

@@ -24,6 +24,7 @@ import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.SmartApplicationListener;
import we.beans.factory.config.FizzBeanFactoryPostProcessor;
import we.context.scope.refresh.FizzRefreshScope;
import we.util.JacksonUtils;
import java.util.HashMap;
import java.util.Map;
@@ -70,13 +71,13 @@ public class FizzRefreshEventListener implements SmartApplicationListener {
if (this.ready.get()) {
// EnvironmentChangeEvent ?
if (event.getType() == FizzRefreshEvent.ENV_CHANGE) {
/*Map<String*//*bean*//*, Map<String*//*property*//*, String*//*value*//*>> bean2propertyValuesMap = new HashMap<>();
Map<String, String> changedPropertyValueMap = (Map<String, String>) event.getData();
Map<String/*bean*/, Map<String/*property*/, Object/*value*/>> bean2propertyValuesMap = new HashMap<>();
Map<String, Object> changedPropertyValueMap = (Map<String, Object>) event.getData();
changedPropertyValueMap.forEach(
(property, value) -> {
String bean = fizzBeanFactoryPostProcessor.getBean(property);
if (bean != null) {
Map<String, String> propertyValueMap = bean2propertyValuesMap.computeIfAbsent(bean, k -> new HashMap<>());
Map<String, Object> propertyValueMap = bean2propertyValuesMap.computeIfAbsent(bean, k -> new HashMap<>());
propertyValueMap.put(property, value);
}
}
@@ -84,17 +85,7 @@ public class FizzRefreshEventListener implements SmartApplicationListener {
bean2propertyValuesMap.forEach(
(bean, propertyValueMap) -> {
fizzRefreshScope.refresh(bean);
LOGGER.info("fizz refresh {} bean with {}", bean, propertyValueMap);
}
);*/
Map<String, Object> changedPropertyValue = (Map<String, Object>) event.getData();
changedPropertyValue.forEach(
(property, value) -> {
String bean = fizzBeanFactoryPostProcessor.getBean(property);
if (bean != null) {
fizzRefreshScope.refresh(bean);
LOGGER.info("fizz refresh {} bean with {}={}", bean, property, value);
}
LOGGER.info("fizz refresh {} bean with {}", bean, JacksonUtils.writeValueAsString(propertyValueMap));
}
);
}