修改SourceChangeable类接口

This commit is contained in:
Redkale
2022-12-05 20:04:10 +08:00
parent 709d320a5a
commit e01cb1b94d
7 changed files with 70 additions and 50 deletions

View File

@@ -1754,49 +1754,59 @@ public final class Application {
void updateSourceProperties(Properties sourceChangeCache) { void updateSourceProperties(Properties sourceChangeCache) {
if (sourceChangeCache == null || sourceChangeCache.isEmpty()) return; if (sourceChangeCache == null || sourceChangeCache.isEmpty()) return;
boolean same = true; synchronized (sourceProperties) {
for (Map.Entry<Object, Object> en : sourceChangeCache.entrySet()) { boolean same = true;
String key = en.getKey().toString(); for (Map.Entry<Object, Object> en : sourceChangeCache.entrySet()) {
if (key.startsWith("redkale.datasource.") || key.startsWith("redkale.datasource[") String key = en.getKey().toString();
|| key.startsWith("redkale.cachesource.") || key.startsWith("redkale.cachesource[")) { if (key.startsWith("redkale.datasource.") || key.startsWith("redkale.datasource[")
if (!Objects.equals(en.getValue(), sourceProperties.get(key))) { || key.startsWith("redkale.cachesource.") || key.startsWith("redkale.cachesource[")) {
same = false; if (!Objects.equals(en.getValue(), sourceProperties.get(key))) {
same = false;
}
} else {
throw new RuntimeException("source properties contains illegal key: " + key);
} }
} else {
throw new RuntimeException("source properties contains illegal key: " + key);
} }
} if (same) return; //无内容改变
if (same) return; //无内容改变 AnyValue redNode = AnyValue.loadFromProperties(sourceChangeCache).getAnyValue("redkale");
AnyValue redNode = AnyValue.loadFromProperties(sourceChangeCache).getAnyValue("redkale"); AnyValue cacheNode = redNode.getAnyValue("cachesource");
AnyValue cacheNode = redNode.getAnyValue("cachesource"); if (cacheNode != null) {
if (cacheNode != null) { cacheNode.forEach(null, (name, conf) -> {
cacheNode.forEach(null, (name, conf) -> { CacheSource source = Utility.find(cacheSources, s -> Objects.equals(s.resourceName(), name));
CacheSource source = Utility.find(cacheSources, s -> Objects.equals(s.resourceName(), name)); if (source == null) return;
if (source == null) return; List<ResourceEvent> events = new ArrayList<>();
List<ResourceEvent> events = new ArrayList<>(); AnyValue old = findSourceConfig(name, "cachesource");
AnyValue old = findSourceConfig(name, "cachesource"); conf.forEach((k, v) -> {
conf.forEach((k, v) -> { if (old != null) {
events.add(ResourceEvent.create(k, v, old == null ? null : old.getValue(k))); events.add(ResourceEvent.create(k, v, old.getValue(k)));
if (old != null) ((DefaultAnyValue) old).setValue(k, v); ((DefaultAnyValue) old).setValue(k, v);
} else {
events.add(ResourceEvent.create(k, v, null));
}
});
((AbstractCacheSource) source).onChange(old == null ? conf : old, events.toArray(new ResourceEvent[events.size()]));
}); });
((AbstractCacheSource) source).onChange(events.toArray(new ResourceEvent[events.size()])); }
}); AnyValue sourceNode = redNode.getAnyValue("datasource");
} if (sourceNode != null) {
AnyValue sourceNode = redNode.getAnyValue("datasource"); sourceNode.forEach(null, (name, conf) -> {
if (sourceNode != null) { DataSource source = Utility.find(dataSources, s -> Objects.equals(s.resourceName(), name));
sourceNode.forEach(null, (name, conf) -> { if (source == null) return;
DataSource source = Utility.find(dataSources, s -> Objects.equals(s.resourceName(), name)); List<ResourceEvent> events = new ArrayList<>();
if (source == null) return; AnyValue old = findSourceConfig(name, "datasource");
List<ResourceEvent> events = new ArrayList<>(); conf.forEach((k, v) -> {
AnyValue old = findSourceConfig(name, "datasource"); if (old != null) {
conf.forEach((k, v) -> { events.add(ResourceEvent.create(k, v, old.getValue(k)));
events.add(ResourceEvent.create(k, v, old == null ? null : old.getValue(k))); ((DefaultAnyValue) old).setValue(k, v);
if (old != null) ((DefaultAnyValue) old).setValue(k, v); } else {
events.add(ResourceEvent.create(k, v, null));
}
});
((AbstractDataSource) source).onChange(old == null ? conf : old, events.toArray(new ResourceEvent[events.size()]));
}); });
((AbstractDataSource) source).onChange(events.toArray(new ResourceEvent[events.size()])); }
}); sourceProperties.putAll(sourceChangeCache);
} }
sourceProperties.putAll(sourceChangeCache);
} }
private static String generateHelp() { private static String generateHelp() {

View File

@@ -63,7 +63,7 @@ public final class CacheMemorySource extends AbstractCacheSource {
} }
@Override @Override
public void onChange(ResourceEvent[] events) { public void onChange(AnyValue newConf, ResourceEvent[] events) {
} }
public static boolean acceptsConf(AnyValue config) { public static boolean acceptsConf(AnyValue config) {

View File

@@ -49,8 +49,8 @@ public class DataJdbcSource extends DataSqlSource {
} }
@Override @Override
public void onChange(ResourceEvent[] events) { public void onChange(AnyValue newConf, ResourceEvent[] events) {
//@TODO 待实现
} }
@Override @Override
@@ -1141,7 +1141,7 @@ public class DataJdbcSource extends DataSqlSource {
} }
@Override @Override
public void onChange(ResourceEvent[] events) { public void onChange(AnyValue newConf, ResourceEvent[] events) {
for (ResourceEvent event : events) { for (ResourceEvent event : events) {
if (event.name().equals(DATA_SOURCE_CONNECTTIMEOUT_SECONDS) || event.name().endsWith("." + DATA_SOURCE_CONNECTTIMEOUT_SECONDS)) { if (event.name().equals(DATA_SOURCE_CONNECTTIMEOUT_SECONDS) || event.name().endsWith("." + DATA_SOURCE_CONNECTTIMEOUT_SECONDS)) {
this.connectTimeoutSeconds = Integer.decode(event.newValue().toString()); this.connectTimeoutSeconds = Integer.decode(event.newValue().toString());

View File

@@ -44,7 +44,7 @@ public class DataMemorySource extends DataSqlSource implements SearchSource {
} }
@Override @Override
public void onChange(ResourceEvent[] events) { public void onChange(AnyValue newConf, ResourceEvent[] events) {
} }
public static boolean acceptsConf(AnyValue config) { public static boolean acceptsConf(AnyValue config) {

View File

@@ -119,6 +119,11 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi
this.cacheForbidden = "NONE".equalsIgnoreCase(readConfProps.getProperty(DATA_SOURCE_CACHEMODE)); this.cacheForbidden = "NONE".equalsIgnoreCase(readConfProps.getProperty(DATA_SOURCE_CACHEMODE));
} }
@Override
public void onChange(AnyValue newConf, ResourceEvent[] events) {
//@TODO 待实现
}
//解密可能存在的加密字段, 可重载 //解密可能存在的加密字段, 可重载
protected void decryptProperties(Properties props) { protected void decryptProperties(Properties props) {
@@ -150,10 +155,6 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi
} }
} }
@Override
public void onChange(ResourceEvent[] events) {
}
@Override @Override
public String toString() { public String toString() {
if (readConfProps == null) return getClass().getSimpleName() + "{}"; //compileMode模式下会为null if (readConfProps == null) return getClass().getSimpleName() + "{}"; //compileMode模式下会为null

View File

@@ -1400,9 +1400,18 @@ public final class EntityInfo<T> {
return DataResultSet.getRowColumnValue(this, attr, index, columnLabel); return DataResultSet.getRowColumnValue(this, attr, index, columnLabel);
} }
//判断当前行值是否为null /**
* 判断当前行值是否为null
*
* @return boolean
*/
public boolean wasNull(); public boolean wasNull();
/**
* 获取字段名集合,尽量不要多次调用
*
* @return List
*/
public List<String> getColumnLabels(); public List<String> getColumnLabels();
} }

View File

@@ -3,7 +3,7 @@
*/ */
package org.redkale.source; package org.redkale.source;
import org.redkale.util.ResourceEvent; import org.redkale.util.*;
/** /**
* 资源变更回调接口 * 资源变更回调接口
@@ -17,6 +17,6 @@ import org.redkale.util.ResourceEvent;
*/ */
public interface SourceChangeable { public interface SourceChangeable {
public void onChange(ResourceEvent[] events); public void onChange(AnyValue newConf, ResourceEvent[] events);
} }