提交 ba2b6c04 编写于 作者: zhouweidong's avatar zhouweidong

规则执行结果动态存储

上级 a156fddd
...@@ -12,8 +12,9 @@ public interface DbEntityMapper extends BaseMapper<EntityObj>{ ...@@ -12,8 +12,9 @@ public interface DbEntityMapper extends BaseMapper<EntityObj>{
List<EntityObj> search(@Param("sql") String sql, @Param("ew") Wrapper<EntityObj> wrapper); List<EntityObj> search(@Param("sql") String sql, @Param("ew") Wrapper<EntityObj> wrapper);
Integer searchCount(@Param("sql") String sql, @Param("ew") Wrapper<EntityObj> wrapper); Integer searchCount(@Param("sql") String sql, @Param("ew") Wrapper<EntityObj> wrapper);
int replaceBatch(List<ExecResult> var1); int replaceBatch(@Param("list") List<ExecResult> var1 , @Param("resultTableName") String tableName);
int clearBatch(List<ExecResult> var1); int replaceBatchByOracle(@Param("list") List<ExecResult> var1 , @Param("resultTableName") String tableName);
int clearBatch(@Param("list")List<ExecResult> var1, @Param("resultTableName") String tableName);
} }
\ No newline at end of file
package cn.ibizlab.core.extensions.service; package cn.ibizlab.core.extensions.service;
import cn.ibizlab.core.lite.extensions.service.DbEntityService; import cn.ibizlab.core.lite.extensions.service.DbEntityService;
import cn.ibizlab.core.rule.domain.ExecResult;
import cn.ibizlab.core.rule.service.impl.ExecResultServiceImpl; import cn.ibizlab.core.rule.service.impl.ExecResultServiceImpl;
import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import cn.ibizlab.core.rule.domain.ExecResult;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.context.annotation.Primary; import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import javax.sql.DataSource;
import java.util.*; import java.util.*;
/** /**
...@@ -18,16 +26,23 @@ import java.util.*; ...@@ -18,16 +26,23 @@ import java.util.*;
@Service("ExecResultExService") @Service("ExecResultExService")
public class ExecResultExService extends ExecResultServiceImpl { public class ExecResultExService extends ExecResultServiceImpl {
@Autowired
private DstDataSourceExService dstDataSourceService;
@Autowired @Autowired
private DbEntityService dbEntityService; private DbEntityService dbEntityService;
private ArrayList<ExecResult> resultsSaveAsync=new ArrayList<ExecResult>(); @Autowired
private Object objResultSaveAsyncLock=new Object(); @Lazy
private Timer timerSaveAsync=null; private DynamicRoutingDataSource dynamicRoutingDataSource;
private ArrayList<ExecResult> resultsDeleteAsync=new ArrayList<ExecResult>(); private Map<String, List<ExecResult>> resultsSaveAsync = new HashMap<>();
private Object objResultDeleteAsyncLock=new Object(); private Object objResultSaveAsyncLock = new Object();
private Timer timerDeleteAsync=null; private Timer timerSaveAsync = null;
private Map<String, List<ExecResult>> resultsDeleteAsync = new HashMap<>();
private Object objResultDeleteAsyncLock = new Object();
private Timer timerDeleteAsync = null;
@Override @Override
protected Class currentModelClass() { protected Class currentModelClass() {
...@@ -36,19 +51,18 @@ public class ExecResultExService extends ExecResultServiceImpl { ...@@ -36,19 +51,18 @@ public class ExecResultExService extends ExecResultServiceImpl {
/** /**
* [SaveAsync:SaveAsync] 行为扩展 * [SaveAsync:SaveAsync] 行为扩展
*
* @param et * @param et
* @return * @return
*/ */
@Override @Override
@Transactional @Transactional
public ExecResult saveAsync(ExecResult et) { public ExecResult saveAsync(ExecResult et) {
synchronized(objResultSaveAsyncLock) synchronized (objResultSaveAsyncLock) {
{ addExecResult(resultsSaveAsync, et);
this.resultsSaveAsync.add(et);
} }
//创建一个定时器 //创建一个定时器
if(timerSaveAsync==null) if (timerSaveAsync == null) {
{
timerSaveAsync = new Timer(); timerSaveAsync = new Timer();
//schedule方法是执行时间定时任务的方法 //schedule方法是执行时间定时任务的方法
timerSaveAsync.schedule(new TimerTask() { timerSaveAsync.schedule(new TimerTask() {
...@@ -56,42 +70,39 @@ public class ExecResultExService extends ExecResultServiceImpl { ...@@ -56,42 +70,39 @@ public class ExecResultExService extends ExecResultServiceImpl {
//run方法就是具体需要定时执行的任务 //run方法就是具体需要定时执行的任务
@Override @Override
public void run() { public void run() {
try try {
{ synchronized (objResultSaveAsyncLock) {
if (resultsSaveAsync.size() > 0) {
synchronized(objResultSaveAsyncLock) for (Map.Entry<String, List<ExecResult>> entry : resultsSaveAsync.entrySet()) {
{ String tabName = "";
if(resultsSaveAsync.size()>0) String dsName = entry.getKey();
{ List<ExecResult> execResults = entry.getValue();
List<ExecResult> args=new ArrayList<>(); List<ExecResult> tempExecResults = new ArrayList<>();
if (execResults.size() > 0) {
for(ExecResult arg:resultsSaveAsync) dstDataSourceService.initDataSource(dsName);
{ DynamicDataSourceContextHolder.push(dsName);
args.add(arg); for (ExecResult execResult : execResults) {
if(args.size()>=500) tempExecResults.add(execResult);
{ Object objTabName = execResult.get(RuleEngineExService.Setting_ResultTableName);
// TreatEarlyWarningThread task = new TreatEarlyWarningThread(args); if (StringUtils.isEmpty(tabName) && !ObjectUtils.isEmpty(objTabName)) {
// task.start(); tabName = String.valueOf(objTabName);
dbEntityService.saveResultBatch(args);
args.clear();
} }
if (tempExecResults.size() >= 500) {
saveResultBatch(tempExecResults, dsName, tabName);
} }
if(args.size()>0)
{
// TreatEarlyWarningThread task = new TreatEarlyWarningThread(args);
// task.start();
dbEntityService.saveResultBatch(args);
args.clear();
} }
resultsSaveAsync.clear(); if (tempExecResults.size() > 0) {
saveResultBatch(tempExecResults, dsName, tabName);
}
resultsSaveAsync.remove(dsName);
}
} }
} }
} }
catch (Exception ex) } catch (Exception ex) {
{
log.error(ex.getMessage()); log.error(ex.getMessage());
} finally {
DynamicDataSourceContextHolder.poll();
} }
} }
...@@ -102,18 +113,17 @@ public class ExecResultExService extends ExecResultServiceImpl { ...@@ -102,18 +113,17 @@ public class ExecResultExService extends ExecResultServiceImpl {
/** /**
* [DeleteAsync:DeleteAsync] 行为扩展 * [DeleteAsync:DeleteAsync] 行为扩展
*
* @param et * @param et
* @return * @return
*/ */
@Override @Override
@Transactional @Transactional
public ExecResult deleteAsync(ExecResult et) { public ExecResult deleteAsync(ExecResult et) {
synchronized(objResultDeleteAsyncLock) synchronized (objResultDeleteAsyncLock) {
{ addExecResult(resultsDeleteAsync, et);
this.resultsDeleteAsync.add(et);
} }
if(timerDeleteAsync==null) if (timerDeleteAsync == null) {
{
timerDeleteAsync = new Timer(); timerDeleteAsync = new Timer();
//schedule方法是执行时间定时任务的方法 //schedule方法是执行时间定时任务的方法
timerDeleteAsync.schedule(new TimerTask() { timerDeleteAsync.schedule(new TimerTask() {
...@@ -121,42 +131,39 @@ public class ExecResultExService extends ExecResultServiceImpl { ...@@ -121,42 +131,39 @@ public class ExecResultExService extends ExecResultServiceImpl {
//run方法就是具体需要定时执行的任务 //run方法就是具体需要定时执行的任务
@Override @Override
public void run() { public void run() {
try try {
{ synchronized (objResultDeleteAsyncLock) {
if (resultsDeleteAsync.size() > 0) {
synchronized(objResultDeleteAsyncLock) for (Map.Entry<String, List<ExecResult>> entry : resultsDeleteAsync.entrySet()) {
{ String tabName = "";
if(resultsDeleteAsync.size()>0) String dsName = entry.getKey();
{ List<ExecResult> execResults = entry.getValue();
List<ExecResult> args=new ArrayList<>(); List<ExecResult> tempExecResults = new ArrayList<>();
if (execResults.size() > 0) {
for(ExecResult arg:resultsDeleteAsync) dstDataSourceService.initDataSource(dsName);
{ DynamicDataSourceContextHolder.push(dsName);
args.add(arg); for (ExecResult execResult : execResults) {
if(args.size()>=500) tempExecResults.add(execResult);
{ Object objTabName = execResult.get(RuleEngineExService.Setting_ResultTableName);
// UpdateTreatEarlyWarningThread updateTreatEarlyWarningThread = new UpdateTreatEarlyWarningThread(args); if (StringUtils.isEmpty(tabName) && !ObjectUtils.isEmpty(objTabName)) {
// updateTreatEarlyWarningThread.start(); tabName = String.valueOf(objTabName);
dbEntityService.clearResultBatch(args);
args.clear();
} }
if (tempExecResults.size() >= 500) {
dbEntityService.clearResultBatch(tempExecResults, tabName);
} }
if(args.size()>=0)
{
// UpdateTreatEarlyWarningThread updateTreatEarlyWarningThread = new UpdateTreatEarlyWarningThread(args);
// updateTreatEarlyWarningThread.start();
dbEntityService.clearResultBatch(args);
args.clear();
} }
resultsDeleteAsync.clear(); if (tempExecResults.size() > 0) {
dbEntityService.clearResultBatch(tempExecResults, tabName);
} }
resultsDeleteAsync.remove(dsName);
} }
} }
catch (Exception ex) }
{ }
} catch (Exception ex) {
log.error(ex.getMessage()); log.error(ex.getMessage());
} finally {
DynamicDataSourceContextHolder.poll();
} }
} }
...@@ -164,5 +171,51 @@ public class ExecResultExService extends ExecResultServiceImpl { ...@@ -164,5 +171,51 @@ public class ExecResultExService extends ExecResultServiceImpl {
} }
return et; return et;
} }
/**
* 将规则执行结果添加到待保存集合中
* @param execResultMap
* @param execResult
*/
private void addExecResult(Map<String, List<ExecResult>> execResultMap, ExecResult execResult) {
Object dsName = execResult.get(RuleEngineExService.Setting_ResultDataSource);
Object tabName = execResult.get(RuleEngineExService.Setting_ResultTableName);
if (ObjectUtils.isEmpty(tabName)) {
execResult.set(RuleEngineExService.Setting_ResultTableName, RuleEngineExService.Default_ResultTableName);
}
if (ObjectUtils.isEmpty(dsName)) {
dsName = RuleEngineExService.Default_ResultDataSource;
}
String dataSource = String.valueOf(dsName);
if (execResultMap.containsKey(dataSource)) {
execResultMap.get(dataSource).add(execResult);
} else {
execResultMap.put(dataSource, new ArrayList() {{
add(execResult);
}});
}
}
/**
* 根据数据源类型保存结果
* @param args
* @param
*/
private void saveResultBatch(List<ExecResult> args, String dsName, String tabName) {
Map<String, DataSource> dynamicDSMap = dynamicRoutingDataSource.getCurrentDataSources();
if (!ObjectUtils.isEmpty(dynamicDSMap) && dynamicDSMap.containsKey(dsName)) {
DataSource ds = dynamicDSMap.get(dsName);
if (ds instanceof DruidDataSource) {
DruidDataSource druidDataSource = (DruidDataSource) dynamicDSMap.get(dsName);
if ("oracle".equals(druidDataSource.getDbType())) {
dbEntityService.saveResultBatchByOracle(args, tabName);
args.clear();
return;
}
}
}
dbEntityService.saveResultBatch(args, tabName);
args.clear();
}
} }
...@@ -60,6 +60,8 @@ public class RuleEngineExService extends RuleEngineServiceImpl { ...@@ -60,6 +60,8 @@ public class RuleEngineExService extends RuleEngineServiceImpl {
public static final String Setting_ResultDataSource = "resultDataSource"; public static final String Setting_ResultDataSource = "resultDataSource";
public static final String Setting_ResultTableName = "resultTableName"; public static final String Setting_ResultTableName = "resultTableName";
public static final String Default_ResultDataSource = "default";
public static final String Default_ResultTableName = "IBZRULERESULT";
@Autowired @Autowired
private DbEntityService dbEntityService; private DbEntityService dbEntityService;
......
...@@ -166,20 +166,31 @@ public class DbEntityService extends ServiceImpl<DbEntityMapper, EntityObj> impl ...@@ -166,20 +166,31 @@ public class DbEntityService extends ServiceImpl<DbEntityMapper, EntityObj> impl
} }
} }
@Override
public int saveResultBatch(List<ExecResult> var1) {
return 0;
}
@Override
public int clearResultBatch(List<ExecResult> var1) {
return 0;
}
@Autowired @Autowired
private DbEntityMapper dbEntityMapper; private DbEntityMapper dbEntityMapper;
@Override public int saveResultBatch(List<ExecResult> var1,String tableName)
public int saveResultBatch(List<ExecResult> var1)
{ {
return dbEntityMapper.replaceBatch(var1); return dbEntityMapper.replaceBatch(var1,tableName);
} }
@Override public int saveResultBatchByOracle(List<ExecResult> var1,String tableName) {
public int clearResultBatch(List<ExecResult> var1) return dbEntityMapper.replaceBatchByOracle(var1,tableName);
}
public int clearResultBatch(List<ExecResult> var1,String tableName)
{ {
return dbEntityMapper.clearBatch(var1); return dbEntityMapper.clearBatch(var1,tableName);
} }
@Autowired @Autowired
......
...@@ -15,9 +15,9 @@ ...@@ -15,9 +15,9 @@
<if test="ew!=null and ew.sqlSegment!=null and ew.emptyOfWhere">${ew.sqlSegment}</if> <if test="ew!=null and ew.sqlSegment!=null and ew.emptyOfWhere">${ew.sqlSegment}</if>
</select> </select>
<insert id="replaceBatch" parameterType="java.util.List" databaseId="mysql"> <insert id="replaceBatch" parameterType="java.util.List">
<foreach collection="list" item="item" index="index" separator=";"> <foreach collection="list" item="item" index="index" separator=";">
INSERT INTO IBZRULERESULT INSERT INTO ${resultTableName}
(ru_execresultid,ru_execresultname, createdate, updatedate, ruleid, rulename, retvalue, keyvaluefield,dimfield, metricfield,domainsfield,timefield,ext1field,ext2field, businesscat ) (ru_execresultid,ru_execresultname, createdate, updatedate, ruleid, rulename, retvalue, keyvaluefield,dimfield, metricfield,domainsfield,timefield,ext1field,ext2field, businesscat )
VALUES VALUES
(#{item.id}, #{item.name}, now(), now(), #{item.ruleId}, #{item.ruleName}, #{item.retValue}, #{item.keyValueField}, #{item.dimField}, #{item.metricField}, (#{item.id}, #{item.name}, now(), now(), #{item.ruleId}, #{item.ruleName}, #{item.retValue}, #{item.keyValueField}, #{item.dimField}, #{item.metricField},
...@@ -30,8 +30,8 @@ ...@@ -30,8 +30,8 @@
</insert> </insert>
<!--t1.BUSINESSCAT, t1.CREATEDATE, t1.DIMFIELD, t1.DOMAINSFIELD, t1.EXT1FIELD, t1.EXT2FIELD, t1.KEYVALUEFIELD, t1.METRICFIELD, <!--t1.BUSINESSCAT, t1.CREATEDATE, t1.DIMFIELD, t1.DOMAINSFIELD, t1.EXT1FIELD, t1.EXT2FIELD, t1.KEYVALUEFIELD, t1.METRICFIELD,
t1.RETVALUE, t1.RULEID, t1.RULENAME, t1.RU_EXECRESULTID, t1.RU_EXECRESULTNAME, t1.SYSTEMID, t1.TIMEFIELD, t1.UPDATEDATE--> t1.RETVALUE, t1.RULEID, t1.RULENAME, t1.RU_EXECRESULTID, t1.RU_EXECRESULTNAME, t1.SYSTEMID, t1.TIMEFIELD, t1.UPDATEDATE-->
<insert id="replaceBatch" parameterType="java.util.List" databaseId="oracle" > <insert id="replaceBatchByOracle" parameterType="java.util.List">
MERGE INTO IBZRULERESULT A USING ( MERGE INTO ${resultTableName} A USING (
<foreach collection='list' item='item' index='index' separator='UNION ALL'> <foreach collection='list' item='item' index='index' separator='UNION ALL'>
select #{item.id} as ru_execresultid, #{item.name} as ru_execresultname, sysdate as createdate, sysdate as updatedate, #{item.ruleId} as ruleid, #{item.ruleName} as rulename,#{item.retValue} as retvalue, #{item.keyValueField} as keyvaluefield, #{item.dimField} as dimfield, select #{item.id} as ru_execresultid, #{item.name} as ru_execresultname, sysdate as createdate, sysdate as updatedate, #{item.ruleId} as ruleid, #{item.ruleName} as rulename,#{item.retValue} as retvalue, #{item.keyValueField} as keyvaluefield, #{item.dimField} as dimfield,
#{item.metricField} as metricfield,#{item.domainsField} as domainsfield,#{item.timeField} as timefield, #{item.ext1Field} as ext1field,#{item.ext2Field} as ext2field,#{item.businessCat} as businesscat from dual #{item.metricField} as metricfield,#{item.domainsField} as domainsfield,#{item.timeField} as timefield, #{item.ext1Field} as ext1field,#{item.ext2Field} as ext2field,#{item.businessCat} as businesscat from dual
...@@ -45,7 +45,7 @@ ...@@ -45,7 +45,7 @@
</insert> </insert>
<delete id="clearBatch" parameterType="java.util.List" > <delete id="clearBatch" parameterType="java.util.List" >
delete from IBZRULERESULT where (ru_execresultid) in delete from ${resultTableName} where (ru_execresultid) in
<foreach collection="list" item="item" index="index" open="(" close=")" separator=","> <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
(#{item.id}) (#{item.id})
</foreach> </foreach>
......
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册