优化createDataSource和createCacheSource
This commit is contained in:
@@ -1189,47 +1189,18 @@ public final class Application {
|
||||
CacheSource source = new CacheMemorySource(sourceName);
|
||||
cacheSources.add(source);
|
||||
resourceFactory.register(sourceName, CacheSource.class, source);
|
||||
resourceFactory.inject(sourceName, source);
|
||||
if (!compileMode && source instanceof Service) ((Service) source).init(sourceConf);
|
||||
logger.info("[" + Thread.currentThread().getName() + "] Load CacheSource resourceName = " + sourceName + ", source = " + source + " in " + (System.currentTimeMillis() - st) + " ms");
|
||||
return source;
|
||||
}
|
||||
String classVal = sourceConf.getValue("type");
|
||||
try {
|
||||
CacheSource source = null;
|
||||
if (classVal == null || classVal.isEmpty()) {
|
||||
RedkaleClassLoader.putServiceLoader(CacheSourceProvider.class);
|
||||
List<CacheSourceProvider> providers = new ArrayList<>();
|
||||
Iterator<CacheSourceProvider> it = ServiceLoader.load(CacheSourceProvider.class, serverClassLoader).iterator();
|
||||
while (it.hasNext()) {
|
||||
CacheSourceProvider provider = it.next();
|
||||
if (provider != null) RedkaleClassLoader.putReflectionPublicConstructors(provider.getClass(), provider.getClass().getName());
|
||||
if (provider != null && provider.acceptsConf(sourceConf)) {
|
||||
providers.add(provider);
|
||||
}
|
||||
}
|
||||
for (CacheSourceProvider provider : InstanceProvider.sort(providers)) {
|
||||
source = provider.createInstance();
|
||||
if (source != null) break;
|
||||
}
|
||||
if (source == null) {
|
||||
if (CacheMemorySource.acceptsConf(sourceConf)) {
|
||||
source = new CacheMemorySource(sourceName);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Class sourceType = serverClassLoader.loadClass(classVal);
|
||||
RedkaleClassLoader.putReflectionPublicConstructors(sourceType, sourceType.getName());
|
||||
source = (CacheSource) sourceType.getConstructor().newInstance();
|
||||
}
|
||||
if (source == null) throw new RuntimeException("Not found CacheSourceProvider for config=" + sourceConf);
|
||||
|
||||
CacheSource source = AbstractCacheSource.createCacheSource(serverClassLoader, resourceFactory, sourceConf, sourceName, compileMode);
|
||||
cacheSources.add(source);
|
||||
resourceFactory.register(sourceName, source);
|
||||
resourceFactory.inject(sourceName, source);
|
||||
if (!compileMode && source instanceof Service) ((Service) source).init(sourceConf);
|
||||
logger.info("[" + Thread.currentThread().getName() + "] Load CacheSource resourceName = " + sourceName + ", source = " + source + " in " + (System.currentTimeMillis() - st) + " ms");
|
||||
return source;
|
||||
} catch (RuntimeException ex) {
|
||||
throw ex;
|
||||
} catch (Exception e) {
|
||||
logger.log(Level.SEVERE, "load application CaheSource error: " + sourceConf, e);
|
||||
}
|
||||
@@ -1243,55 +1214,23 @@ public final class Application {
|
||||
if (sourceConf == null) {
|
||||
if (!autoMemory) return null;
|
||||
DataSource source = new DataMemorySource(sourceName);
|
||||
if (!compileMode && source instanceof Service) {
|
||||
resourceFactory.inject(sourceName, source);
|
||||
((Service) source).init(sourceConf);
|
||||
}
|
||||
dataSources.add(source);
|
||||
resourceFactory.register(sourceName, DataSource.class, source);
|
||||
resourceFactory.inject(sourceName, source);
|
||||
if (!compileMode && source instanceof Service) ((Service) source).init(sourceConf);
|
||||
logger.info("[" + Thread.currentThread().getName() + "] Load DataSource resourceName = " + sourceName + ", source = " + source);
|
||||
return source;
|
||||
}
|
||||
String classVal = sourceConf.getValue("type");
|
||||
try {
|
||||
DataSource source = null;
|
||||
if (classVal == null || classVal.isEmpty()) {
|
||||
if (DataJdbcSource.acceptsConf(sourceConf)) {
|
||||
source = new DataJdbcSource();
|
||||
} else {
|
||||
RedkaleClassLoader.putServiceLoader(DataSourceProvider.class);
|
||||
List<DataSourceProvider> providers = new ArrayList<>();
|
||||
Iterator<DataSourceProvider> it = ServiceLoader.load(DataSourceProvider.class, serverClassLoader).iterator();
|
||||
while (it.hasNext()) {
|
||||
DataSourceProvider provider = it.next();
|
||||
if (provider != null) RedkaleClassLoader.putReflectionPublicConstructors(provider.getClass(), provider.getClass().getName());
|
||||
if (provider != null && provider.acceptsConf(sourceConf)) {
|
||||
providers.add(provider);
|
||||
}
|
||||
}
|
||||
for (DataSourceProvider provider : InstanceProvider.sort(providers)) {
|
||||
source = provider.createInstance();
|
||||
if (source != null) break;
|
||||
}
|
||||
if (source == null) {
|
||||
if (DataMemorySource.acceptsConf(sourceConf)) {
|
||||
source = new DataMemorySource(sourceName);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Class sourceType = serverClassLoader.loadClass(classVal);
|
||||
RedkaleClassLoader.putReflectionPublicConstructors(sourceType, sourceType.getName());
|
||||
source = (DataSource) sourceType.getConstructor().newInstance();
|
||||
}
|
||||
if (source == null) throw new RuntimeException("Not found DataSourceProvider for config=" + sourceConf);
|
||||
|
||||
DataSource source = AbstractDataSource.createDataSource(serverClassLoader, resourceFactory, sourceConf, sourceName, compileMode);
|
||||
dataSources.add(source);
|
||||
if (source instanceof DataMemorySource && DataMemorySource.isSearchType(sourceConf)) {
|
||||
resourceFactory.register(sourceName, SearchSource.class, source);
|
||||
} else {
|
||||
resourceFactory.register(sourceName, source);
|
||||
}
|
||||
resourceFactory.inject(sourceName, source);
|
||||
if (!compileMode && source instanceof Service) ((Service) source).init(sourceConf);
|
||||
logger.info("[" + Thread.currentThread().getName() + "] Load DataSource resourceName = " + sourceName + ", source = " + source);
|
||||
return source;
|
||||
} catch (RuntimeException ex) {
|
||||
@@ -1655,7 +1594,7 @@ public final class Application {
|
||||
}
|
||||
if (server == null) {
|
||||
logger.log(Level.SEVERE, "Not found Server Class for protocol({0})", serconf.getValue("protocol"));
|
||||
System.exit(0);
|
||||
System.exit(1);
|
||||
}
|
||||
server.init(serconf);
|
||||
if (!singletonMode && !compileMode) {
|
||||
@@ -1669,6 +1608,7 @@ public final class Application {
|
||||
} catch (Exception ex) {
|
||||
logger.log(Level.WARNING, serconf + " runServers error", ex);
|
||||
Application.this.shutdownLatch.countDown();
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
*/
|
||||
package org.redkale.source;
|
||||
|
||||
import java.util.*;
|
||||
import org.redkale.annotation.AutoLoad;
|
||||
import org.redkale.annotation.ResourceListener;
|
||||
import org.redkale.annotation.ResourceType;
|
||||
@@ -49,4 +50,55 @@ public abstract class AbstractCacheSource extends AbstractService implements Cac
|
||||
|
||||
@ResourceListener
|
||||
public abstract void onResourceChange(ResourceEvent[] events);
|
||||
|
||||
//从Properties配置中创建DataSource
|
||||
public static CacheSource createCacheSource(Properties sourceProperties, String sourceName) throws Exception {
|
||||
AnyValue redConf = AnyValue.loadFromProperties(sourceProperties);
|
||||
AnyValue sourceConf = redConf.getAnyValue("cachesource").getAnyValue(sourceName);
|
||||
return createCacheSource(null, null, sourceConf, sourceName, false);
|
||||
}
|
||||
|
||||
//根据配置中创建DataSource
|
||||
public static CacheSource createCacheSource(ClassLoader serverClassLoader, ResourceFactory resourceFactory, AnyValue sourceConf, String sourceName, boolean compileMode) throws Exception {
|
||||
CacheSource source = null;
|
||||
if (serverClassLoader == null) {
|
||||
serverClassLoader = Thread.currentThread().getContextClassLoader();
|
||||
}
|
||||
String classVal = sourceConf.getValue("type");
|
||||
if (classVal == null || classVal.isEmpty()) {
|
||||
RedkaleClassLoader.putServiceLoader(CacheSourceProvider.class);
|
||||
List<CacheSourceProvider> providers = new ArrayList<>();
|
||||
Iterator<CacheSourceProvider> it = ServiceLoader.load(CacheSourceProvider.class, serverClassLoader).iterator();
|
||||
while (it.hasNext()) {
|
||||
CacheSourceProvider provider = it.next();
|
||||
if (provider != null) RedkaleClassLoader.putReflectionPublicConstructors(provider.getClass(), provider.getClass().getName());
|
||||
if (provider != null && provider.acceptsConf(sourceConf)) {
|
||||
providers.add(provider);
|
||||
}
|
||||
}
|
||||
for (CacheSourceProvider provider : InstanceProvider.sort(providers)) {
|
||||
source = provider.createInstance();
|
||||
if (source != null) break;
|
||||
}
|
||||
if (source == null) {
|
||||
if (CacheMemorySource.acceptsConf(sourceConf)) {
|
||||
source = new CacheMemorySource(sourceName);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Class sourceType = serverClassLoader.loadClass(classVal);
|
||||
RedkaleClassLoader.putReflectionPublicConstructors(sourceType, sourceType.getName());
|
||||
source = (CacheSource) sourceType.getConstructor().newInstance();
|
||||
}
|
||||
if (source == null) {
|
||||
throw new RuntimeException("Not found CacheSourceProvider for config=" + sourceConf);
|
||||
}
|
||||
if (!compileMode && resourceFactory != null) {
|
||||
resourceFactory.inject(sourceName, source);
|
||||
}
|
||||
if (!compileMode && source instanceof Service) {
|
||||
((Service) source).init(sourceConf);
|
||||
}
|
||||
return source;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,6 +102,61 @@ public abstract class AbstractDataSource extends AbstractService implements Data
|
||||
@ResourceListener
|
||||
public abstract void onResourceChange(ResourceEvent[] events);
|
||||
|
||||
//从Properties配置中创建DataSource
|
||||
public static DataSource createDataSource(Properties sourceProperties, String sourceName) throws Exception {
|
||||
AnyValue redConf = AnyValue.loadFromProperties(sourceProperties);
|
||||
AnyValue sourceConf = redConf.getAnyValue("datasource").getAnyValue(sourceName);
|
||||
return createDataSource(null, null, sourceConf, sourceName, false);
|
||||
}
|
||||
|
||||
//根据配置中创建DataSource
|
||||
public static DataSource createDataSource(ClassLoader serverClassLoader, ResourceFactory resourceFactory, AnyValue sourceConf, String sourceName, boolean compileMode) throws Exception {
|
||||
DataSource source = null;
|
||||
if (serverClassLoader == null) {
|
||||
serverClassLoader = Thread.currentThread().getContextClassLoader();
|
||||
}
|
||||
String classVal = sourceConf.getValue("type");
|
||||
if (classVal == null || classVal.isEmpty()) {
|
||||
if (DataJdbcSource.acceptsConf(sourceConf)) {
|
||||
source = new DataJdbcSource();
|
||||
} else {
|
||||
RedkaleClassLoader.putServiceLoader(DataSourceProvider.class);
|
||||
List<DataSourceProvider> providers = new ArrayList<>();
|
||||
Iterator<DataSourceProvider> it = ServiceLoader.load(DataSourceProvider.class, serverClassLoader).iterator();
|
||||
while (it.hasNext()) {
|
||||
DataSourceProvider provider = it.next();
|
||||
if (provider != null) RedkaleClassLoader.putReflectionPublicConstructors(provider.getClass(), provider.getClass().getName());
|
||||
if (provider != null && provider.acceptsConf(sourceConf)) {
|
||||
providers.add(provider);
|
||||
}
|
||||
}
|
||||
for (DataSourceProvider provider : InstanceProvider.sort(providers)) {
|
||||
source = provider.createInstance();
|
||||
if (source != null) break;
|
||||
}
|
||||
if (source == null) {
|
||||
if (DataMemorySource.acceptsConf(sourceConf)) {
|
||||
source = new DataMemorySource(sourceName);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Class sourceType = serverClassLoader.loadClass(classVal);
|
||||
RedkaleClassLoader.putReflectionPublicConstructors(sourceType, sourceType.getName());
|
||||
source = (DataSource) sourceType.getConstructor().newInstance();
|
||||
}
|
||||
if (source == null) {
|
||||
throw new RuntimeException("Not found DataSourceProvider for config=" + sourceConf);
|
||||
}
|
||||
if (!compileMode && resourceFactory != null) {
|
||||
resourceFactory.inject(sourceName, source);
|
||||
}
|
||||
if (!compileMode && source instanceof Service) {
|
||||
((Service) source).init(sourceConf);
|
||||
}
|
||||
return source;
|
||||
}
|
||||
|
||||
public static String parseDbtype(String url) {
|
||||
String dbtype = null;
|
||||
if (url == null) return dbtype;
|
||||
|
||||
@@ -24,7 +24,6 @@ import org.redkale.service.Local;
|
||||
import org.redkale.source.EntityInfo.EntityColumn;
|
||||
import org.redkale.util.*;
|
||||
|
||||
|
||||
/**
|
||||
* DataSource的SQL抽象实现类 <br>
|
||||
* 注意: 所有的操作只能作用在一张表上,不能同时变更多张表
|
||||
@@ -56,10 +55,10 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi
|
||||
|
||||
protected Properties writeConfProps;
|
||||
|
||||
@Resource(name = RESNAME_APP_CLIENT_ASYNCGROUP)
|
||||
@Resource(name = RESNAME_APP_CLIENT_ASYNCGROUP, required = false)
|
||||
protected AsyncGroup clientAsyncGroup;
|
||||
|
||||
@Resource(name = RESNAME_APP_EXECUTOR)
|
||||
@Resource(name = RESNAME_APP_EXECUTOR, required = false)
|
||||
protected ExecutorService workExecutor;
|
||||
|
||||
protected BiFunction<EntityInfo, Object, CharSequence> sqlFormatter;
|
||||
@@ -125,7 +124,7 @@ public abstract class DataSqlSource extends AbstractDataSource implements Functi
|
||||
|
||||
this.tableNotExistSqlstates = ";" + readConfProps.getProperty(DATA_SOURCE_TABLENOTEXIST_SQLSTATES, "42000;42S02") + ";";
|
||||
this.tablecopySQL = readConfProps.getProperty(DATA_SOURCE_TABLECOPY_SQLTEMPLATE, "CREATE TABLE IF NOT EXISTS ${newtable} LIKE ${oldtable}");
|
||||
|
||||
|
||||
this.cacheForbidden = "NONE".equalsIgnoreCase(readConfProps.getProperty(DATA_SOURCE_CACHEMODE));
|
||||
this.slowms = Integer.parseInt(readConfProps.getProperty(DATA_SOURCE_SLOWMS, "3000").trim());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user