From f28032a829ef89fbfd5a2b3cd765c4d59f92a7ce Mon Sep 17 00:00:00 2001 From: huoyo <1729913829@qq.com> Date: Sun, 29 May 2022 12:30:16 +0800 Subject: [PATCH] optimize the InvokedQueue --- pom.xml | 2 +- .../cn/langpy/kotime/config/LoadConfig.java | 8 ++--- .../java/cn/langpy/kotime/data/DataBase.java | 18 +++++++---- .../langpy/kotime/service/InvokedQueue.java | 32 +++++++++---------- .../cn/langpy/kotime/util/DataBaseUtil.java | 24 ++++++++++++++ .../spring-configuration-metadata.json | 2 +- 6 files changed, 56 insertions(+), 30 deletions(-) diff --git a/pom.xml b/pom.xml index 37009b3..97f63f0 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ cn.langpy ko-time - 2.2.0.BETA + 2.2.0-PREPARE koTime koTime diff --git a/src/main/java/cn/langpy/kotime/config/LoadConfig.java b/src/main/java/cn/langpy/kotime/config/LoadConfig.java index 47c4b29..4a1d1e4 100644 --- a/src/main/java/cn/langpy/kotime/config/LoadConfig.java +++ b/src/main/java/cn/langpy/kotime/config/LoadConfig.java @@ -44,7 +44,7 @@ public class LoadConfig { private Boolean exceptionEnable; @Value("${koTime.saver:memory}") private String saveSaver; - @Value("${koTime.thread-num:5}") + @Value("${koTime.thread-num:2}") private Integer threadNum; @Value("${server.port:8080}") private Integer serverPort; @@ -68,7 +68,7 @@ public class LoadConfig { config.setSaver(defaultConfig.getSaver() == null ? saveSaver : defaultConfig.getSaver()); config.setEnable(defaultConfig.getEnable() == null ? kotimeEnable : defaultConfig.getEnable()); config.setContextPath(defaultConfig.getContextPath()); - config.setThreadNum(defaultConfig.getThreadNum() == null ? 5 : defaultConfig.getThreadNum()); + config.setThreadNum(defaultConfig.getThreadNum() == null ? 2 : defaultConfig.getThreadNum()); config.setAuthEnable(defaultConfig.getAuthEnable() == null ? false : defaultConfig.getAuthEnable()); config.setParamAnalyse(defaultConfig.getParamAnalyse() == null ? true : defaultConfig.getParamAnalyse()); if (null != config) { @@ -79,13 +79,13 @@ public class LoadConfig { Context.setDataSource(dataSource); }catch (NoUniqueBeanDefinitionException e){ if (StringUtils.isEmpty(config.getDataSource())) { - log.warning("No unique bean of type 'javax.sql.DataSource' available,you can define it by `ko-time.data-source=xxx`"); + log.warning("kotime=>No unique bean of type 'javax.sql.DataSource' available,you can define it by `ko-time.data-source=xxx`"); }else { DataSource dataSource = applicationContext.getBean(config.getDataSource(),DataSource.class); Context.setDataSource(dataSource); } }catch (NoSuchBeanDefinitionException e){ - log.warning("No qualifying bean of type 'javax.sql.DataSource' available,you can ignore it if your KoTime saver is `ko-time.saver=memory`"); + log.warning("kotime=>No qualifying bean of type 'javax.sql.DataSource' available,you can ignore it if your KoTime saver is `ko-time.saver=memory`"); } Context.setConfig(config); diff --git a/src/main/java/cn/langpy/kotime/data/DataBase.java b/src/main/java/cn/langpy/kotime/data/DataBase.java index 4e8429c..a775d94 100644 --- a/src/main/java/cn/langpy/kotime/data/DataBase.java +++ b/src/main/java/cn/langpy/kotime/data/DataBase.java @@ -36,7 +36,6 @@ public class DataBase implements GraphService { Runtime.getRuntime().addShutdownHook( new Thread(() -> { try { - log.info("kotime=>close database connections..."); if (null!=readConnection) { readConnection.close(); } @@ -45,6 +44,8 @@ public class DataBase implements GraphService { } } catch (SQLException throwables) { throwables.printStackTrace(); + }finally { + log.info("kotime=>closed database connections..."); } }) ); @@ -78,8 +79,9 @@ public class DataBase implements GraphService { if (null == methodNode) { return; } - List> query = DataBaseUtil.query(getWriteConnection(),KoSqlConstant.queryMethod, new Object[]{methodNode.getId()}); - if (query.size() == 0) { +// List> query = DataBaseUtil.query(getWriteConnection(),KoSqlConstant.queryMethod, new Object[]{methodNode.getId()}); + boolean existsById = DataBaseUtil.existsById(getWriteConnection(),KoSqlConstant.queryMethod, methodNode.getId()); + if (!existsById) { Object[] params = new Object[]{ methodNode.getId(), methodNode.getName(), @@ -107,8 +109,9 @@ public class DataBase implements GraphService { @Override public synchronized void addExceptionNode(ExceptionNode exceptionNode) { - List> query = DataBaseUtil.query(getWriteConnection(),KoSqlConstant.queryException, new Object[]{exceptionNode.getId()}); - if (query.size() == 0) { +// List> query = DataBaseUtil.query(getWriteConnection(),KoSqlConstant.queryException, new Object[]{exceptionNode.getId()}); + boolean existsById = DataBaseUtil.existsById(getWriteConnection(),KoSqlConstant.queryException, exceptionNode.getId()); + if (!existsById) { Object[] params = new Object[]{ exceptionNode.getId(), exceptionNode.getName(), @@ -168,8 +171,9 @@ public class DataBase implements GraphService { @Override public synchronized ExceptionRelation addExceptionRelation(MethodNode sourceMethodNode, ExceptionNode exceptionNode) { - List> query = DataBaseUtil.query(getWriteConnection(),KoSqlConstant.queryExceptionRe, new Object[]{sourceMethodNode.getId() + exceptionNode.getId()}); - if (query.size() == 0) { +// List> query = DataBaseUtil.query(getWriteConnection(),KoSqlConstant.queryExceptionRe, new Object[]{sourceMethodNode.getId() + exceptionNode.getId()}); + boolean existsById = DataBaseUtil.existsById(getWriteConnection(),KoSqlConstant.queryExceptionRe, sourceMethodNode.getId() + exceptionNode.getId()); + if (!existsById) { Object[] params = new Object[]{ sourceMethodNode.getId() + exceptionNode.getId(), sourceMethodNode.getId(), diff --git a/src/main/java/cn/langpy/kotime/service/InvokedQueue.java b/src/main/java/cn/langpy/kotime/service/InvokedQueue.java index c73e344..9931116 100644 --- a/src/main/java/cn/langpy/kotime/service/InvokedQueue.java +++ b/src/main/java/cn/langpy/kotime/service/InvokedQueue.java @@ -4,39 +4,37 @@ import cn.langpy.kotime.handler.InvokedHandler; import cn.langpy.kotime.model.InvokedInfo; import cn.langpy.kotime.util.Context; -import java.util.LinkedList; +import java.util.concurrent.ConcurrentLinkedQueue; import java.util.logging.Logger; public class InvokedQueue { public static Logger log = Logger.getLogger(InvokedQueue.class.toString()); - private volatile static LinkedList linkedList = new LinkedList(); + private volatile static ConcurrentLinkedQueue queue = new ConcurrentLinkedQueue(); public static void add(InvokedInfo invokedInfo) { - linkedList.add(invokedInfo); + queue.add(invokedInfo); } public static void onInveked() { while (true) { try { - InvokedInfo poll = null; - synchronized (linkedList) { - if (linkedList.size() > 0) { - poll = linkedList.poll(); + if (queue.isEmpty()) { + continue; + } + InvokedInfo poll = queue.poll(); + if (poll==null) { + continue; + } + for (InvokedHandler invokedHandler : Context.getInvokedHandlers()) { + invokedHandler.onInvoked(poll.getCurrent(), poll.getParent(), poll.getNames(), poll.getValues()); + if (null != poll.getException()) { + invokedHandler.onException(poll.getCurrent(), poll.getParent(), poll.getException(), poll.getNames(), poll.getValues()); } } - if (null != poll) { - for (InvokedHandler invokedHandler : Context.getInvokedHandlers()) { - invokedHandler.onInvoked(poll.getCurrent(), poll.getParent(), poll.getNames(), poll.getValues()); - if (null!=poll.getException()) { - invokedHandler.onException(poll.getCurrent(), poll.getParent(),poll.getException(), poll.getNames(), poll.getValues()); - } - } - } - }catch (Exception e){ + } catch (Exception e) { e.printStackTrace(); } } } - } diff --git a/src/main/java/cn/langpy/kotime/util/DataBaseUtil.java b/src/main/java/cn/langpy/kotime/util/DataBaseUtil.java index 612f993..e7c84ff 100644 --- a/src/main/java/cn/langpy/kotime/util/DataBaseUtil.java +++ b/src/main/java/cn/langpy/kotime/util/DataBaseUtil.java @@ -21,6 +21,7 @@ public class DataBaseUtil { public static DataSource getDataSource() { return Context.getDataSource(); } + public static int insert(String sql, Object[] values) { try { Connection connection = getDataSource().getConnection(); @@ -110,6 +111,29 @@ public class DataBaseUtil { return list; } + public static boolean existsById(Connection connection, String sql, Object id) { + PreparedStatement statement = null; + try { + statement = connection.prepareStatement(sql); + statement = setParams(statement, new Object[]{id}); + ResultSet resultSet = statement.executeQuery(); + if (resultSet.next()) { + return true; + } + } catch (SQLException throwables) { + throwables.printStackTrace(); + } finally { + if (statement != null) { + try { + statement.close(); + } catch (SQLException throwables) { + throwables.printStackTrace(); + } + } + } + return false; + } + public static List query(Connection connection, String sql, Object[] values, Class c) { List list = new ArrayList<>(); PreparedStatement statement = null; diff --git a/src/main/resources/META-INF/spring-configuration-metadata.json b/src/main/resources/META-INF/spring-configuration-metadata.json index 5850c7f..f3862ac 100644 --- a/src/main/resources/META-INF/spring-configuration-metadata.json +++ b/src/main/resources/META-INF/spring-configuration-metadata.json @@ -66,7 +66,7 @@ { "name": "ko-time.thread-num", "type": "java.lang.Integer", - "defaultValue": 10, + "defaultValue": 2, "description": "waiting to implement", "sourceType": "cn.langpy.kotime.config.DefaultConfig" },