增加SourceChangeable

This commit is contained in:
Redkale
2022-12-04 15:45:27 +08:00
parent b2e7160ce7
commit f8eb3d03ad
7 changed files with 61 additions and 5 deletions

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 { public abstract class AbstractCacheSource extends AbstractService implements CacheSource, AutoCloseable, Resourcable, SourceChangeable {
//@since 2.7.0 //@since 2.7.0
public static final String CACHE_SOURCE_URL = "url"; public static final String CACHE_SOURCE_URL = "url";

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 { public abstract class AbstractDataSource extends AbstractService implements DataSource, AutoCloseable, Resourcable, SourceChangeable {
//@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";

View File

@@ -62,6 +62,10 @@ public final class CacheMemorySource extends AbstractCacheSource {
return "memory"; return "memory";
} }
@Override
public void onChange(ResourceEvent[] events) {
}
public static boolean acceptsConf(AnyValue config) { public static boolean acceptsConf(AnyValue config) {
return config.getValue(CACHE_SOURCE_URL).startsWith("memory:"); return config.getValue(CACHE_SOURCE_URL).startsWith("memory:");
} }

View File

@@ -48,6 +48,11 @@ public class DataJdbcSource extends DataSqlSource {
} }
} }
@Override
public void onChange(ResourceEvent[] events) {
}
@Override @Override
public void destroy(AnyValue config) { public void destroy(AnyValue config) {
if (readPool != null) readPool.close(); if (readPool != null) readPool.close();
@@ -1079,7 +1084,7 @@ public class DataJdbcSource extends DataSqlSource {
}; };
} }
protected class ConnectionPool implements AutoCloseable { protected class ConnectionPool implements AutoCloseable, SourceChangeable {
protected final LongAdder closeCounter = new LongAdder(); //已关闭连接数 protected final LongAdder closeCounter = new LongAdder(); //已关闭连接数
@@ -1095,7 +1100,7 @@ public class DataJdbcSource extends DataSqlSource {
protected final Properties connectAttrs; protected final Properties connectAttrs;
protected final ArrayBlockingQueue<Connection> queue; protected ArrayBlockingQueue<Connection> queue;
protected int connectTimeoutSeconds; protected int connectTimeoutSeconds;
@@ -1120,6 +1125,23 @@ public class DataJdbcSource extends DataSqlSource {
} }
} }
@Override
public void onChange(ResourceEvent[] events) {
for (ResourceEvent event : events) {
if (event.name().equals(DATA_SOURCE_CONNECTTIMEOUT_SECONDS) || event.name().endsWith("." + DATA_SOURCE_CONNECTTIMEOUT_SECONDS)) {
this.connectTimeoutSeconds = Integer.decode(event.newValue().toString());
} else if (event.name().equals(DATA_SOURCE_URL) || event.name().endsWith("." + DATA_SOURCE_URL)) {
this.url = event.newValue().toString();
} else if (event.name().equals(DATA_SOURCE_USER) || event.name().endsWith("." + DATA_SOURCE_USER)) {
this.connectAttrs.put("user", event.newValue().toString());
} else if (event.name().equals(DATA_SOURCE_PASSWORD) || event.name().endsWith("." + DATA_SOURCE_PASSWORD)) {
this.connectAttrs.put("password", event.newValue().toString());
} else if (event.name().equals(DATA_SOURCE_MAXCONNS) || event.name().endsWith("." + DATA_SOURCE_MAXCONNS)) {
logger.log(Level.WARNING, event.name() + " (new-value: " + event.newValue() + ") will not take effect");
}
}
}
public synchronized Connection pollConnection() { public synchronized Connection pollConnection() {
Connection conn = queue.poll(); Connection conn = queue.poll();
if (conn == null) { if (conn == null) {

View File

@@ -43,6 +43,10 @@ public class DataMemorySource extends DataSqlSource implements SearchSource {
return "memory"; return "memory";
} }
@Override
public void onChange(ResourceEvent[] events) {
}
public static boolean acceptsConf(AnyValue config) { public static boolean acceptsConf(AnyValue config) {
return config.getValue(DATA_SOURCE_URL).startsWith("memory:"); return config.getValue(DATA_SOURCE_URL).startsWith("memory:");
} }
@@ -56,8 +60,8 @@ public class DataMemorySource extends DataSqlSource implements SearchSource {
return getClass().getSimpleName() + "{type=memory, name='" + resourceName() + "'}"; return getClass().getSimpleName() + "{type=memory, name='" + resourceName() + "'}";
} }
@Override
@Local @Local
@Override
public <T> void compile(Class<T> clazz) { public <T> void compile(Class<T> clazz) {
EntityInfo entityInfo = EntityInfo.compile(clazz, this); EntityInfo entityInfo = EntityInfo.compile(clazz, this);
if (entityInfo.getCache() == null) new EntityCache<>(entityInfo, null).clear(); if (entityInfo.getCache() == null) new EntityCache<>(entityInfo, null).clear();

View File

@@ -150,6 +150,10 @@ 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

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