移除SourceChangeable

This commit is contained in:
Redkale
2022-12-06 13:38:21 +08:00
parent e01cb1b94d
commit cc3c07d0c2
9 changed files with 30 additions and 45 deletions

View File

@@ -404,8 +404,8 @@ public final class Application {
AnyValue clusterConf = resources.getAnyValue("cluster"); AnyValue clusterConf = resources.getAnyValue("cluster");
if (clusterConf != null) { if (clusterConf != null) {
try { try {
String classval = clusterConf.getValue("type", clusterConf.getValue("value")); //兼容value字段 String classVal = clusterConf.getValue("type", clusterConf.getValue("value")); //兼容value字段
if (classval == null || classval.isEmpty()) { if (classVal == null || classVal.isEmpty() || classVal.indexOf('.') < 0) { //不包含.表示非类名,比如值: consul, nacos
Iterator<ClusterAgentProvider> it = ServiceLoader.load(ClusterAgentProvider.class, classLoader).iterator(); Iterator<ClusterAgentProvider> it = ServiceLoader.load(ClusterAgentProvider.class, classLoader).iterator();
RedkaleClassLoader.putServiceLoader(ClusterAgentProvider.class); RedkaleClassLoader.putServiceLoader(ClusterAgentProvider.class);
while (it.hasNext()) { while (it.hasNext()) {
@@ -426,7 +426,7 @@ public final class Application {
} }
if (cluster == null) logger.log(Level.SEVERE, "load application cluster resource, but not found name='type' value error: " + clusterConf); if (cluster == null) logger.log(Level.SEVERE, "load application cluster resource, but not found name='type' value error: " + clusterConf);
} else { } else {
Class type = classLoader.loadClass(classval); Class type = classLoader.loadClass(classVal);
if (!ClusterAgent.class.isAssignableFrom(type)) { if (!ClusterAgent.class.isAssignableFrom(type)) {
logger.log(Level.SEVERE, "load application cluster resource, but not found " + ClusterAgent.class.getSimpleName() + " implements class error: " + clusterConf); logger.log(Level.SEVERE, "load application cluster resource, but not found " + ClusterAgent.class.getSimpleName() + " implements class error: " + clusterConf);
} else { } else {
@@ -459,8 +459,8 @@ public final class Application {
} }
} }
try { try {
String classval = mqConf.getValue("type", mqConf.getValue("value")); //兼容value字段 String classVal = mqConf.getValue("type", mqConf.getValue("value")); //兼容value字段
if (classval == null || classval.isEmpty()) { if (classVal == null || classVal.isEmpty() || classVal.indexOf('.') < 0) { //不包含.表示非类名,比如值: kafka, pulsar
Iterator<MessageAgentProvider> it = ServiceLoader.load(MessageAgentProvider.class, classLoader).iterator(); Iterator<MessageAgentProvider> it = ServiceLoader.load(MessageAgentProvider.class, classLoader).iterator();
RedkaleClassLoader.putServiceLoader(MessageAgentProvider.class); RedkaleClassLoader.putServiceLoader(MessageAgentProvider.class);
while (it.hasNext()) { while (it.hasNext()) {
@@ -474,7 +474,7 @@ public final class Application {
} }
if (mqs[i] == null) logger.log(Level.SEVERE, "load application mq resource, but not found name='value' value error: " + mqConf); if (mqs[i] == null) logger.log(Level.SEVERE, "load application mq resource, but not found name='value' value error: " + mqConf);
} else { } else {
Class type = classLoader.loadClass(classval); Class type = classLoader.loadClass(classVal);
if (!MessageAgent.class.isAssignableFrom(type)) { if (!MessageAgent.class.isAssignableFrom(type)) {
logger.log(Level.SEVERE, "load application mq resource, but not found " + MessageAgent.class.getSimpleName() + " implements class error: " + mqConf); logger.log(Level.SEVERE, "load application mq resource, but not found " + MessageAgent.class.getSimpleName() + " implements class error: " + mqConf);
} else { } else {
@@ -1066,10 +1066,10 @@ public final class Application {
logger.info("[" + Thread.currentThread().getName() + "] Load DataSource resourceName = " + sourceName + ", source = " + source); logger.info("[" + Thread.currentThread().getName() + "] Load DataSource resourceName = " + sourceName + ", source = " + source);
return source; return source;
} }
String classval = sourceConf.getValue("type"); String classVal = sourceConf.getValue("type");
try { try {
DataSource source = null; DataSource source = null;
if (classval == null || classval.isEmpty()) { if (classVal == null || classVal.isEmpty()) {
if (DataJdbcSource.acceptsConf(sourceConf)) { if (DataJdbcSource.acceptsConf(sourceConf)) {
source = new DataJdbcSource(); source = new DataJdbcSource();
} else { } else {
@@ -1094,7 +1094,7 @@ public final class Application {
} }
} }
} else { } else {
Class sourceType = serverClassLoader.loadClass(classval); Class sourceType = serverClassLoader.loadClass(classVal);
RedkaleClassLoader.putReflectionPublicConstructors(sourceType, sourceType.getName()); RedkaleClassLoader.putReflectionPublicConstructors(sourceType, sourceType.getName());
source = (DataSource) sourceType.getConstructor().newInstance(); source = (DataSource) sourceType.getConstructor().newInstance();
} }
@@ -1784,7 +1784,7 @@ public final class Application {
events.add(ResourceEvent.create(k, v, null)); events.add(ResourceEvent.create(k, v, null));
} }
}); });
((AbstractCacheSource) source).onChange(old == null ? conf : old, events.toArray(new ResourceEvent[events.size()])); ((AbstractCacheSource) source).onResourceChange(events.toArray(new ResourceEvent[events.size()]));
}); });
} }
AnyValue sourceNode = redNode.getAnyValue("datasource"); AnyValue sourceNode = redNode.getAnyValue("datasource");
@@ -1802,7 +1802,7 @@ public final class Application {
events.add(ResourceEvent.create(k, v, null)); events.add(ResourceEvent.create(k, v, null));
} }
}); });
((AbstractDataSource) source).onChange(old == null ? conf : old, events.toArray(new ResourceEvent[events.size()])); ((AbstractDataSource) source).onResourceChange(events.toArray(new ResourceEvent[events.size()]));
}); });
} }
sourceProperties.putAll(sourceChangeCache); sourceProperties.putAll(sourceChangeCache);

View File

@@ -18,7 +18,7 @@ import org.redkale.util.*;
@AutoLoad(false) @AutoLoad(false)
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ResourceType(CacheSource.class) @ResourceType(CacheSource.class)
public abstract class AbstractCacheSource extends AbstractService implements CacheSource, AutoCloseable, Resourcable, SourceChangeable { public abstract class AbstractCacheSource extends AbstractService implements CacheSource, AutoCloseable, Resourcable {
//@since 2.7.0 //@since 2.7.0
public static final String CACHE_SOURCE_URL = "url"; public static final String CACHE_SOURCE_URL = "url";
@@ -44,4 +44,6 @@ public abstract class AbstractCacheSource extends AbstractService implements Cac
//@since 2.7.0 //@since 2.7.0
public static final String CACHE_SOURCE_PIPELINES = "pipelines"; public static final String CACHE_SOURCE_PIPELINES = "pipelines";
@ResourceListener
public abstract void onResourceChange(ResourceEvent[] events);
} }

View File

@@ -30,7 +30,7 @@ import org.redkale.util.*;
@AutoLoad(false) @AutoLoad(false)
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ResourceType(DataSource.class) @ResourceType(DataSource.class)
public abstract class AbstractDataSource extends AbstractService implements DataSource, AutoCloseable, Resourcable, SourceChangeable { public abstract class AbstractDataSource extends AbstractService implements DataSource, AutoCloseable, Resourcable {
//@since 2.7.0 格式: x.x.x.x:yyyy //@since 2.7.0 格式: x.x.x.x:yyyy
public static final String DATA_SOURCE_PROXY_ADDRESS = "proxy-address"; public static final String DATA_SOURCE_PROXY_ADDRESS = "proxy-address";
@@ -92,6 +92,9 @@ public abstract class AbstractDataSource extends AbstractService implements Data
//@since 2.7.0 //@since 2.7.0
public static final String DATA_SOURCE_TABLECOPY_SQLTEMPLATE = "tablecopy-sqltemplate"; public static final String DATA_SOURCE_TABLECOPY_SQLTEMPLATE = "tablecopy-sqltemplate";
@ResourceListener
public abstract void onResourceChange(ResourceEvent[] events);
public static String parseDbtype(String url) { public static String parseDbtype(String url) {
String dbtype = null; String dbtype = null;
if (url == null) return dbtype; if (url == null) return dbtype;

View File

@@ -62,8 +62,8 @@ public final class CacheMemorySource extends AbstractCacheSource {
return "memory"; return "memory";
} }
@Override @ResourceListener
public void onChange(AnyValue newConf, ResourceEvent[] events) { public void onResourceChange(ResourceEvent[] events) {
} }
public static boolean acceptsConf(AnyValue config) { public static boolean acceptsConf(AnyValue config) {

View File

@@ -49,7 +49,8 @@ public class DataJdbcSource extends DataSqlSource {
} }
@Override @Override
public void onChange(AnyValue newConf, ResourceEvent[] events) { @ResourceListener
public void onResourceChange(ResourceEvent[] events) {
//@TODO 待实现 //@TODO 待实现
} }
@@ -1099,7 +1100,7 @@ public class DataJdbcSource extends DataSqlSource {
}; };
} }
protected class ConnectionPool implements AutoCloseable, SourceChangeable { protected class ConnectionPool implements AutoCloseable {
protected final LongAdder closeCounter = new LongAdder(); //已关闭连接数 protected final LongAdder closeCounter = new LongAdder(); //已关闭连接数
@@ -1140,8 +1141,8 @@ public class DataJdbcSource extends DataSqlSource {
} }
} }
@Override @ResourceListener
public void onChange(AnyValue newConf, ResourceEvent[] events) { public void onResourceChange(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,8 @@ public class DataMemorySource extends DataSqlSource implements SearchSource {
} }
@Override @Override
public void onChange(AnyValue newConf, ResourceEvent[] events) { @ResourceListener
public void onResourceChange(ResourceEvent[] events) {
} }
public static boolean acceptsConf(AnyValue config) { public static boolean acceptsConf(AnyValue config) {

View File

@@ -119,8 +119,8 @@ 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 @ResourceListener
public void onChange(AnyValue newConf, ResourceEvent[] events) { public void onResourceChange(ResourceEvent[] events) {
//@TODO 待实现 //@TODO 待实现
} }

View File

@@ -1,22 +0,0 @@
/*
*
*/
package org.redkale.source;
import org.redkale.util.*;
/**
* 资源变更回调接口
*
* <p>
* 详情见: https://redkale.org
*
* @author zhangjx
*
* @since 2.8.0
*/
public interface SourceChangeable {
public void onChange(AnyValue newConf, ResourceEvent[] events);
}

View File

@@ -11,7 +11,7 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME;
/** /**
* &#64;Resource资源被更新时的监听事件, 本注解只能标记在方法参数为ResourceEvent[]上 <br> * &#64;Resource资源被更新时的监听事件, 本注解只能标记在方法参数为ResourceEvent[]上 <br>
* 注意: 一个类只能存在一个&#64;ResourceResourceListener的方法 多余的会被忽略 * 注意: 一个类只能存在一个&#64;ResourceListener的方法 多余的会被忽略 <br>
* 方法在资源被更新以后调用。 * 方法在资源被更新以后调用。
* *
* <blockquote><pre> * <blockquote><pre>