优化Application.singleton方法
This commit is contained in:
@@ -135,7 +135,7 @@ public final class Application {
|
|||||||
|
|
||||||
//--------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------
|
||||||
//是否用于main方法运行
|
//是否用于main方法运行
|
||||||
private final boolean singletonrun;
|
final boolean singletonrun;
|
||||||
|
|
||||||
//根WatchFactory
|
//根WatchFactory
|
||||||
//private final WatchFactory watchFactory = WatchFactory.root();
|
//private final WatchFactory watchFactory = WatchFactory.root();
|
||||||
@@ -672,6 +672,7 @@ public final class Application {
|
|||||||
final List<AnyValue> others = new ArrayList<>();
|
final List<AnyValue> others = new ArrayList<>();
|
||||||
final List<AnyValue> watchs = new ArrayList<>();
|
final List<AnyValue> watchs = new ArrayList<>();
|
||||||
for (final AnyValue entry : entrys) {
|
for (final AnyValue entry : entrys) {
|
||||||
|
if (singletonrun) ((DefaultAnyValue) entry).setValue("red" + "kale-singleton-serviceclass", config.getValue("red" + "kale-singleton-serviceclass"));
|
||||||
if (entry.getValue("protocol", "").toUpperCase().startsWith("SNCP")) {
|
if (entry.getValue("protocol", "").toUpperCase().startsWith("SNCP")) {
|
||||||
sncps.add(entry);
|
sncps.add(entry);
|
||||||
} else if (entry.getValue("protocol", "").toUpperCase().startsWith("WATCH")) {
|
} else if (entry.getValue("protocol", "").toUpperCase().startsWith("WATCH")) {
|
||||||
@@ -819,6 +820,7 @@ public final class Application {
|
|||||||
public static <T extends Service> T singleton(String name, Class<T> serviceClass) throws Exception {
|
public static <T extends Service> T singleton(String name, Class<T> serviceClass) throws Exception {
|
||||||
if (serviceClass == null) throw new IllegalArgumentException("serviceClass is null");
|
if (serviceClass == null) throw new IllegalArgumentException("serviceClass is null");
|
||||||
final Application application = Application.create(true);
|
final Application application = Application.create(true);
|
||||||
|
((DefaultAnyValue) application.config).setValue("red" + "kale-singleton-serviceclass", serviceClass.getName());
|
||||||
application.init();
|
application.init();
|
||||||
application.start();
|
application.start();
|
||||||
for (NodeServer server : application.servers) {
|
for (NodeServer server : application.servers) {
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import java.lang.reflect.Modifier;
|
|||||||
import java.net.*;
|
import java.net.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
|
import java.util.function.Predicate;
|
||||||
import java.util.jar.*;
|
import java.util.jar.*;
|
||||||
import java.util.logging.*;
|
import java.util.logging.*;
|
||||||
import java.util.regex.*;
|
import java.util.regex.*;
|
||||||
@@ -36,6 +37,8 @@ public final class ClassFilter<T> {
|
|||||||
|
|
||||||
private final Set<FilterEntry<T>> expectEntrys = new HashSet<>(); //准备符合条件的结果
|
private final Set<FilterEntry<T>> expectEntrys = new HashSet<>(); //准备符合条件的结果
|
||||||
|
|
||||||
|
private Predicate<String> expectPredicate;
|
||||||
|
|
||||||
private boolean refused; //是否拒绝所有数据,设置true,则其他规则失效,都是拒绝.
|
private boolean refused; //是否拒绝所有数据,设置true,则其他规则失效,都是拒绝.
|
||||||
|
|
||||||
private Class superClass; //符合的父类型。不为空时,扫描结果的class必须是superClass的子类
|
private Class superClass; //符合的父类型。不为空时,扫描结果的class必须是superClass的子类
|
||||||
@@ -196,7 +199,7 @@ public final class ClassFilter<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
AutoLoad auto = (AutoLoad) clazz.getAnnotation(AutoLoad.class);
|
AutoLoad auto = (AutoLoad) clazz.getAnnotation(AutoLoad.class);
|
||||||
if (autoscan && auto != null && !auto.value()) { //自动扫描且被标记为@AutoLoad(false)的
|
if ((expectPredicate != null && expectPredicate.test(clazzname)) || (autoscan && auto != null && !auto.value())) { //自动扫描且被标记为@AutoLoad(false)的
|
||||||
expectEntrys.add(new FilterEntry(clazz, autoscan, true, property));
|
expectEntrys.add(new FilterEntry(clazz, autoscan, true, property));
|
||||||
} else {
|
} else {
|
||||||
entrys.add(new FilterEntry(clazz, autoscan, false, property));
|
entrys.add(new FilterEntry(clazz, autoscan, false, property));
|
||||||
@@ -347,6 +350,14 @@ public final class ClassFilter<T> {
|
|||||||
this.refused = refused;
|
this.refused = refused;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Predicate<String> getExpectPredicate() {
|
||||||
|
return expectPredicate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setExpectPredicate(Predicate<String> predicate) {
|
||||||
|
this.expectPredicate = predicate;
|
||||||
|
}
|
||||||
|
|
||||||
public Set<String> getPrivilegeIncludes() {
|
public Set<String> getPrivilegeIncludes() {
|
||||||
return privilegeIncludes;
|
return privilegeIncludes;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -154,17 +154,27 @@ public abstract class NodeServer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ClassFilter<Service> serviceFilter = createServiceClassFilter();
|
ClassFilter<Service> serviceFilter = createServiceClassFilter();
|
||||||
|
if (application.singletonrun) {
|
||||||
|
final String ssc = config == null ? null : config.getValue("red" + "kale-singleton-serviceclass");
|
||||||
|
if (ssc != null) serviceFilter.setExpectPredicate(c -> !ssc.equals(c));
|
||||||
|
}
|
||||||
ClassFilter<Filter> filterFilter = createFilterClassFilter();
|
ClassFilter<Filter> filterFilter = createFilterClassFilter();
|
||||||
ClassFilter<Servlet> servletFilter = createServletClassFilter();
|
ClassFilter<Servlet> servletFilter = createServletClassFilter();
|
||||||
ClassFilter otherFilter = createOtherClassFilter();
|
ClassFilter otherFilter = createOtherClassFilter();
|
||||||
|
if (application.singletonrun) {
|
||||||
|
if (filterFilter != null) filterFilter.setRefused(true);
|
||||||
|
if (servletFilter != null) servletFilter.setRefused(true);
|
||||||
|
if (otherFilter != null) otherFilter.setRefused(true);
|
||||||
|
}
|
||||||
long s = System.currentTimeMillis();
|
long s = System.currentTimeMillis();
|
||||||
ClassFilter.Loader.load(application.getHome(), serverConf.getValue("excludelibs", "").split(";"), serviceFilter, filterFilter, servletFilter, otherFilter);
|
ClassFilter.Loader.load(application.getHome(), serverConf.getValue("excludelibs", "").split(";"), serviceFilter, filterFilter, servletFilter, otherFilter);
|
||||||
long e = System.currentTimeMillis() - s;
|
long e = System.currentTimeMillis() - s;
|
||||||
logger.info(this.getClass().getSimpleName() + " load filter class in " + e + " ms");
|
logger.info(this.getClass().getSimpleName() + " load filter class in " + e + " ms");
|
||||||
loadService(serviceFilter, otherFilter); //必须在servlet之前
|
loadService(serviceFilter, otherFilter); //必须在servlet之前
|
||||||
loadFilter(filterFilter, otherFilter);
|
if (!application.singletonrun) {
|
||||||
loadServlet(servletFilter, otherFilter);
|
loadFilter(filterFilter, otherFilter);
|
||||||
|
loadServlet(servletFilter, otherFilter);
|
||||||
|
}
|
||||||
if (this.interceptor != null) this.resourceFactory.inject(this.interceptor);
|
if (this.interceptor != null) this.resourceFactory.inject(this.interceptor);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ public class NodeSncpServer extends NodeServer {
|
|||||||
private NodeSncpServer(Application application, AnyValue serconf) {
|
private NodeSncpServer(Application application, AnyValue serconf) {
|
||||||
super(application, createServer(application, serconf));
|
super(application, createServer(application, serconf));
|
||||||
this.sncpServer = (SncpServer) this.server;
|
this.sncpServer = (SncpServer) this.server;
|
||||||
this.consumer = sncpServer == null ? null : x -> sncpServer.addSncpServlet(x);
|
this.consumer = sncpServer == null || application.singletonrun ? null : x -> sncpServer.addSncpServlet(x); //singleton模式下不生成SncpServlet
|
||||||
}
|
}
|
||||||
|
|
||||||
public static NodeServer createNodeServer(Application application, AnyValue serconf) {
|
public static NodeServer createNodeServer(Application application, AnyValue serconf) {
|
||||||
|
|||||||
Reference in New Issue
Block a user