From 57215c57752ed639617fc8f1b2163e24575ad302 Mon Sep 17 00:00:00 2001 From: o2null Date: Mon, 18 Oct 2021 10:41:42 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=80=E5=87=BA=E6=97=B6=E5=85=B3=E9=97=ADdr?= =?UTF-8?q?uid=20DataSource?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/x/server/console/Main.java | 4 +- .../com/x/server/console/ResourceFactory.java | 82 +++++++++++++------ 2 files changed, 59 insertions(+), 27 deletions(-) diff --git a/o2server/x_console/src/main/java/com/x/server/console/Main.java b/o2server/x_console/src/main/java/com/x/server/console/Main.java index 6ecc805353..1de513fc9d 100644 --- a/o2server/x_console/src/main/java/com/x/server/console/Main.java +++ b/o2server/x_console/src/main/java/com/x/server/console/Main.java @@ -117,7 +117,7 @@ public class Main { cleanTempDir(base); createTempClassesDirectory(base); Log4j2Configuration.reconfigure(); - ResourceFactory.bind(); + ResourceFactory.init(); CommandFactory.printStartHelp(); // 初始化hadoop环境 Hadoop.init(); @@ -433,7 +433,9 @@ public class Main { private static void exit() { stopAll(); + ResourceFactory.destory(); System.exit(0); + } private static void restart() { diff --git a/o2server/x_console/src/main/java/com/x/server/console/ResourceFactory.java b/o2server/x_console/src/main/java/com/x/server/console/ResourceFactory.java index c1ccf1b1f9..181a3c39ed 100644 --- a/o2server/x_console/src/main/java/com/x/server/console/ResourceFactory.java +++ b/o2server/x_console/src/main/java/com/x/server/console/ResourceFactory.java @@ -14,8 +14,9 @@ import java.util.Properties; import java.util.TreeMap; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; import javax.naming.NamingException; @@ -50,23 +51,33 @@ import io.github.classgraph.ClassGraph; import io.github.classgraph.ClassInfo; import io.github.classgraph.ScanResult; +/** + * + * @author ray + * + */ public class ResourceFactory { private static final Logger logger = LoggerFactory.getLogger(ResourceFactory.class); private static final int TOKENTHRESHOLDSMAXSIZE = 2000; + /** + * 用于销毁时的对象 + */ + private static List dataSources = new ArrayList<>(); + private ResourceFactory() { // nothing } - public static void bind() throws Exception { + public static void init() throws Exception { ClassLoader cl = ClassLoaderTools.urlClassLoader(true, false, true, true, true, unzipCustomWar()); try (ScanResult sr = new ClassGraph().addClassLoader(cl).enableAnnotationInfo().scan()) { - node(cl, sr); + node(); containerEntities(cl, sr); - containerEntityNames(cl, sr); - stroageContainerEntityNames(cl, sr); + containerEntityNames(sr); + stroageContainerEntityNames(sr); } if (BooleanUtils.isTrue(Config.externalDataSources().enable())) { external(); @@ -77,12 +88,21 @@ public class ResourceFactory { tokenThresholds(); } + /** + * 需要将custom模块中的entity加入到扫描目录中,
+ * 所以需要对custom中的web-inf/classes目录下的实体类进行扫描
+ * 先把war解压到临时目录然后读取classes目录下的类 + * + * @return + * @throws Exception + */ private static Path[] unzipCustomWar() throws Exception { FileUtils.cleanDirectory(Config.dir_local_temp_custom(true)); List list = new ArrayList<>(); File dir = Config.dir_custom(true); - if (null != dir) { - for (String str : dir.list(new WildcardFileFilter("*" + PathTools.DOT_WAR))) { + String[] wars = dir.list(new WildcardFileFilter("*" + PathTools.DOT_WAR)); + if (null != wars) { + for (String str : wars) { list.add(FilenameUtils.getBaseName(str)); } } @@ -97,20 +117,20 @@ public class ResourceFactory { return paths.toArray(new Path[paths.size()]); } - private static void node(ClassLoader classLoader, ScanResult sr) throws Exception { + private static void node() throws Exception { LinkedBlockingQueue eventQueue = new LinkedBlockingQueue<>(); EventQueueExecutor eventQueueExecutor = new EventQueueExecutor(eventQueue); eventQueueExecutor.start(); new Resource(Config.RESOURCE_NODE_EVENTQUEUE, eventQueue); new Resource(Config.RESOURCE_NODE_EVENTQUEUEEXECUTOR, eventQueueExecutor); - new Resource(Config.RESOURCE_NODE_APPLICATIONS, new ConcurrentHashMap()); + new Resource(Config.RESOURCE_NODE_APPLICATIONS, new ConcurrentHashMap(10)); Entry entry = Config.nodes().centerServers().first(); Config.resource_node_centersPirmaryNode(entry.getKey()); Config.resource_node_centersPirmaryPort(entry.getValue().getPort()); Config.resource_node_centersPirmarySslEnable(entry.getValue().getSslEnable()); } - private static void containerEntityNames(ClassLoader classLoader, ScanResult sr) throws Exception { + private static void containerEntityNames(ScanResult sr) throws NamingException { List list = new ArrayList<>(); for (ClassInfo info : sr.getClassesWithAnnotation(ContainerEntity.class.getName())) { list.add(info.getName()); @@ -119,7 +139,7 @@ public class ResourceFactory { new Resource(Config.RESOURCE_CONTAINERENTITYNAMES, ListUtils.unmodifiableList(list)); } - private static void stroageContainerEntityNames(ClassLoader classLoader, ScanResult sr) throws Exception { + private static void stroageContainerEntityNames(ScanResult sr) throws NamingException { List list = new ArrayList<>(); for (ClassInfo info : sr.getClassesWithAnnotation(Storage.class.getName())) { list.add(info.getName()); @@ -128,7 +148,8 @@ public class ResourceFactory { new Resource(Config.RESOURCE_STORAGECONTAINERENTITYNAMES, ListUtils.unmodifiableList(list)); } - private static void containerEntities(ClassLoader classLoader, ScanResult sr) throws Exception { + private static void containerEntities(ClassLoader classLoader, ScanResult sr) + throws NamingException, ClassNotFoundException { Map> map = new TreeMap<>(); for (ClassInfo info : sr.getClassesWithAnnotation(Module.class.getName())) { Class cls = classLoader.loadClass(info.getName()); @@ -139,10 +160,11 @@ public class ResourceFactory { } private static void external() throws Exception { - external_druid_c3p0(); + dataSources.addAll(externalDruidC3p0()); } - private static void external_druid_c3p0() throws Exception { + private static List externalDruidC3p0() throws Exception { + List list = new ArrayList<>(); for (ExternalDataSource ds : Config.externalDataSources()) { if (BooleanUtils.isNotTrue(ds.getEnable())) { continue; @@ -170,14 +192,26 @@ public class ResourceFactory { dataSource.setAutoCommitOnClose(ds.getAutoCommit()); String name = Config.externalDataSources().name(ds); new Resource(Config.RESOURCE_JDBC_PREFIX + name, dataSource); + list.add(dataSource); } + return list; } private static void internal() throws Exception { - internal_driud_c3p0(); + dataSources.addAll(internalDriudC3p0()); } - private static void internal_driud_c3p0() throws Exception { + /** + * @author ray Druid DataSource 是需要close的. + */ + public static void destory() { + for (DruidDataSourceC3P0Adapter dataSource : dataSources) { + dataSource.close(); + } + } + + private static List internalDriudC3p0() throws Exception { + List list = new ArrayList<>(); for (Entry entry : Config.nodes().dataServers().entrySet()) { DruidDataSourceC3P0Adapter dataSource = new DruidDataSourceC3P0Adapter(); String url = "jdbc:h2:tcp://" + entry.getKey() + ":" + entry.getValue().getTcpPort() @@ -202,22 +236,18 @@ public class ResourceFactory { dataSource.setAutoCommitOnClose(false); String name = Config.nodes().dataServers().name(entry.getValue()); new Resource(Config.RESOURCE_JDBC_PREFIX + name, dataSource); + list.add(dataSource); } + return list; } -// private static void auditLog() throws Exception { -// RolloverFileOutputStream rolloverFileOutputStream = new RolloverFileOutputStream( -// Config.dir_logs(true).getAbsolutePath() + "/yyyy_mm_dd.audit.log", true, -// Config.logLevel().audit().logSize()); -// new Resource(Config.RESOURCE_AUDITLOGPRINTSTREAM, -// new PrintStream(rolloverFileOutputStream, true, DefaultCharset.name_iso_utf_8)); -// } - private static void processPlatformExecutors() throws Exception { ExecutorService[] services = new ExecutorService[Config.processPlatform().getExecutorCount()]; for (int i = 0; i < Config.processPlatform().getExecutorCount(); i++) { - services[i] = Executors.newFixedThreadPool(1, new BasicThreadFactory.Builder() - .namingPattern("ProcessPlatformExecutor-" + i).daemon(true).build()); + // 等价于 Executors.newFixedThreadPool + services[i] = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue(), + new BasicThreadFactory.Builder().namingPattern("ProcessPlatformExecutor-" + i).daemon(true) + .build()); } new Resource(Config.RESOURCE_NODE_PROCESSPLATFORMEXECUTORS, services); -- GitLab