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

规则执行结果动态存储

上级 f4d7bd11
......@@ -2,11 +2,8 @@ package cn.ibizlab.core.extensions.service;
import cn.ibizlab.core.lite.extensions.service.DbEntityService;
import cn.ibizlab.core.rule.domain.ExecResult;
import cn.ibizlab.core.rule.domain.RuleEngine;
import cn.ibizlab.core.rule.service.IRuleEngineService;
import cn.ibizlab.core.rule.service.impl.ExecResultServiceImpl;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
......@@ -14,8 +11,6 @@ import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import java.util.*;
/**
......@@ -70,23 +65,23 @@ public class ExecResultExService extends ExecResultServiceImpl {
synchronized (objResultSaveAsyncLock) {
if (resultsSaveAsync.size() > 0) {
for (Map.Entry<String, List<ExecResult>> entry : resultsSaveAsync.entrySet()) {
String engineId = entry.getKey();
String dsName = getDSNameByEngineId(engineId);
String execResultId = entry.getKey();
String [] idArray = execResultId.split("\\|");
List<ExecResult> execResults = entry.getValue();
List<ExecResult> tempExecResults = new ArrayList<>();
if (execResults.size() > 0) {
if (execResults.size() > 0 && idArray.length==3) {
for (ExecResult execResult : execResults) {
tempExecResults.add(execResult);
if (tempExecResults.size() >= 500) {
dbEntityService.saveResultBatch(tempExecResults, dsName);
dbEntityService.saveResultBatch(tempExecResults, idArray[1] , idArray[2]);
tempExecResults.clear();
}
}
if (tempExecResults.size() > 0) {
dbEntityService.saveResultBatch(tempExecResults, dsName);
dbEntityService.saveResultBatch(tempExecResults, idArray[1] , idArray[2]);
tempExecResults.clear();
}
resultsSaveAsync.remove(engineId);
resultsSaveAsync.remove(execResultId);
}
}
}
......@@ -124,23 +119,23 @@ public class ExecResultExService extends ExecResultServiceImpl {
synchronized (objResultDeleteAsyncLock) {
if (resultsDeleteAsync.size() > 0) {
for (Map.Entry<String, List<ExecResult>> entry : resultsDeleteAsync.entrySet()) {
String engineId = entry.getKey();
String dsName = getDSNameByEngineId(engineId);
String execResultId = entry.getKey();
List<ExecResult> execResults = entry.getValue();
List<ExecResult> tempExecResults = new ArrayList<>();
if (execResults.size() > 0) {
String [] idArray = execResultId.split("|");
if (execResults.size() > 0 && idArray.length==3) {
for (ExecResult execResult : execResults) {
tempExecResults.add(execResult);
if (tempExecResults.size() >= 500) {
dbEntityService.clearResultBatch(tempExecResults, dsName);
dbEntityService.clearResultBatch(tempExecResults, idArray[1] , idArray[2]);
tempExecResults.clear();
}
}
if (tempExecResults.size() > 0) {
dbEntityService.clearResultBatch(tempExecResults, dsName);
dbEntityService.clearResultBatch(tempExecResults, idArray[1] , idArray[2]);
tempExecResults.clear();
}
resultsDeleteAsync.remove(engineId);
resultsDeleteAsync.remove(execResultId);
}
}
}
......@@ -154,30 +149,6 @@ public class ExecResultExService extends ExecResultServiceImpl {
return et;
}
/**
* 通过引擎获取数据源信息
*
* @param engineId
* @return
*/
private String getDSNameByEngineId(String engineId) {
String dsName = "";
RuleEngine engine = engineService.get(engineId);
if (!ObjectUtils.isEmpty(engine.getExtParams())) {
JSONArray array = JSONArray.parseArray(engine.getExtParams());
for (int a = 0; a < array.size(); a++) {
JSONObject engineParam = array.getJSONObject(a);
String property = engineParam.getString("property");
String value = engineParam.getString("value");
if (!StringUtils.isEmpty(property) && RuleEngineExService.Setting_ResultDataSource.equalsIgnoreCase(property) && !StringUtils.isEmpty(value)) {
return value;
}
}
}
return dsName;
}
/**
* 将规则执行结果添加到待保存集合中
*
......@@ -186,15 +157,17 @@ public class ExecResultExService extends ExecResultServiceImpl {
*/
private void addExecResult(Map<String, List<ExecResult>> execResultMap, ExecResult execResult) {
Object engineId = execResult.get(BaseEntityServiceImpl.Tag_EngineId);
Object dsName = execResult.get(RuleEngineExService.Setting_ResultDataSource);
Object tabName = execResult.get(RuleEngineExService.Setting_ResultTableName);
String id = String.format("%s|%s|%s",engineId,dsName,tabName);
if (ObjectUtils.isEmpty(engineId)) {
log.error("存储规则结果失败,无法数据[{}]的引擎标识!", execResult.getId());
return;
}
String strEngine = String.valueOf(engineId);
if (execResultMap.containsKey(engineId)) {
execResultMap.get(engineId).add(execResult);
if (execResultMap.containsKey(id)) {
execResultMap.get(id).add(execResult);
} else {
execResultMap.put(strEngine, new ArrayList() {{
execResultMap.put(id, new ArrayList() {{
add(execResult);
}});
}
......
......@@ -2,7 +2,6 @@ package cn.ibizlab.core.lite.extensions.service;
import cn.ibizlab.core.extensions.mapper.DbEntityMapper;
import cn.ibizlab.core.extensions.service.DstDataSourceExService;
import cn.ibizlab.core.extensions.service.RuleEngineExService;
import cn.ibizlab.core.lite.extensions.domain.EntityModel;
import cn.ibizlab.core.lite.extensions.domain.EntityObj;
import cn.ibizlab.core.lite.extensions.domain.FieldModel;
......@@ -170,15 +169,17 @@ public class DbEntityService extends ServiceImpl<DbEntityMapper, EntityObj> impl
@Autowired
private DbEntityMapper dbEntityMapper;
public int saveResultBatch(List<ExecResult> execResults, String dsName) {
public int saveResultBatch(List<ExecResult> execResults, String dsName ,String tableName) {
int result = 0;
try {
if(!ObjectUtils.isEmpty(dsName)){
if(!ObjectUtils.isEmpty(dsName) && !dsName.equalsIgnoreCase("null")){
dstDataSourceService.initDataSource(dsName);
DynamicDataSourceContextHolder.push(dsName);
}
if(StringUtils.isEmpty(tableName) || tableName.equalsIgnoreCase("null")){
tableName = Default_ResultTableName;
}
Map<String, DataSource> dynamicDSMap = dynamicRoutingDataSource.getCurrentDataSources();
String tableName = getTableName(execResults);
if (!ObjectUtils.isEmpty(dynamicDSMap) && dynamicDSMap.containsKey(dsName)) {
DataSource ds = dynamicDSMap.get(dsName);
if (ds instanceof DruidDataSource) {
......@@ -197,25 +198,9 @@ public class DbEntityService extends ServiceImpl<DbEntityMapper, EntityObj> impl
return result;
}
/**
* 获取规则结果表名
* @param execResults
* @return
*/
private String getTableName(List<ExecResult> execResults) {
String tableName = Default_ResultTableName;
for(ExecResult execResult : execResults){
if(!ObjectUtils.isEmpty(execResult.get(RuleEngineExService.Setting_ResultTableName))){
return String.valueOf(execResult.get(RuleEngineExService.Setting_ResultTableName));
}
}
return tableName;
}
public int clearResultBatch(List<ExecResult> execResults, String dsName) {
public int clearResultBatch(List<ExecResult> execResults, String dsName ,String tableName) {
int result = 0;
try {
String tableName = getTableName(execResults);
dstDataSourceService.initDataSource(dsName);
DynamicDataSourceContextHolder.push(dsName);
result = dbEntityMapper.clearBatch(execResults, tableName);
......
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册