Commit a56dedd6 authored by yanzg's avatar yanzg

修复等待时间

parent b8b78355
package com.yanzuoguang.util.helper;
import com.yanzuoguang.util.log.Log;
import com.yanzuoguang.util.thread.ThreadHelper;
import com.yanzuoguang.util.vo.Ref;
/**
* 超时监控
*
* @author 颜佐光
*/
public class YzgTimeout {
public static final long TIME_OUT_DEFAULT = 5000;
public static final int TIME_OUT_TIP = 1000;
/**
* 超时监控
*
* @param cls 日志类
* @param message 消息
* @param runnable 运行函数
*/
public static void timeOut(Class<?> cls, String message, Runnable runnable) {
final Ref<Boolean> isRun = new Ref<>(false);
ThreadHelper.runThread(() -> {
try {
long timeMax = TIME_OUT_DEFAULT;
int timeUnit = 10;
long start = System.currentTimeMillis();
do {
ThreadHelper.sleep(timeUnit);
long end = System.currentTimeMillis();
if (end - start > timeMax) {
timeUnit = TIME_OUT_TIP;
Log.error(cls, message + "超时,正在等待执行完成");
}
} while (!isRun.value);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
Log.info(cls, message + "完成");
}
});
runnable.run();
isRun.value = true;
}
}
......@@ -11,9 +11,12 @@ import com.yanzuoguang.util.cache.MemoryCache;
import com.yanzuoguang.util.helper.ArrayHelper;
import com.yanzuoguang.util.helper.StringFormatHandle;
import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.vo.*;
import com.yanzuoguang.util.vo.MapRow;
import com.yanzuoguang.util.vo.PageSizeData;
import com.yanzuoguang.util.vo.PageSizeReq;
import com.yanzuoguang.util.vo.PageSizeReqVo;
import org.springframework.beans.factory.annotation.Autowired;
import javax.annotation.Resource;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
......@@ -69,7 +72,7 @@ public abstract class BaseDaoSql {
this.init();
}
@Resource
@Autowired
public void setDb(DbExecute db) {
this.db = db;
}
......@@ -87,8 +90,8 @@ public abstract class BaseDaoSql {
return table;
}
protected void checkTable(){
if(this.table == null){
protected void checkTable() {
if (this.table == null) {
throw YzgError.getRuntimeException("002");
}
}
......
......@@ -2,18 +2,15 @@ package com.yanzuoguang.db.impl;
import com.yanzuoguang.db.ConfigDb;
import com.yanzuoguang.db.DbExecute;
import com.yanzuoguang.util.helper.YzgTimeout;
import com.yanzuoguang.util.vo.MapRow;
import com.yanzuoguang.util.vo.Ref;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Lazy;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
......@@ -25,18 +22,18 @@ import java.util.List;
@Component
public class DbExecuteImpl implements DbExecute {
@Resource
@Qualifier("jdbcTemplate")
private JdbcTemplate jdbc;
private final JdbcTemplate jdbcTemplate;
private final DbPrintSql printSql;
private final ConfigDb configDb;
@Autowired
private DbPrintSql printSql;
@Autowired
private ConfigDb configDb;
public DbExecuteImpl(JdbcTemplate jdbcTemplate, DbPrintSql printSql, ConfigDb configDb) {
this.jdbcTemplate = jdbcTemplate;
this.printSql = printSql;
this.configDb = configDb;
}
public JdbcTemplate getJdbc() {
return jdbc;
return jdbcTemplate;
}
/**
......@@ -50,14 +47,15 @@ public class DbExecuteImpl implements DbExecute {
*/
@Override
public int update(Class targetClass, String sqlName, String sql, Object... paras) {
int row = 0;
Ref<Integer> row = new Ref<>(0);
long start = System.currentTimeMillis();
try {
sql = this.handleParas(sql, paras);
row = this.getJdbc().update(sql, paras);
return row;
String finalSql = sql;
YzgTimeout.timeOut(DbExecuteImpl.class, sqlName, () -> row.value = getJdbc().update(finalSql, paras));
return row.value;
} finally {
printSql.print(targetClass, sqlName, start, row, sql, paras);
printSql.print(targetClass, sqlName, start, row.value, sql, paras);
}
}
......@@ -78,16 +76,16 @@ public class DbExecuteImpl implements DbExecute {
long start = System.currentTimeMillis();
try {
sql = this.handleParas(sql, paras);
RowCallbackHandler rowCallbackHandler = new RowCallbackHandler() {
@Override
public void processRow(ResultSet rs) throws SQLException {
String finalSql = sql;
YzgTimeout.timeOut(DbExecuteImpl.class, sqlName, () -> {
RowCallbackHandler rowCallbackHandler = rs -> {
AllBeanRowMapper<T> rowMap = AllBeanRowMapper.getInstance(cls, configDb);
T data = rowMap.mapRow(rs, row.value);
rowHandle.handle(data);
row.value++;
}
};
this.getJdbc().query(sql, rowCallbackHandler, paras);
this.getJdbc().query(finalSql, rowCallbackHandler, paras);
});
} finally {
printSql.print(targetClass, sqlName, start, row.value, sql, paras);
}
......@@ -111,12 +109,17 @@ public class DbExecuteImpl implements DbExecute {
long start = System.currentTimeMillis();
try {
sql = this.handleParas(sql, paras);
List<T> ret = this.getJdbc().query(sql, paras, AllBeanRowMapper.getInstance(cls, configDb));
if (ret == null) {
ret = new ArrayList<T>();
Ref<List<T>> ret = new Ref<>(null);
String finalSql = sql;
YzgTimeout.timeOut(cls, sqlName, () -> {
ret.value = this.getJdbc().query(finalSql, paras, AllBeanRowMapper.getInstance(cls, configDb));
});
if (ret.value == null) {
ret.value = new ArrayList<T>();
}
row = ret.size();
return ret;
row = ret.value.size();
return ret.value;
} finally {
printSql.print(targetClass, sqlName, start, row, sql, paras);
}
......@@ -147,18 +150,24 @@ public class DbExecuteImpl implements DbExecute {
*/
@Override
public Object queryCell(Class targetClass, String sqlName, String sql, Object... paras) {
int row = 0;
Ref<Integer> row = new Ref<>(0);
long start = System.currentTimeMillis();
try {
sql = this.handleParas(sql, paras);
SqlRowSet rowSet = this.getJdbc().queryForRowSet(sql, paras);
Ref<Object> ret = new Ref<>(null);
String finalSql = sql;
YzgTimeout.timeOut(DbExecuteImpl.class, sqlName, () -> {
SqlRowSet rowSet = this.getJdbc().queryForRowSet(finalSql, paras);
while (rowSet.next()) {
row = 1;
return rowSet.getObject(1);
row.value = 1;
ret.value = rowSet.getObject(1);
break;
}
});
return null;
} finally {
printSql.print(targetClass, sqlName, start, row, sql, paras);
printSql.print(targetClass, sqlName, start, row.value, sql, paras);
}
}
......@@ -170,11 +179,6 @@ public class DbExecuteImpl implements DbExecute {
* @return
*/
protected String handleParas(String sql, Object... paras) {
return sql.replaceAll("1\\s*?=\\s*?1\\s*?(?i)AND", "")
.replaceAll("(?i)WHERE\\s*?1\\s*?=\\s*?1", "")
.replaceAll("((?i)ORDER\\s*?(?i)BY\\s*?)1\\s*?,", "$1")
.replaceAll("(?i)ORDER\\s*?(?i)BY\\s*?1\\s*?", "")
.replaceAll("((?i)GROUP\\s*?(?i)BY\\s*?)1\\s*?,", "$1")
.replaceAll("(?i)GROUP\\s*?(?i)BY\\s*?1\\s*?", "");
return sql.replaceAll("1\\s*?=\\s*?1\\s*?(?i)AND", "").replaceAll("(?i)WHERE\\s*?1\\s*?=\\s*?1", "").replaceAll("((?i)ORDER\\s*?(?i)BY\\s*?)1\\s*?,", "$1").replaceAll("(?i)ORDER\\s*?(?i)BY\\s*?1\\s*?", "").replaceAll("((?i)GROUP\\s*?(?i)BY\\s*?)1\\s*?,", "$1").replaceAll("(?i)GROUP\\s*?(?i)BY\\s*?1\\s*?", "");
}
}
......@@ -3,9 +3,7 @@ package com.yanzuoguang.mq.dao;
import com.yanzuoguang.dao.DaoConst;
import com.yanzuoguang.util.YzgError;
import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.log.Log;
import com.yanzuoguang.util.thread.ThreadHelper;
import com.yanzuoguang.util.vo.Ref;
import com.yanzuoguang.util.helper.YzgTimeout;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;
......@@ -113,7 +111,7 @@ public class BeanDao {
ex.printStackTrace();
}
if (isCreateQueue) {
this.timeOut(Queue.class, queueName, "创建队列", () -> amqpAdmin.declareQueue(queueNew));
YzgTimeout.timeOut(BeanDao.class, "创建队列" + queueName, () -> amqpAdmin.declareQueue(queueNew));
}
// 将实体注册到上下文中
register(key, queueNew);
......@@ -137,7 +135,7 @@ public class BeanDao {
// 创建实体
TopicExchange bean = new TopicExchange(exchangeName, true, false);
this.timeOut(TopicExchange.class, bean.getName(), "创建交换器", () -> amqpAdmin.declareExchange(bean));
YzgTimeout.timeOut(BeanDao.class, "创建交换器" + exchangeName, () -> amqpAdmin.declareExchange(bean));
// 将实体注册到上下文中
register(key, bean);
......@@ -155,7 +153,7 @@ public class BeanDao {
*/
public Binding createBinding(String exchangeName, String queueName, String routeKey) {
Binding binding = BindingBuilder.bind(getQueue(queueName)).to(getExchange(exchangeName)).with(routeKey);
this.timeOut(TopicExchange.class, binding.getRoutingKey(), "创建路由绑定", () -> amqpAdmin.declareBinding(binding));
YzgTimeout.timeOut(BeanDao.class, "创建路由绑定" + routeKey, () -> amqpAdmin.declareBinding(binding));
return binding;
}
......@@ -191,31 +189,5 @@ public class BeanDao {
defaultListableBeanFactory.registerSingleton(name, target);
}
private void timeOut(Class<?> cls, String name, String message, Runnable runnable) {
final Ref<Boolean> isRun = new Ref<>(false);
ThreadHelper.runThread(() -> {
try {
long timeMax = 1000;
int timeUnit = 10;
long start = System.currentTimeMillis();
do {
ThreadHelper.sleep(timeUnit);
long end = System.currentTimeMillis();
if (end - start > timeMax) {
timeUnit = 1000;
Log.error(cls, message + name + "超时,正在等待执行完成");
}
}
while (!isRun.value);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
Log.info(cls, message + name + "完成");
}
});
runnable.run();
isRun.value = true;
}
}
......@@ -5,6 +5,7 @@ import com.yanzuoguang.dao.impl.BaseDaoImpl;
import com.yanzuoguang.dao.impl.SqlData;
import com.yanzuoguang.mq.dao.MessageDao;
import com.yanzuoguang.mq.vo.MessageVo;
import com.yanzuoguang.util.helper.YzgTimeout;
import com.yanzuoguang.util.vo.MapRow;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
......@@ -64,12 +65,14 @@ public class MessageDaoImpl extends BaseDaoImpl implements MessageDao, Initializ
*/
@Override
public void afterPropertiesSet() throws Exception {
YzgTimeout.timeOut(MessageDaoImpl.class, "消息队列处理工具类初始化", () -> {
List<MapRow> tables = this.getDb().query(MessageDaoImpl.class, "QUERY_TABLE_SQL", QUERY_TABLE_SQL);
if (tables.isEmpty()) {
this.getDb().update(MessageDaoImpl.class, "CREATE_TABLE_SQL", CREATE_TABLE_SQL);
} else {
this.getDb().update(MessageDaoImpl.class, "ALTER_TABLE_SQL", ALTER_TABLE_SQL);
}
});
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment