optimize the InvokedQueue

This commit is contained in:
huoyo 2022-05-29 12:30:16 +08:00
parent 8302ef82a1
commit f28032a829
6 changed files with 56 additions and 30 deletions

View File

@ -6,7 +6,7 @@
<groupId>cn.langpy</groupId> <groupId>cn.langpy</groupId>
<artifactId>ko-time</artifactId> <artifactId>ko-time</artifactId>
<version>2.2.0.BETA</version> <version>2.2.0-PREPARE</version>
<name>koTime</name> <name>koTime</name>
<description>koTime</description> <description>koTime</description>
<licenses> <licenses>

View File

@ -44,7 +44,7 @@ public class LoadConfig {
private Boolean exceptionEnable; private Boolean exceptionEnable;
@Value("${koTime.saver:memory}") @Value("${koTime.saver:memory}")
private String saveSaver; private String saveSaver;
@Value("${koTime.thread-num:5}") @Value("${koTime.thread-num:2}")
private Integer threadNum; private Integer threadNum;
@Value("${server.port:8080}") @Value("${server.port:8080}")
private Integer serverPort; private Integer serverPort;
@ -68,7 +68,7 @@ public class LoadConfig {
config.setSaver(defaultConfig.getSaver() == null ? saveSaver : defaultConfig.getSaver()); config.setSaver(defaultConfig.getSaver() == null ? saveSaver : defaultConfig.getSaver());
config.setEnable(defaultConfig.getEnable() == null ? kotimeEnable : defaultConfig.getEnable()); config.setEnable(defaultConfig.getEnable() == null ? kotimeEnable : defaultConfig.getEnable());
config.setContextPath(defaultConfig.getContextPath()); 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.setAuthEnable(defaultConfig.getAuthEnable() == null ? false : defaultConfig.getAuthEnable());
config.setParamAnalyse(defaultConfig.getParamAnalyse() == null ? true : defaultConfig.getParamAnalyse()); config.setParamAnalyse(defaultConfig.getParamAnalyse() == null ? true : defaultConfig.getParamAnalyse());
if (null != config) { if (null != config) {
@ -79,13 +79,13 @@ public class LoadConfig {
Context.setDataSource(dataSource); Context.setDataSource(dataSource);
}catch (NoUniqueBeanDefinitionException e){ }catch (NoUniqueBeanDefinitionException e){
if (StringUtils.isEmpty(config.getDataSource())) { 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 { }else {
DataSource dataSource = applicationContext.getBean(config.getDataSource(),DataSource.class); DataSource dataSource = applicationContext.getBean(config.getDataSource(),DataSource.class);
Context.setDataSource(dataSource); Context.setDataSource(dataSource);
} }
}catch (NoSuchBeanDefinitionException e){ }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); Context.setConfig(config);

View File

@ -36,7 +36,6 @@ public class DataBase implements GraphService {
Runtime.getRuntime().addShutdownHook( Runtime.getRuntime().addShutdownHook(
new Thread(() -> { new Thread(() -> {
try { try {
log.info("kotime=>close database connections...");
if (null!=readConnection) { if (null!=readConnection) {
readConnection.close(); readConnection.close();
} }
@ -45,6 +44,8 @@ public class DataBase implements GraphService {
} }
} catch (SQLException throwables) { } catch (SQLException throwables) {
throwables.printStackTrace(); throwables.printStackTrace();
}finally {
log.info("kotime=>closed database connections...");
} }
}) })
); );
@ -78,8 +79,9 @@ public class DataBase implements GraphService {
if (null == methodNode) { if (null == methodNode) {
return; return;
} }
List<Map<String, Object>> query = DataBaseUtil.query(getWriteConnection(),KoSqlConstant.queryMethod, new Object[]{methodNode.getId()}); // List<Map<String, Object>> query = DataBaseUtil.query(getWriteConnection(),KoSqlConstant.queryMethod, new Object[]{methodNode.getId()});
if (query.size() == 0) { boolean existsById = DataBaseUtil.existsById(getWriteConnection(),KoSqlConstant.queryMethod, methodNode.getId());
if (!existsById) {
Object[] params = new Object[]{ Object[] params = new Object[]{
methodNode.getId(), methodNode.getId(),
methodNode.getName(), methodNode.getName(),
@ -107,8 +109,9 @@ public class DataBase implements GraphService {
@Override @Override
public synchronized void addExceptionNode(ExceptionNode exceptionNode) { public synchronized void addExceptionNode(ExceptionNode exceptionNode) {
List<Map<String, Object>> query = DataBaseUtil.query(getWriteConnection(),KoSqlConstant.queryException, new Object[]{exceptionNode.getId()}); // List<Map<String, Object>> query = DataBaseUtil.query(getWriteConnection(),KoSqlConstant.queryException, new Object[]{exceptionNode.getId()});
if (query.size() == 0) { boolean existsById = DataBaseUtil.existsById(getWriteConnection(),KoSqlConstant.queryException, exceptionNode.getId());
if (!existsById) {
Object[] params = new Object[]{ Object[] params = new Object[]{
exceptionNode.getId(), exceptionNode.getId(),
exceptionNode.getName(), exceptionNode.getName(),
@ -168,8 +171,9 @@ public class DataBase implements GraphService {
@Override @Override
public synchronized ExceptionRelation addExceptionRelation(MethodNode sourceMethodNode, ExceptionNode exceptionNode) { public synchronized ExceptionRelation addExceptionRelation(MethodNode sourceMethodNode, ExceptionNode exceptionNode) {
List<Map<String, Object>> query = DataBaseUtil.query(getWriteConnection(),KoSqlConstant.queryExceptionRe, new Object[]{sourceMethodNode.getId() + exceptionNode.getId()}); // List<Map<String, Object>> query = DataBaseUtil.query(getWriteConnection(),KoSqlConstant.queryExceptionRe, new Object[]{sourceMethodNode.getId() + exceptionNode.getId()});
if (query.size() == 0) { boolean existsById = DataBaseUtil.existsById(getWriteConnection(),KoSqlConstant.queryExceptionRe, sourceMethodNode.getId() + exceptionNode.getId());
if (!existsById) {
Object[] params = new Object[]{ Object[] params = new Object[]{
sourceMethodNode.getId() + exceptionNode.getId(), sourceMethodNode.getId() + exceptionNode.getId(),
sourceMethodNode.getId(), sourceMethodNode.getId(),

View File

@ -4,39 +4,37 @@ import cn.langpy.kotime.handler.InvokedHandler;
import cn.langpy.kotime.model.InvokedInfo; import cn.langpy.kotime.model.InvokedInfo;
import cn.langpy.kotime.util.Context; import cn.langpy.kotime.util.Context;
import java.util.LinkedList; import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.logging.Logger; import java.util.logging.Logger;
public class InvokedQueue { public class InvokedQueue {
public static Logger log = Logger.getLogger(InvokedQueue.class.toString()); public static Logger log = Logger.getLogger(InvokedQueue.class.toString());
private volatile static LinkedList<InvokedInfo> linkedList = new LinkedList(); private volatile static ConcurrentLinkedQueue<InvokedInfo> queue = new ConcurrentLinkedQueue();
public static void add(InvokedInfo invokedInfo) { public static void add(InvokedInfo invokedInfo) {
linkedList.add(invokedInfo); queue.add(invokedInfo);
} }
public static void onInveked() { public static void onInveked() {
while (true) { while (true) {
try { try {
InvokedInfo poll = null; if (queue.isEmpty()) {
synchronized (linkedList) { continue;
if (linkedList.size() > 0) { }
poll = linkedList.poll(); 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) { } catch (Exception e) {
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){
e.printStackTrace(); e.printStackTrace();
} }
} }
} }
} }

View File

@ -21,6 +21,7 @@ public class DataBaseUtil {
public static DataSource getDataSource() { public static DataSource getDataSource() {
return Context.getDataSource(); return Context.getDataSource();
} }
public static int insert(String sql, Object[] values) { public static int insert(String sql, Object[] values) {
try { try {
Connection connection = getDataSource().getConnection(); Connection connection = getDataSource().getConnection();
@ -110,6 +111,29 @@ public class DataBaseUtil {
return list; 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 <T> List<T> query(Connection connection, String sql, Object[] values, Class<T> c) { public static <T> List<T> query(Connection connection, String sql, Object[] values, Class<T> c) {
List<T> list = new ArrayList<>(); List<T> list = new ArrayList<>();
PreparedStatement statement = null; PreparedStatement statement = null;

View File

@ -66,7 +66,7 @@
{ {
"name": "ko-time.thread-num", "name": "ko-time.thread-num",
"type": "java.lang.Integer", "type": "java.lang.Integer",
"defaultValue": 10, "defaultValue": 2,
"description": "waiting to implement", "description": "waiting to implement",
"sourceType": "cn.langpy.kotime.config.DefaultConfig" "sourceType": "cn.langpy.kotime.config.DefaultConfig"
}, },