From 282081cf95ee701c0bf53caeb76629f1fd79d201 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=9C=B0=E5=B9=B3=E7=BA=BF?= <22250530@qq.com> Date: Fri, 17 Apr 2015 16:57:02 +0800 Subject: [PATCH] --- src/META-INF/application-template.xml | 38 +++++++++++++++---- src/com/wentch/redkale/boot/Application.java | 21 +++++----- .../wentch/redkale/boot/NodeHttpServer.java | 4 +- src/com/wentch/redkale/boot/NodeServer.java | 2 +- .../wentch/redkale/boot/NodeSncpServer.java | 4 +- .../wentch/redkale/source/DataJDBCSource.java | 36 ++++++++---------- .../wentch/redkale/source/DataJPASource.java | 10 ----- src/com/wentch/redkale/source/DataSource.java | 4 -- .../wentch/redkale/source/EntityCache.java | 2 +- 9 files changed, 63 insertions(+), 58 deletions(-) diff --git a/src/META-INF/application-template.xml b/src/META-INF/application-template.xml index c66950542..ef7042b8a 100644 --- a/src/META-INF/application-template.xml +++ b/src/META-INF/application-template.xml @@ -14,8 +14,9 @@ - + - + - - + + @@ -98,6 +99,29 @@ --> + + + + + + + + + + + + + diff --git a/src/com/wentch/redkale/boot/Application.java b/src/com/wentch/redkale/boot/Application.java index e727dc754..7ac5691cd 100644 --- a/src/com/wentch/redkale/boot/Application.java +++ b/src/com/wentch/redkale/boot/Application.java @@ -93,7 +93,7 @@ public final class Application { private final long startTime = System.currentTimeMillis(); - private CountDownLatch cdl; + private CountDownLatch serverscdl; private Application(final AnyValue config) { this.config = config; @@ -203,7 +203,7 @@ public final class Application { final Application application = Application.create(); application.init(); application.factory.register(service); - new NodeHttpServer(application, new CountDownLatch(1), null).load(application.config); + new NodeHttpServer(application, new CountDownLatch(1), null).prepare(application.config); application.factory.inject(service); } @@ -240,6 +240,7 @@ public final class Application { try { final DatagramChannel channel = DatagramChannel.open(); channel.configureBlocking(true); + channel.socket().setSoTimeout(3000); channel.bind(new InetSocketAddress(config.getValue("host", "127.0.0.1"), config.getIntValue("port"))); boolean loop = true; ByteBuffer buffer = ByteBuffer.allocateDirect(1024); @@ -254,14 +255,13 @@ public final class Application { long s = System.currentTimeMillis(); logger.info(application.getClass().getSimpleName() + " shutdowning"); application.shutdown(); - application.cdl.countDown(); buffer.clear(); buffer.put("SHUTDOWN OK".getBytes()); buffer.flip(); channel.send(buffer, address); long e = System.currentTimeMillis() - s; logger.info(application.getClass().getSimpleName() + " shutdown in " + e + " ms"); - Thread.sleep(100L); + application.serverscdl.countDown(); System.exit(0); } catch (Exception ex) { logger.log(Level.INFO, "SHUTDOWN FAIL", ex); @@ -301,12 +301,12 @@ public final class Application { public void start() throws Exception { final AnyValue[] entrys = config.getAnyValues("server"); - cdl = new CountDownLatch(entrys.length + 1); + this.serverscdl = new CountDownLatch(entrys.length + 1); CountDownLatch timecd = new CountDownLatch(entrys.length); runServers(timecd, entrys); timecd.await(); logger.info(this.getClass().getSimpleName() + " started in " + (System.currentTimeMillis() - startTime) + " ms"); - cdl.await(); + this.serverscdl.await(); } @SuppressWarnings("unchecked") @@ -337,14 +337,13 @@ public final class Application { } servers.add(server); - server.load(entry); //必须在init之前 + server.prepare(entry); //必须在init之前 server.init(entry); server.start(); timecd.countDown(); } catch (Exception ex) { logger.log(Level.WARNING, entry + " runServers error", ex); - } finally { - cdl.countDown(); + serverscdl.countDown(); } } }.start(); @@ -519,11 +518,11 @@ public final class Application { } catch (Exception t) { logger.log(Level.WARNING, " shutdown server(" + server.getSocketAddress() + ") error", t); } finally { - cdl.countDown(); + serverscdl.countDown(); } }); final StringBuilder sb = logger.isLoggable(Level.INFO) ? new StringBuilder() : null; - localServices.entrySet().parallelStream().forEach(k -> { + localServices.entrySet().stream().forEach(k -> { Class x = k.getKey(); ServiceEntry y = k.getValue(); long s = System.currentTimeMillis(); diff --git a/src/com/wentch/redkale/boot/NodeHttpServer.java b/src/com/wentch/redkale/boot/NodeHttpServer.java index 6fc78c3bc..73b12f62b 100644 --- a/src/com/wentch/redkale/boot/NodeHttpServer.java +++ b/src/com/wentch/redkale/boot/NodeHttpServer.java @@ -42,8 +42,8 @@ public final class NodeHttpServer extends NodeServer { } @Override - public void load(AnyValue config) throws Exception { - super.load(config); + public void prepare(AnyValue config) throws Exception { + super.prepare(config); ClassFilter httpFilter = createHttpServletClassFilter(application.nodeName, config); ClassFilter serviceFilter = createServiceClassFilter(application.nodeName, config); long s = System.currentTimeMillis(); diff --git a/src/com/wentch/redkale/boot/NodeServer.java b/src/com/wentch/redkale/boot/NodeServer.java index b5325e5b3..420e10976 100644 --- a/src/com/wentch/redkale/boot/NodeServer.java +++ b/src/com/wentch/redkale/boot/NodeServer.java @@ -48,7 +48,7 @@ public abstract class NodeServer { this.logger = Logger.getLogger(this.getClass().getSimpleName()); } - public void load(final AnyValue config) throws Exception { + public void prepare(final AnyValue config) throws Exception { } public void init(AnyValue config) throws Exception { diff --git a/src/com/wentch/redkale/boot/NodeSncpServer.java b/src/com/wentch/redkale/boot/NodeSncpServer.java index 8f6e74899..692ca5fb2 100644 --- a/src/com/wentch/redkale/boot/NodeSncpServer.java +++ b/src/com/wentch/redkale/boot/NodeSncpServer.java @@ -37,8 +37,8 @@ public final class NodeSncpServer extends NodeServer { } @Override - public void load(AnyValue config) throws Exception { - super.load(config); + public void prepare(AnyValue config) throws Exception { + super.prepare(config); ClassFilter serviceFilter = createServiceClassFilter(application.nodeName, config); long s = System.currentTimeMillis(); ClassFilter.Loader.load(application.getHome(), serviceFilter); diff --git a/src/com/wentch/redkale/source/DataJDBCSource.java b/src/com/wentch/redkale/source/DataJDBCSource.java index 1bf0d919a..a6e548bd3 100644 --- a/src/com/wentch/redkale/source/DataJDBCSource.java +++ b/src/com/wentch/redkale/source/DataJDBCSource.java @@ -565,32 +565,28 @@ public final class DataJDBCSource implements DataSource { public void delete(Class clazz, Serializable... ids) { Connection conn = createWriteSQLConnection(); try { + if (ids != null && ids.length == 1 && ids[0] != null && ids[0].getClass().isArray()) { + Class clz = ids[0].getClass(); + if (clz == long[].class) { + long[] vs = (long[]) ids[0]; + ids = new Serializable[vs.length]; + for (int i = 0; i < vs.length; i++) { + ids[i] = vs[i]; + } + } else if (clz == int[].class) { + int[] vs = (int[]) ids[0]; + ids = new Serializable[vs.length]; + for (int i = 0; i < vs.length; i++) { + ids[i] = vs[i]; + } + } + } delete(conn, clazz, ids); } finally { closeSQLConnection(conn); } } - @Override - public void delete(Class clazz, int[] ids) { - if (ids == null) return; - Serializable[] newids = new Serializable[ids.length]; - for (int i = 0; i < ids.length; i++) { - newids[i] = ids[i]; - } - delete(clazz, newids); - } - - @Override - public void delete(Class clazz, long[] ids) { - if (ids == null) return; - Serializable[] newids = new Serializable[ids.length]; - for (int i = 0; i < ids.length; i++) { - newids[i] = ids[i]; - } - delete(clazz, newids); - } - /** * 根据主键值删除对象, 必须是Entity Class * diff --git a/src/com/wentch/redkale/source/DataJPASource.java b/src/com/wentch/redkale/source/DataJPASource.java index eba69d740..d7ac29d71 100644 --- a/src/com/wentch/redkale/source/DataJPASource.java +++ b/src/com/wentch/redkale/source/DataJPASource.java @@ -59,16 +59,6 @@ final class DataJPASource implements DataSource { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } - @Override - public void delete(Class clazz, int[] ids) { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. - } - - @Override - public void delete(Class clazz, long[] ids) { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. - } - private static class DataJPAConnection extends DataConnection { private final EntityManager manager; diff --git a/src/com/wentch/redkale/source/DataSource.java b/src/com/wentch/redkale/source/DataSource.java index 28a33e157..ced5c3f4d 100644 --- a/src/com/wentch/redkale/source/DataSource.java +++ b/src/com/wentch/redkale/source/DataSource.java @@ -83,10 +83,6 @@ public interface DataSource { */ public void delete(Class clazz, Serializable... ids); - public void delete(Class clazz, int[] ids); - - public void delete(Class clazz, long[] ids); - /** * 根据主键值删除对象, 必须是Entity Class * diff --git a/src/com/wentch/redkale/source/EntityCache.java b/src/com/wentch/redkale/source/EntityCache.java index 6c325f24b..aee292301 100644 --- a/src/com/wentch/redkale/source/EntityCache.java +++ b/src/com/wentch/redkale/source/EntityCache.java @@ -149,7 +149,7 @@ final class EntityCache { public void insert(T value) { if (value == null) return; - T rs = needcopy ? reproduce.copy(this.creator.create(), value) : value; //确保同一主键值的map与list中的对象必须共用。 + T rs = reproduce.copy(this.creator.create(), value); //确保同一主键值的map与list中的对象必须共用。 T old = this.map.put((Serializable) this.primary.get(rs), rs); if (old != null) logger.log(Level.WARNING, "cache repeat insert data: " + value); this.list.add(rs);