diff --git a/src/org/redkale/boot/NodeHttpServer.java b/src/org/redkale/boot/NodeHttpServer.java index d2c9826fa..f2ddb644e 100644 --- a/src/org/redkale/boot/NodeHttpServer.java +++ b/src/org/redkale/boot/NodeHttpServer.java @@ -43,7 +43,7 @@ public class NodeHttpServer extends NodeServer { } private static Server createServer(Application application, AnyValue serconf) { - return new HttpServer(application.getStartTime(), application.getWatchFactory()); + return new HttpServer(application.getStartTime()); } @Override @@ -114,7 +114,6 @@ public class NodeHttpServer extends NodeServer { WebServlet ws = clazz.getAnnotation(WebServlet.class); if (ws == null || ws.value().length == 0) continue; final HttpServlet servlet = clazz.newInstance(); - resourceFactory.inject(servlet, this); final String[] mappings = ws.value(); String pref = ws.repair() ? prefix : ""; DefaultAnyValue servletConf = (DefaultAnyValue) en.getProperty(); diff --git a/src/org/redkale/boot/NodeServer.java b/src/org/redkale/boot/NodeServer.java index e3f51a741..f0fccebba 100644 --- a/src/org/redkale/boot/NodeServer.java +++ b/src/org/redkale/boot/NodeServer.java @@ -244,6 +244,7 @@ public abstract class NodeServer { } field.set(src, source); rf.inject(source, self); // 给其可能包含@Resource的字段赋值; + //NodeServer.this.watchFactory.inject(src); if (source instanceof Service) ((Service) source).init(null); } catch (Exception e) { logger.log(Level.SEVERE, "DataSource inject error", e); diff --git a/src/org/redkale/boot/NodeSncpServer.java b/src/org/redkale/boot/NodeSncpServer.java index c423418db..84fb6acc5 100644 --- a/src/org/redkale/boot/NodeSncpServer.java +++ b/src/org/redkale/boot/NodeSncpServer.java @@ -41,7 +41,7 @@ public class NodeSncpServer extends NodeServer { } private static Server createServer(Application application, AnyValue serconf) { - return new SncpServer(application.getStartTime(), application.getWatchFactory()); + return new SncpServer(application.getStartTime()); } @Override diff --git a/src/org/redkale/net/Context.java b/src/org/redkale/net/Context.java index 9f40fe58c..6cafbffc0 100644 --- a/src/org/redkale/net/Context.java +++ b/src/org/redkale/net/Context.java @@ -14,7 +14,6 @@ import java.util.logging.*; import org.redkale.convert.bson.*; import org.redkale.convert.json.*; import org.redkale.util.*; -import org.redkale.watch.*; /** * 服务器上下文对象 @@ -70,12 +69,8 @@ public class Context { //JSON操作工厂 protected final JsonFactory jsonFactory; - //监控对象 - protected final WatchFactory watch; - public Context(long serverStartTime, Logger logger, ExecutorService executor, int bufferCapacity, ObjectPool bufferPool, ObjectPool responsePool, - final int maxbody, Charset charset, InetSocketAddress address, final PrepareServlet prepare, final WatchFactory watch, - final int readTimeoutSecond, final int writeTimeoutSecond) { + final int maxbody, Charset charset, InetSocketAddress address, final PrepareServlet prepare, final int readTimeoutSecond, final int writeTimeoutSecond) { this.serverStartTime = serverStartTime; this.logger = logger; this.executor = executor; @@ -86,7 +81,6 @@ public class Context { this.charset = UTF8.equals(charset) ? null : charset; this.address = address; this.prepare = prepare; - this.watch = watch; this.readTimeoutSecond = readTimeoutSecond; this.writeTimeoutSecond = writeTimeoutSecond; this.jsonFactory = JsonFactory.root(); diff --git a/src/org/redkale/net/Server.java b/src/org/redkale/net/Server.java index 90f5c80ea..6fd6f4f31 100644 --- a/src/org/redkale/net/Server.java +++ b/src/org/redkale/net/Server.java @@ -15,7 +15,6 @@ import java.util.concurrent.*; import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Logger; import org.redkale.util.AnyValue; -import org.redkale.watch.WatchFactory; /** * @@ -39,9 +38,6 @@ public abstract class Server servlet, final WatchFactory watch) { + protected Server(long serverStartTime, String protocol, PrepareServlet servlet) { this.serverStartTime = serverStartTime; this.protocol = protocol; this.prepare = servlet; - this.watch = watch; } public void init(final AnyValue config) throws Exception { @@ -161,7 +156,6 @@ public abstract class Server bufferPool, ObjectPool responsePool, int maxbody, Charset charset, InetSocketAddress address, PrepareServlet prepare, - WatchFactory watch, int readTimeoutSecond, int writeTimeoutSecond) { + int readTimeoutSecond, int writeTimeoutSecond) { super(serverStartTime, logger, executor, bufferCapacity, bufferPool, responsePool, maxbody, charset, - address, prepare, watch, readTimeoutSecond, writeTimeoutSecond); + address, prepare, readTimeoutSecond, writeTimeoutSecond); random.setSeed(Math.abs(System.nanoTime())); } @@ -46,10 +45,6 @@ public class HttpContext extends Context { return new String(Utility.binToHex(bytes)); } - protected WatchFactory getWatchFactory() { - return watch; - } - protected ExecutorService getExecutor() { return executor; } diff --git a/src/org/redkale/net/http/HttpPrepareServlet.java b/src/org/redkale/net/http/HttpPrepareServlet.java index f81339fbe..68ba8390f 100644 --- a/src/org/redkale/net/http/HttpPrepareServlet.java +++ b/src/org/redkale/net/http/HttpPrepareServlet.java @@ -14,7 +14,6 @@ import java.util.logging.*; import java.util.regex.*; import org.redkale.net.*; import org.redkale.util.*; -import org.redkale.watch.*; /** * HTTP Servlet的总入口,请求在HttpPrepareServlet中进行分流。
@@ -103,12 +102,6 @@ public class HttpPrepareServlet extends PrepareServlet { - watch.inject(s); - }); - } AnyValue resConfig = config.getAnyValue("resource-servlet"); if ((resConfig instanceof DefaultAnyValue) && resConfig.getValue("webroot", "").isEmpty()) { ((DefaultAnyValue) resConfig).addValue("webroot", config.getValue("root")); diff --git a/src/org/redkale/net/http/HttpServer.java b/src/org/redkale/net/http/HttpServer.java index 760b18409..25e3cc973 100644 --- a/src/org/redkale/net/http/HttpServer.java +++ b/src/org/redkale/net/http/HttpServer.java @@ -13,7 +13,6 @@ import java.util.concurrent.atomic.AtomicLong; import org.redkale.net.*; import org.redkale.service.Service; import org.redkale.util.*; -import org.redkale.watch.WatchFactory; /** * Http服务器 @@ -26,11 +25,11 @@ import org.redkale.watch.WatchFactory; public class HttpServer extends Server { public HttpServer() { - this(System.currentTimeMillis(), null); + this(System.currentTimeMillis()); } - public HttpServer(long serverStartTime, final WatchFactory watch) { - super(serverStartTime, "TCP", new HttpPrepareServlet(), watch); + public HttpServer(long serverStartTime) { + super(serverStartTime, "TCP", new HttpPrepareServlet()); } @Override @@ -170,8 +169,8 @@ public class HttpServer extends Server bufferPool = new ObjectPool<>(createBufferCounter, cycleBufferCounter, this.bufferPoolSize, @@ -250,11 +249,11 @@ public class HttpServer extends Server responsePool = HttpResponse.createPool(createResponseCounter, cycleResponseCounter, this.responsePoolSize, null); HttpContext httpcontext = new HttpContext(this.serverStartTime, this.logger, executor, rcapacity, bufferPool, responsePool, - this.maxbody, this.charset, this.address, this.prepare, this.watch, this.readTimeoutSecond, this.writeTimeoutSecond); + this.maxbody, this.charset, this.address, this.prepare, this.readTimeoutSecond, this.writeTimeoutSecond); responsePool.setCreator((Object... params) -> new HttpResponse(httpcontext, new HttpRequest(httpcontext, addrHeader), addHeaders, setHeaders, defCookie)); return httpcontext; } diff --git a/src/org/redkale/net/sncp/SncpContext.java b/src/org/redkale/net/sncp/SncpContext.java index ab82de6ca..c6bc34d84 100644 --- a/src/org/redkale/net/sncp/SncpContext.java +++ b/src/org/redkale/net/sncp/SncpContext.java @@ -12,7 +12,6 @@ import java.util.concurrent.ExecutorService; import java.util.logging.Logger; import org.redkale.net.*; import org.redkale.util.ObjectPool; -import org.redkale.watch.WatchFactory; /** *

@@ -23,9 +22,9 @@ import org.redkale.watch.WatchFactory; public class SncpContext extends Context { public SncpContext(long serverStartTime, Logger logger, ExecutorService executor, int bufferCapacity, ObjectPool bufferPool, - ObjectPool responsePool, int maxbody, Charset charset, InetSocketAddress address, PrepareServlet prepare, - WatchFactory watch, int readTimeoutSecond, int writeTimeoutSecond) { + ObjectPool responsePool, int maxbody, Charset charset, InetSocketAddress address, PrepareServlet prepare, + int readTimeoutSecond, int writeTimeoutSecond) { super(serverStartTime, logger, executor, bufferCapacity, bufferPool, responsePool, maxbody, charset, - address, prepare, watch, readTimeoutSecond, writeTimeoutSecond); + address, prepare, readTimeoutSecond, writeTimeoutSecond); } } diff --git a/src/org/redkale/net/sncp/SncpServer.java b/src/org/redkale/net/sncp/SncpServer.java index c0e351bf1..0319bdbb4 100644 --- a/src/org/redkale/net/sncp/SncpServer.java +++ b/src/org/redkale/net/sncp/SncpServer.java @@ -12,7 +12,6 @@ import org.redkale.convert.bson.*; import org.redkale.net.*; import org.redkale.service.Service; import org.redkale.util.*; -import org.redkale.watch.*; /** * Service Node Communicate Protocol @@ -26,11 +25,11 @@ import org.redkale.watch.*; public class SncpServer extends Server { public SncpServer() { - this(System.currentTimeMillis(), null); + this(System.currentTimeMillis()); } - public SncpServer(long serverStartTime, final WatchFactory watch) { - super(serverStartTime, "TCP", new SncpPrepareServlet(), watch); + public SncpServer(long serverStartTime) { + super(serverStartTime, "TCP", new SncpPrepareServlet()); } @Override @@ -58,8 +57,8 @@ public class SncpServer extends Server bufferPool = new ObjectPool<>(createBufferCounter, cycleBufferCounter, this.bufferPoolSize, (Object... params) -> ByteBuffer.allocateDirect(rcapacity), null, (e) -> { @@ -67,11 +66,11 @@ public class SncpServer extends Server responsePool = SncpResponse.createPool(createResponseCounter, cycleResponseCounter, this.responsePoolSize, null); SncpContext sncpcontext = new SncpContext(this.serverStartTime, this.logger, executor, rcapacity, bufferPool, responsePool, - this.maxbody, this.charset, this.address, this.prepare, this.watch, this.readTimeoutSecond, this.writeTimeoutSecond); + this.maxbody, this.charset, this.address, this.prepare, this.readTimeoutSecond, this.writeTimeoutSecond); responsePool.setCreator((Object... params) -> new SncpResponse(sncpcontext, new SncpRequest(sncpcontext))); return sncpcontext; } diff --git a/src/org/redkale/util/ResourceFactory.java b/src/org/redkale/util/ResourceFactory.java index 7dee15e03..e26aace12 100644 --- a/src/org/redkale/util/ResourceFactory.java +++ b/src/org/redkale/util/ResourceFactory.java @@ -11,7 +11,6 @@ import java.util.*; import java.util.concurrent.*; import java.util.function.BiConsumer; import java.util.logging.*; -import java.util.regex.Pattern; import javax.annotation.Resource; /** @@ -279,20 +278,6 @@ public final class ResourceFactory { return null; } - private void load(final Pattern reg, Class clazz, final A exclude, final Map result) { - ConcurrentHashMap map = this.store.get(clazz); - if (map != null) { - for (Map.Entry en : map.entrySet()) { // 不用forEach为兼容JDK 6 - String x = en.getKey(); - ResourceEntry re = en.getValue(); - if (re == null) continue; - Object y = re.value; - if (y != exclude && reg.matcher(x).find() && result.get(x) == null) result.put(x, (A) y); - } - } - if (parent != null) parent.load(reg, clazz, exclude, result); - } - public boolean inject(final Object src) { return inject(src, null); } diff --git a/src/org/redkale/watch/WatchFactory.java b/src/org/redkale/watch/WatchFactory.java index 0a7546cd2..b5d5ad66c 100644 --- a/src/org/redkale/watch/WatchFactory.java +++ b/src/org/redkale/watch/WatchFactory.java @@ -20,15 +20,18 @@ import java.util.function.LongSupplier; */ public final class WatchFactory { - private static final WatchFactory instance = new WatchFactory(null); + private static final WatchFactory instance = new WatchFactory("", null); private final List> chidren = new CopyOnWriteArrayList<>(); private final List> beans = new CopyOnWriteArrayList<>(); + private final String name; + private final WatchFactory parent; - private WatchFactory(WatchFactory parent) { + private WatchFactory(String name, WatchFactory parent) { + this.name = name; this.parent = parent; } @@ -42,8 +45,8 @@ public final class WatchFactory { return instance; } - public WatchFactory createChild() { - WatchFactory child = new WatchFactory(this); + public WatchFactory createChild(final String name) { + WatchFactory child = new WatchFactory(name, this); this.chidren.add(new WeakReference<>(child)); return child; } @@ -87,9 +90,7 @@ public final class WatchFactory { } public WatchNumber createWatchNumber(String name, String description, boolean interval, long v) { - WatchNumber bean = new WatchNumber(name, description, interval, v); - register(bean); - return bean; + return new WatchNumber(name, description, interval, v); } public void register(String name, LongSupplier supplier) { @@ -106,11 +107,15 @@ public final class WatchFactory { } } - public boolean inject(final Object src) { - return inject(src, new ArrayList<>()); + protected boolean inject(final Object src) { + return inject(src, null); } - private boolean inject(final Object src, final List list) { + protected boolean inject(final Object src, final T attachment) { + return inject(src, attachment, new ArrayList<>()); + } + + private boolean inject(final Object src, final T attachment, final List list) { if (src == null) return false; try { list.add(src); @@ -121,7 +126,7 @@ public final class WatchFactory { field.setAccessible(true); final Class type = field.getType(); Watchable wo = field.getAnnotation(Watchable.class); - + if (wo == null && !WatchNode.class.isAssignableFrom(type)) continue; } } while ((clazz = clazz.getSuperclass()) != Object.class); return true;