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>
<artifactId>ko-time</artifactId>
<version>2.2.0.BETA</version>
<version>2.2.0-PREPARE</version>
<name>koTime</name>
<description>koTime</description>
<licenses>

View File

@ -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);

View File

@ -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<Map<String, Object>> query = DataBaseUtil.query(getWriteConnection(),KoSqlConstant.queryMethod, new Object[]{methodNode.getId()});
if (query.size() == 0) {
// List<Map<String, Object>> 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<Map<String, Object>> query = DataBaseUtil.query(getWriteConnection(),KoSqlConstant.queryException, new Object[]{exceptionNode.getId()});
if (query.size() == 0) {
// List<Map<String, Object>> 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<Map<String, Object>> query = DataBaseUtil.query(getWriteConnection(),KoSqlConstant.queryExceptionRe, new Object[]{sourceMethodNode.getId() + exceptionNode.getId()});
if (query.size() == 0) {
// List<Map<String, Object>> 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(),

View File

@ -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<InvokedInfo> linkedList = new LinkedList();
private volatile static ConcurrentLinkedQueue<InvokedInfo> 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;
}
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());
if (null != poll.getException()) {
invokedHandler.onException(poll.getCurrent(), poll.getParent(), poll.getException(), poll.getNames(), poll.getValues());
}
}
}
}catch (Exception e){
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

View File

@ -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 <T> List<T> query(Connection connection, String sql, Object[] values, Class<T> c) {
List<T> list = new ArrayList<>();
PreparedStatement statement = null;

View File

@ -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"
},