|
|
|
@ -0,0 +1,479 @@
|
|
|
|
|
package cn.estsh.i3plus.ext.mes.apiservice.dbinterface;
|
|
|
|
|
|
|
|
|
|
import cn.estsh.i3plus.mes.apiservice.util.datatable.DataSet;
|
|
|
|
|
import cn.estsh.i3plus.platform.common.tool.TimeTool;
|
|
|
|
|
import cn.estsh.i3plus.platform.common.util.MesConstWords;
|
|
|
|
|
import cn.estsh.i3plus.platform.plugin.datasource.DynamicDataSourceProxy;
|
|
|
|
|
import cn.estsh.i3plus.pojo.base.codemaker.SnowflakeIdMaker;
|
|
|
|
|
import cn.estsh.i3plus.pojo.mes.dbinterface.MesInterfaceDataMapper;
|
|
|
|
|
import cn.estsh.i3plus.pojo.mes.dbinterface.MesInterfaceEnumUtil;
|
|
|
|
|
import cn.estsh.impp.framework.boot.util.ImppRedis;
|
|
|
|
|
import cn.estsh.impp.framework.boot.util.SpringContextsUtil;
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
|
import lombok.NoArgsConstructor;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
|
import java.sql.Connection;
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
|
import java.time.Duration;
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @Description : 接口数据适配器
|
|
|
|
|
* 从接口规则表中读取所有需要转换的接口表信息并调用读取类进行数据获取,再调用转换类进行转换,最后调用写入类把数据保存到业务表
|
|
|
|
|
* @Reference :
|
|
|
|
|
* @Author : rock.yu
|
|
|
|
|
* @CreateDate : 2019-06-11 19:34
|
|
|
|
|
* @Modify: 1.
|
|
|
|
|
**/
|
|
|
|
|
@Component
|
|
|
|
|
@NoArgsConstructor
|
|
|
|
|
public class MesSAPDbAdapter {
|
|
|
|
|
|
|
|
|
|
public static final Logger LOGGER = LoggerFactory.getLogger(WmsSAPDbWriter.class);
|
|
|
|
|
|
|
|
|
|
@Resource(name = "yfasDataSource")
|
|
|
|
|
private DynamicDataSourceProxy sapDataSourceProxy;
|
|
|
|
|
|
|
|
|
|
@Resource(name = "mesDataSource")
|
|
|
|
|
private DynamicDataSourceProxy mesDataSourceProxy;
|
|
|
|
|
|
|
|
|
|
@Value("${sync.redis.time:1800}")
|
|
|
|
|
private Integer redisTime;
|
|
|
|
|
|
|
|
|
|
private WmsSAPDbExpression wmsSAPDbExpression;
|
|
|
|
|
|
|
|
|
|
private WmsSAPDbWriter writer;
|
|
|
|
|
|
|
|
|
|
private WmsSAPDbReader reader;
|
|
|
|
|
|
|
|
|
|
private WmsSAPDbTranslator translator;
|
|
|
|
|
|
|
|
|
|
private MesSAPDbDataMapper dbDataMapper;
|
|
|
|
|
|
|
|
|
|
private Connection srcConn;
|
|
|
|
|
private Connection destConn;
|
|
|
|
|
private Connection mesConn;
|
|
|
|
|
|
|
|
|
|
@Resource(name = "redisMes")
|
|
|
|
|
private ImppRedis redisMes;
|
|
|
|
|
|
|
|
|
|
// 获取 IMPP ID
|
|
|
|
|
@Autowired
|
|
|
|
|
private SnowflakeIdMaker snowflakeIdMaker;
|
|
|
|
|
|
|
|
|
|
private WmsSAPDbWriter buildWriter(String groupName,
|
|
|
|
|
DynamicDataSourceProxy sapDataSourceProxy,
|
|
|
|
|
DynamicDataSourceProxy mesDataSourceProxy) throws Exception {
|
|
|
|
|
|
|
|
|
|
WmsSAPDbWriter wmsSAPDbWriter = null;
|
|
|
|
|
|
|
|
|
|
if (groupName.equals(MesInterfaceEnumUtil.DIRECTION_TYPE.SAP2MES.getName())) {
|
|
|
|
|
wmsSAPDbWriter = new WmsSAPDbWriter(sapDataSourceProxy, mesDataSourceProxy);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (groupName.equals(MesInterfaceEnumUtil.DIRECTION_TYPE.MES2SAP.getName())) {
|
|
|
|
|
wmsSAPDbWriter = new WmsSAPDbWriter(mesDataSourceProxy, sapDataSourceProxy);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// new 的对象需要手工注入 bean
|
|
|
|
|
if (SpringContextsUtil.getApplicationContext() != null) {
|
|
|
|
|
SpringContextsUtil.getApplicationContext().
|
|
|
|
|
getAutowireCapableBeanFactory().autowireBean(wmsSAPDbWriter);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.srcConn = wmsSAPDbWriter.getSrcDataSourceProxy().getWriteConnectionWithoutPool();
|
|
|
|
|
this.destConn = wmsSAPDbWriter.getDestDataSourceProxy().getWriteConnectionWithoutPool();
|
|
|
|
|
this.mesConn = mesDataSourceProxy.getWriteConnectionWithoutPool();
|
|
|
|
|
|
|
|
|
|
if (wmsSAPDbWriter != null) {
|
|
|
|
|
wmsSAPDbExpression = new WmsSAPDbExpression();
|
|
|
|
|
wmsSAPDbExpression.setSrcConn(mesConn);
|
|
|
|
|
// new 的对象需要手工注入 bean
|
|
|
|
|
if (SpringContextsUtil.getApplicationContext() != null) {
|
|
|
|
|
SpringContextsUtil.getApplicationContext().
|
|
|
|
|
getAutowireCapableBeanFactory().autowireBean(wmsSAPDbExpression);
|
|
|
|
|
}
|
|
|
|
|
wmsSAPDbWriter.setWmsSAPDbExpression(wmsSAPDbExpression);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return wmsSAPDbWriter;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private WmsSAPDbReader buildReader(String groupName,
|
|
|
|
|
DynamicDataSourceProxy sapDataSourceProxy,
|
|
|
|
|
DynamicDataSourceProxy mesDataSourceProxy) {
|
|
|
|
|
|
|
|
|
|
WmsSAPDbReader wmsSAPDbReader = null;
|
|
|
|
|
|
|
|
|
|
if (groupName.equals(MesInterfaceEnumUtil.DIRECTION_TYPE.SAP2MES.getName())) {
|
|
|
|
|
wmsSAPDbReader = new WmsSAPDbReader(sapDataSourceProxy);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (groupName.equals(MesInterfaceEnumUtil.DIRECTION_TYPE.MES2SAP.getName())) {
|
|
|
|
|
wmsSAPDbReader = new WmsSAPDbReader(mesDataSourceProxy);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if (groupName.equals(MesInterfaceEnumUtil.DIRECTION_TYPE.WMS2MES.getName())) {
|
|
|
|
|
// wmsSAPDbReader = new WmsSAPDbReader(mesDataSourceProxy);
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// if (groupName.equals(MesInterfaceEnumUtil.DIRECTION_TYPE.MES2WMS.getName())) {
|
|
|
|
|
// wmsSAPDbReader = new WmsSAPDbReader(mesDataSourceProxy);
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (wmsSAPDbReader != null && SpringContextsUtil.getApplicationContext() != null) {
|
|
|
|
|
SpringContextsUtil.getApplicationContext().
|
|
|
|
|
getAutowireCapableBeanFactory().autowireBean(wmsSAPDbReader);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return wmsSAPDbReader;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String getTime() {
|
|
|
|
|
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
|
|
|
|
|
return df.format(new Date());// new Date()为获取当前系统时间
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 按照配置表同步所有的数据
|
|
|
|
|
* 单一数据源,要防止并发
|
|
|
|
|
*/
|
|
|
|
|
public void syncData(String groupName, String id) throws Exception {
|
|
|
|
|
String redisKey = MesConstWords.SAP_TRANS_JOB_LOCK_TAG + ":" + groupName;
|
|
|
|
|
|
|
|
|
|
// 按ID单个调试时 key 要带上 id,防止跟正常 job 冲突
|
|
|
|
|
if (id != null && !id.isEmpty()) {
|
|
|
|
|
redisKey = redisKey + ":" + id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//添加redis锁(半小时失效)
|
|
|
|
|
if (redisMes.getObject(redisKey) != null) {
|
|
|
|
|
LOGGER.error("数据正在处理中!不能并发处理!RedisKey: {}", redisKey);
|
|
|
|
|
// 直接返回
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LOGGER.info("WmsSAPDbAdapter.syncData start...");
|
|
|
|
|
|
|
|
|
|
// 存放错误信息
|
|
|
|
|
StringBuilder errorMessage = new StringBuilder();
|
|
|
|
|
try {
|
|
|
|
|
redisMes.putObject(redisKey, "starting...", redisTime);
|
|
|
|
|
|
|
|
|
|
// 一次性拿到数据源连接,处理完成后再统一关闭
|
|
|
|
|
// 不需要在这里处理
|
|
|
|
|
//initConnection();
|
|
|
|
|
|
|
|
|
|
writer = buildWriter(groupName, sapDataSourceProxy, mesDataSourceProxy);
|
|
|
|
|
writer.setSrcConn(this.srcConn);
|
|
|
|
|
writer.setDestConn(this.destConn);
|
|
|
|
|
|
|
|
|
|
reader = buildReader(groupName, sapDataSourceProxy, mesDataSourceProxy);
|
|
|
|
|
reader.setSrcConn(this.srcConn);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 映射关系始终从 WMS 表获取
|
|
|
|
|
dbDataMapper = new MesSAPDbDataMapper(mesDataSourceProxy);
|
|
|
|
|
dbDataMapper.setSrcConn(mesConn);
|
|
|
|
|
|
|
|
|
|
translator = new WmsSAPDbTranslator(wmsSAPDbExpression);
|
|
|
|
|
|
|
|
|
|
List<MesInterfaceDataMapper> dataMappers = dbDataMapper.readDataMappersBySQL(groupName, id, null, null, null);
|
|
|
|
|
|
|
|
|
|
LOGGER.info("WmsSAPDbAdapter.foreach dataMapper ...");
|
|
|
|
|
|
|
|
|
|
// 遍历所有已配置的接口表
|
|
|
|
|
for (MesInterfaceDataMapper dataMapper : dataMappers) {
|
|
|
|
|
try {
|
|
|
|
|
// 检查同步周期是否达到预设值
|
|
|
|
|
if (!checkSyncTime(dataMapper)) {
|
|
|
|
|
// 未到达同步时间,继续判断下一个接口
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
LOGGER.error("同步数据发生异常", e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// 设置本此发送的 SID
|
|
|
|
|
dataMapper.setSid(snowflakeIdMaker.nextId());
|
|
|
|
|
// 用当前配置信息初始化对象
|
|
|
|
|
reader.setDataMapper(dataMapper);
|
|
|
|
|
writer.setDataMapper(dataMapper);
|
|
|
|
|
translator.setDataMapper(dataMapper);
|
|
|
|
|
|
|
|
|
|
// 从来源表读取数据
|
|
|
|
|
LOGGER.info("WmsSAPDbAdapter.syncData softAdapter:{} read data", dataMapper.getSoftAdaptorCode());
|
|
|
|
|
|
|
|
|
|
List<Map<String, Object>> srcData = reader.readData();
|
|
|
|
|
// 来源表中没有数据,跳过当前循环,处理下一个表
|
|
|
|
|
if (srcData == null || srcData.size() == 0) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
LOGGER.info("WmsSAPDbAdapter.syncData softAdapter:{},size:{}", dataMapper.getSoftAdaptorCode(), srcData.size());
|
|
|
|
|
// 把来源表转换成目标表
|
|
|
|
|
LOGGER.info("WmsSAPDbAdapter.syncData softAdapter:{} start translateData", dataMapper.getSoftAdaptorCode());
|
|
|
|
|
|
|
|
|
|
DataSet dataSet = translator.translateData(srcData);
|
|
|
|
|
LOGGER.info("WmsSAPDbAdapter.syncData softAdapter:{} end translateData", dataMapper.getSoftAdaptorCode());
|
|
|
|
|
|
|
|
|
|
// 没有获取到转换后的数据,不执行写入操作,处理下一个表
|
|
|
|
|
if (dataSet == null || dataSet.size() == 0) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
LOGGER.info("WmsSAPDbAdapter.syncData.saveData softAdapter:{} start", dataMapper.getSoftAdaptorCode());
|
|
|
|
|
|
|
|
|
|
// 写入目标表信息并更新来源表标志字段
|
|
|
|
|
writer.saveData(srcData, dataSet, reader.getOriginReadData());
|
|
|
|
|
LOGGER.info("WmsSAPDbAdapter.syncData.saveData softAdapter:{} end", dataMapper.getSoftAdaptorCode());
|
|
|
|
|
String updateSql = String.format("update mes_interface_data_mapper set" +
|
|
|
|
|
" last_sync_time ='%s' where id =%s", TimeTool.getNowTime(true), dataMapper.getId());
|
|
|
|
|
mesDataSourceProxy.execute(mesConn, updateSql);
|
|
|
|
|
mesConn.setAutoCommit(false);
|
|
|
|
|
// 把当前时间写入 redis,便于跟踪
|
|
|
|
|
redisMes.putObject(redisKey, getTime(), 1800);
|
|
|
|
|
|
|
|
|
|
dbDataMapper.updateWmsInterfaceDataMapperLastSyncTimeById(dataMapper.getId());
|
|
|
|
|
// 更新下次执行时间
|
|
|
|
|
dbDataMapper.updateNextSyncTime(dataMapper);
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
LOGGER.error("接口同步错误! 接口对象:" + JSON.toJSONString(dataMapper) + "\n" + e.getMessage());
|
|
|
|
|
|
|
|
|
|
errorMessage.append("接口同步错误! 接口对象:" + JSON.toJSONString(dataMapper) + "\n");
|
|
|
|
|
errorMessage.append(e.getMessage() + ", " + e.getCause());
|
|
|
|
|
errorMessage.append("\n\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
LOGGER.error("WmsSAPDbAdapter: " + e.getStackTrace().toString());
|
|
|
|
|
throw e;
|
|
|
|
|
} finally {
|
|
|
|
|
// 解除REDIS执行锁,这个要放到第一行,否则执行有异常时可能导致 KEY 删不掉
|
|
|
|
|
redisMes.deleteKey(redisKey);
|
|
|
|
|
// 手工关闭连接
|
|
|
|
|
//closeConnection();
|
|
|
|
|
mesDataSourceProxy.closeConnectionWithoutPoll(this.srcConn);
|
|
|
|
|
mesDataSourceProxy.closeConnectionWithoutPoll(this.destConn);
|
|
|
|
|
mesDataSourceProxy.closeConnectionWithoutPoll(this.mesConn);
|
|
|
|
|
}
|
|
|
|
|
LOGGER.info("WmsSAPDbAdapter.syncData stop...");
|
|
|
|
|
|
|
|
|
|
// 执行过程中出现了异常,抛出异常
|
|
|
|
|
if (errorMessage.length() > 0) {
|
|
|
|
|
throw new RuntimeException(errorMessage.toString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 检查距离上次同步的间隔时间是否满足预设的同步间隔时间
|
|
|
|
|
*
|
|
|
|
|
* @param dataMapper 接口实体
|
|
|
|
|
* @return 是否满足同步条件
|
|
|
|
|
*/
|
|
|
|
|
private boolean checkSyncTime(MesInterfaceDataMapper dataMapper) {
|
|
|
|
|
try {
|
|
|
|
|
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
int syncFrequency = dataMapper.getSyncFrequency();//间隔分钟数
|
|
|
|
|
String lastSyncTimes = dataMapper.getLastSyncTime();//时分秒的三种数据
|
|
|
|
|
LocalDateTime localDateTimeSatrt = LocalDateTime.parse(lastSyncTimes, df);
|
|
|
|
|
LocalDateTime localDateTimeEnd = LocalDateTime.now();
|
|
|
|
|
Duration duration = Duration.between(localDateTimeSatrt, localDateTimeEnd);
|
|
|
|
|
long minutes = duration.toMinutes();
|
|
|
|
|
// 间隔时间未到达预设的同步周期
|
|
|
|
|
if (minutes < syncFrequency) {
|
|
|
|
|
String msg = String.format("上次同步频率时间相差太短!syncFrequency=%s " +
|
|
|
|
|
"--- localDateTimeSatrt=%s " +
|
|
|
|
|
"--- localDateTimeEnd=%s",
|
|
|
|
|
syncFrequency, localDateTimeSatrt, localDateTimeEnd);
|
|
|
|
|
LOGGER.info(msg);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 按照配置表+工厂同步所有的数据
|
|
|
|
|
* 单一数据源,要防止并发
|
|
|
|
|
*/
|
|
|
|
|
public void syncDataByOrganizeCode(String groupName, String organizeCode, List<String> destBeanNameList, List<String> destBeanNameNotList) throws Exception {
|
|
|
|
|
|
|
|
|
|
//分组代码+工厂代码作业KEY值
|
|
|
|
|
String redisKey = MesConstWords.SAP_TRANS_JOB_LOCK_TAG + ":" + groupName + ":" + organizeCode + destBeanNameList + destBeanNameNotList;
|
|
|
|
|
|
|
|
|
|
//添加redis锁(半小时失效)
|
|
|
|
|
if (redisMes.getObject(redisKey) != null) {
|
|
|
|
|
LOGGER.error("数据正在处理中!不能并发处理!RedisKey: {}", redisKey);
|
|
|
|
|
// 直接返回
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LOGGER.info("WmsSAPDbAdapter.syncData organizeCode:{},start...", organizeCode);
|
|
|
|
|
|
|
|
|
|
// 存放错误信息
|
|
|
|
|
StringBuilder errorMessage = new StringBuilder();
|
|
|
|
|
try {
|
|
|
|
|
redisMes.putObject(redisKey, "starting...", redisTime);
|
|
|
|
|
|
|
|
|
|
// 一次性拿到数据源连接,处理完成后再统一关闭
|
|
|
|
|
// 不需要在这里处理
|
|
|
|
|
//initConnection();
|
|
|
|
|
|
|
|
|
|
writer = buildWriter(groupName, sapDataSourceProxy, mesDataSourceProxy);
|
|
|
|
|
writer.setSrcConn(this.srcConn);
|
|
|
|
|
writer.setDestConn(this.destConn);
|
|
|
|
|
|
|
|
|
|
reader = buildReader(groupName, sapDataSourceProxy, mesDataSourceProxy);
|
|
|
|
|
reader.setSrcConn(this.srcConn);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 映射关系始终从 WMS 表获取
|
|
|
|
|
dbDataMapper = new MesSAPDbDataMapper(mesDataSourceProxy);
|
|
|
|
|
dbDataMapper.setSrcConn(mesConn);
|
|
|
|
|
|
|
|
|
|
translator = new WmsSAPDbTranslator(wmsSAPDbExpression);
|
|
|
|
|
|
|
|
|
|
List<MesInterfaceDataMapper> dataMappers = dbDataMapper.readDataMappersBySQL(groupName, null, organizeCode, destBeanNameList, destBeanNameNotList);
|
|
|
|
|
|
|
|
|
|
LOGGER.info("WmsSAPDbAdapter.foreach dataMapper ...");
|
|
|
|
|
|
|
|
|
|
// 遍历所有已配置的接口表
|
|
|
|
|
for (MesInterfaceDataMapper dataMapper : dataMappers) {
|
|
|
|
|
try {
|
|
|
|
|
// 检查同步周期是否达到预设值
|
|
|
|
|
if (!checkSyncTime(dataMapper)) {
|
|
|
|
|
// 未到达同步时间,继续判断下一个接口
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
LOGGER.error("同步数据发生异常", e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// 设置本此发送的 SID
|
|
|
|
|
dataMapper.setSid(snowflakeIdMaker.nextId());
|
|
|
|
|
// 用当前配置信息初始化对象
|
|
|
|
|
reader.setDataMapper(dataMapper);
|
|
|
|
|
writer.setDataMapper(dataMapper);
|
|
|
|
|
translator.setDataMapper(dataMapper);
|
|
|
|
|
|
|
|
|
|
// 从来源表读取数据
|
|
|
|
|
LOGGER.info("WmsSAPDbAdapter.syncData softAdapter:{} read data", dataMapper.getSoftAdaptorCode());
|
|
|
|
|
|
|
|
|
|
List<Map<String, Object>> srcData = reader.readData();
|
|
|
|
|
// 来源表中没有数据,跳过当前循环,处理下一个表
|
|
|
|
|
if (srcData == null || srcData.size() == 0) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
LOGGER.info("WmsSAPDbAdapter.syncData softAdapter:{},size:{}", dataMapper.getSoftAdaptorCode(), srcData.size());
|
|
|
|
|
// 把来源表转换成目标表
|
|
|
|
|
LOGGER.info("WmsSAPDbAdapter.syncData softAdapter:{} start translateData", dataMapper.getSoftAdaptorCode());
|
|
|
|
|
|
|
|
|
|
DataSet dataSet = translator.translateData(srcData);
|
|
|
|
|
LOGGER.info("WmsSAPDbAdapter.syncData softAdapter:{} end translateData", dataMapper.getSoftAdaptorCode());
|
|
|
|
|
|
|
|
|
|
// 没有获取到转换后的数据,不执行写入操作,处理下一个表
|
|
|
|
|
if (dataSet == null || dataSet.size() == 0) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
LOGGER.info("WmsSAPDbAdapter.syncData.saveData softAdapter:{} start", dataMapper.getSoftAdaptorCode());
|
|
|
|
|
|
|
|
|
|
// 写入目标表信息并更新来源表标志字段
|
|
|
|
|
writer.saveData(srcData, dataSet, reader.getOriginReadData());
|
|
|
|
|
LOGGER.info("WmsSAPDbAdapter.syncData.saveData softAdapter:{} end", dataMapper.getSoftAdaptorCode());
|
|
|
|
|
String updateSql = String.format("update mes_interface_data_mapper set" +
|
|
|
|
|
" last_sync_time ='%s' where id =%s", TimeTool.getNowTime(true), dataMapper.getId());
|
|
|
|
|
mesDataSourceProxy.execute(mesConn, updateSql);
|
|
|
|
|
mesConn.setAutoCommit(false);
|
|
|
|
|
// 把当前时间写入 redis,便于跟踪
|
|
|
|
|
redisMes.putObject(redisKey, getTime(), 1800);
|
|
|
|
|
|
|
|
|
|
dbDataMapper.updateWmsInterfaceDataMapperLastSyncTimeById(dataMapper.getId());
|
|
|
|
|
// 更新下次执行时间
|
|
|
|
|
dbDataMapper.updateNextSyncTime(dataMapper);
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
LOGGER.error("接口同步错误! 接口对象:" + JSON.toJSONString(dataMapper) + "\n" + e.getMessage());
|
|
|
|
|
|
|
|
|
|
errorMessage.append("接口同步错误! 接口对象:" + JSON.toJSONString(dataMapper) + "\n");
|
|
|
|
|
errorMessage.append(e.getMessage() + ", " + e.getCause());
|
|
|
|
|
errorMessage.append("\n\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
LOGGER.error("WmsSAPDbAdapter: " + e.getStackTrace().toString());
|
|
|
|
|
throw e;
|
|
|
|
|
} finally {
|
|
|
|
|
// 解除REDIS执行锁,这个要放到第一行,否则执行有异常时可能导致 KEY 删不掉
|
|
|
|
|
redisMes.deleteKey(redisKey);
|
|
|
|
|
// 手工关闭连接
|
|
|
|
|
//closeConnection();
|
|
|
|
|
mesDataSourceProxy.closeConnectionWithoutPoll(this.srcConn);
|
|
|
|
|
mesDataSourceProxy.closeConnectionWithoutPoll(this.destConn);
|
|
|
|
|
mesDataSourceProxy.closeConnectionWithoutPoll(this.mesConn);
|
|
|
|
|
}
|
|
|
|
|
LOGGER.info("WmsSAPDbAdapter.syncData organizeCode:{} stop...", organizeCode);
|
|
|
|
|
|
|
|
|
|
// 执行过程中出现了异常,抛出异常
|
|
|
|
|
if (errorMessage.length() > 0) {
|
|
|
|
|
throw new RuntimeException(errorMessage.toString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Rock.Yu 2019-07-26
|
|
|
|
|
* 读写用2个数据源有问题,写进去的数据从读数据源查不到(可能是事务问题,没有深入分析)
|
|
|
|
|
* 实际上配置文件中的读写连接串都是相同的,没必要用2个连接(做读写分离可以用 MyCAT)
|
|
|
|
|
* 此处读写都是用的同一个连接(写连接),可以解决上述问题
|
|
|
|
|
*
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
// private void initConnection() throws Exception {
|
|
|
|
|
// sapDataSourceProxy.setReadConnHold(sapDataSourceProxy.getWriteConnectionWithoutPool());
|
|
|
|
|
// sapDataSourceProxy.setWriteConnHold(sapDataSourceProxy.getWriteConnectionWithoutPool());
|
|
|
|
|
// mesDataSourceProxy.setReadConnHold(mesDataSourceProxy.getWriteConnectionWithoutPool());
|
|
|
|
|
// mesDataSourceProxy.setWriteConnHold(mesDataSourceProxy.getWriteConnectionWithoutPool());
|
|
|
|
|
// mesDataSourceProxy.setReadConnHold(mesDataSourceProxy.getWriteConnectionWithoutPool());
|
|
|
|
|
// mesDataSourceProxy.setWriteConnHold(mesDataSourceProxy.getWriteConnectionWithoutPool());
|
|
|
|
|
// swebDataSourceProxy.setReadConnHold(swebDataSourceProxy.getWriteConnectionWithoutPool());
|
|
|
|
|
// swebDataSourceProxy.setWriteConnHold(swebDataSourceProxy.getWriteConnectionWithoutPool());
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// private void closeConnection() throws Exception {
|
|
|
|
|
// try {
|
|
|
|
|
// sapDataSourceProxy.closeConnectionWithoutPoll(sapDataSourceProxy.getReadConnHold());
|
|
|
|
|
// sapDataSourceProxy.closeConnectionWithoutPoll(sapDataSourceProxy.getWriteConnHold());
|
|
|
|
|
// mesDataSourceProxy.closeConnectionWithoutPoll(mesDataSourceProxy.getReadConnHold());
|
|
|
|
|
// mesDataSourceProxy.closeConnectionWithoutPoll(mesDataSourceProxy.getWriteConnHold());
|
|
|
|
|
// mesDataSourceProxy.closeConnectionWithoutPoll(mesDataSourceProxy.getReadConnHold());
|
|
|
|
|
// mesDataSourceProxy.closeConnectionWithoutPoll(mesDataSourceProxy.getWriteConnHold());
|
|
|
|
|
// swebDataSourceProxy.closeConnectionWithoutPoll(swebDataSourceProxy.getReadConnHold());
|
|
|
|
|
// swebDataSourceProxy.closeConnectionWithoutPoll(swebDataSourceProxy.getWriteConnHold());
|
|
|
|
|
// } catch (Exception e) {
|
|
|
|
|
// LOGGER.error(e.getStackTrace().toString());
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
}
|