|
@@ -16,12 +16,15 @@ import com.qhtx.eta.domain.service.DWIndexFrameService;
|
|
import com.qhtx.eta.domain.service.ETAPushDataService;
|
|
import com.qhtx.eta.domain.service.ETAPushDataService;
|
|
import com.qhtx.eta.infra.entity.dw.TEtaIndexPushTask;
|
|
import com.qhtx.eta.infra.entity.dw.TEtaIndexPushTask;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.redisson.api.RLock;
|
|
|
|
+import org.redisson.api.RedissonClient;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
import javax.annotation.Resource;
|
|
import java.text.SimpleDateFormat;
|
|
import java.text.SimpleDateFormat;
|
|
import java.util.*;
|
|
import java.util.*;
|
|
import java.util.concurrent.CompletableFuture;
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@@ -41,62 +44,165 @@ public class ETAFacadeServiceImpl implements ETAFacadeService {
|
|
@Resource
|
|
@Resource
|
|
private ApiServiceHolder apiServiceHolder;
|
|
private ApiServiceHolder apiServiceHolder;
|
|
|
|
|
|
|
|
+ @Resource
|
|
|
|
+ private RedissonClient redissonClient;
|
|
|
|
+
|
|
@Override
|
|
@Override
|
|
@DomainTransDataSource(dataSourceType = DataSourceType.DW)
|
|
@DomainTransDataSource(dataSourceType = DataSourceType.DW)
|
|
- public void pushIndex(String indexCode) {
|
|
|
|
- DWIndexDTO dwIndexDTO = dwIndexFrameService.addSyncIndex(indexCode);
|
|
|
|
|
|
+ public void pushIndex(String indexCode, boolean init) {
|
|
|
|
+ DWIndexDTO dwIndexDTO;
|
|
|
|
+ RLock lock = redissonClient.getLock(indexCode);
|
|
|
|
+ if (init) {
|
|
|
|
+ dwIndexDTO = dwIndexFrameService.addSyncIndex(indexCode);
|
|
|
|
|
|
- CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
|
|
|
|
- List<DWIndexDataDTO> dataList = dwIndexFrameService.getIndexData(indexCode);
|
|
|
|
- dataList.sort(Comparator.comparingLong(o -> o.getDate().getTime()));
|
|
|
|
-// dataList.stream().sorted(Comparator.comparing(DWIndexDataDTO::getDate)).collect(Collectors.toList());
|
|
|
|
- dwIndexDTO.setDataList(dataList);
|
|
|
|
- //api数据发送
|
|
|
|
- if (dwIndexDTO.getDataList().size() > ETAConstants.ETA_PUSH_DATA_LIMIT) {
|
|
|
|
- log.info("数据量超过限制,分批推送");
|
|
|
|
- Lists.partition(dataList, ETAConstants.ETA_PUSH_DATA_CHUNK).forEach(list -> {
|
|
|
|
- Date latestDataDate = list.stream().map(DWIndexDataDTO::getDate).max(Comparator.comparing(Date::getTime)).get();
|
|
|
|
- //防止数据漏更新,先推送,再更新本地任务记录,下游做好冗余和幂等
|
|
|
|
- DWIndexDTO dataDTO = new DWIndexDTO();
|
|
|
|
- dataDTO.setIndexCode(dwIndexDTO.getIndexCode());
|
|
|
|
- dataDTO.setFrequency(dwIndexDTO.getFrequency());
|
|
|
|
- dataDTO.setIndexName(dwIndexDTO.getIndexName());
|
|
|
|
- dataDTO.setRemark(dwIndexDTO.getRemark());
|
|
|
|
- dataDTO.setSourceName(dwIndexDTO.getSourceName());
|
|
|
|
- dataDTO.setUnit(dwIndexDTO.getUnit());
|
|
|
|
- dataDTO.setDataList(list);
|
|
|
|
- List<?> result = apiServiceHolder.runApi(ETAInterfaceEnum.PUSH_ETA_INDEX, dataDTO);
|
|
|
|
- if (result == null) {
|
|
|
|
- log.error("调用ETA接口失败,停止更新");
|
|
|
|
- throw new ETAException(ErrorEnum.PUSH_TO_ETA_ERROR);
|
|
|
|
- }
|
|
|
|
- try {
|
|
|
|
- Thread.sleep(500);
|
|
|
|
- } catch (InterruptedException e) {
|
|
|
|
- log.warn("线程中断异常");
|
|
|
|
|
|
+ CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
|
|
|
|
+ if (lock.tryLock()) {
|
|
|
|
+ List<DWIndexDataDTO> dataList = dwIndexFrameService.getIndexData(indexCode);
|
|
|
|
+ dataList.sort(Comparator.comparingLong(o -> o.getDate().getTime()));
|
|
|
|
+ dwIndexDTO.setDataList(dataList);
|
|
|
|
+ //api数据发送
|
|
|
|
+ if (dwIndexDTO.getDataList().size() > ETAConstants.ETA_PUSH_DATA_LIMIT) {
|
|
|
|
+ log.info("数据量超过限制,分批推送");
|
|
|
|
+ Lists.partition(dataList, ETAConstants.ETA_PUSH_DATA_CHUNK).forEach(list -> {
|
|
|
|
+ Date latestDataDate = list.stream().map(DWIndexDataDTO::getDate).max(Comparator.comparing(Date::getTime)).get();
|
|
|
|
+ //防止数据漏更新,先推送,再更新本地任务记录,下游做好冗余和幂等
|
|
|
|
+ DWIndexDTO dataDTO = new DWIndexDTO();
|
|
|
|
+ dataDTO.setIndexCode(dwIndexDTO.getIndexCode());
|
|
|
|
+ dataDTO.setFrequency(dwIndexDTO.getFrequency());
|
|
|
|
+ dataDTO.setIndexName(dwIndexDTO.getIndexName());
|
|
|
|
+ dataDTO.setRemark(dwIndexDTO.getRemark());
|
|
|
|
+ dataDTO.setSourceName(dwIndexDTO.getSourceName());
|
|
|
|
+ dataDTO.setUnit(dwIndexDTO.getUnit());
|
|
|
|
+ dataDTO.setDataList(list);
|
|
|
|
+ List<?> result = apiServiceHolder.runApi(ETAInterfaceEnum.PUSH_ETA_INDEX, dataDTO);
|
|
|
|
+ if (result == null) {
|
|
|
|
+ log.error("调用ETA接口失败,停止更新");
|
|
|
|
+ throw new ETAException(ErrorEnum.PUSH_TO_ETA_ERROR);
|
|
|
|
+ }
|
|
|
|
+ try {
|
|
|
|
+ Thread.sleep(1000);
|
|
|
|
+ } catch (InterruptedException e) {
|
|
|
|
+ log.warn("线程中断异常");
|
|
|
|
+ }
|
|
|
|
+ TEtaIndexPushTask tEtaIndexPushTask = etaPushDataService.getETAPushTaskByIndexCode(dataDTO.getIndexCode());
|
|
|
|
+ if (tEtaIndexPushTask == null) {
|
|
|
|
+ log.error("未找到对应指标任务记录:{}", dataDTO.getIndexCode());
|
|
|
|
+ throw new ETAException(ErrorEnum.INDEX_UPDATE_ERROR);
|
|
|
|
+ }
|
|
|
|
+ tEtaIndexPushTask.setLatestDataDate(new SimpleDateFormat(ETAConstants.DATE_PATTERN).format(latestDataDate));
|
|
|
|
+ int currentCount = tEtaIndexPushTask.getDataCount() == null ? 0 : tEtaIndexPushTask.getDataCount();
|
|
|
|
+ tEtaIndexPushTask.setDataCount(currentCount + list.size());
|
|
|
|
+ tEtaIndexPushTask.setUpdateTime(new Date());
|
|
|
|
+ tEtaIndexPushTask.setLastPushTime(new Date());
|
|
|
|
+ etaPushDataService.updateTaskByIndexCode(tEtaIndexPushTask);
|
|
|
|
+ });
|
|
|
|
+ } else {
|
|
|
|
+ apiServiceHolder.runApi(ETAInterfaceEnum.PUSH_ETA_INDEX, dwIndexDTO);
|
|
|
|
+ TEtaIndexPushTask tEtaIndexPushTask = etaPushDataService.getETAPushTaskByIndexCode(dwIndexDTO.getIndexCode());
|
|
|
|
+ if (tEtaIndexPushTask == null) {
|
|
|
|
+ log.error("未找到对应指标任务记录:{}", dwIndexDTO.getIndexCode());
|
|
|
|
+ throw new ETAException(ErrorEnum.INDEX_UPDATE_ERROR);
|
|
|
|
+ }
|
|
|
|
+ Date latestDataDate = dwIndexDTO.getDataList().stream().map(DWIndexDataDTO::getDate).max(Comparator.comparing(Date::getTime)).get();
|
|
|
|
+ tEtaIndexPushTask.setLatestDataDate(new SimpleDateFormat(ETAConstants.DATE_PATTERN).format(latestDataDate));
|
|
|
|
+ int currentCount = tEtaIndexPushTask.getDataCount() == null ? 0 : tEtaIndexPushTask.getDataCount();
|
|
|
|
+ tEtaIndexPushTask.setDataCount(currentCount + dwIndexDTO.getDataList().size());
|
|
|
|
+ tEtaIndexPushTask.setUpdateTime(new Date());
|
|
|
|
+ tEtaIndexPushTask.setLastPushTime(new Date());
|
|
|
|
+ etaPushDataService.updateTaskByIndexCode(tEtaIndexPushTask);
|
|
}
|
|
}
|
|
- TEtaIndexPushTask tEtaIndexPushTask = etaPushDataService.getETAPushTaskByIndexCode(dataDTO.getIndexCode());
|
|
|
|
- if (tEtaIndexPushTask == null) {
|
|
|
|
- log.error("未找到对应指标任务记录:{}", dataDTO.getIndexCode());
|
|
|
|
- throw new ETAException(ErrorEnum.INDEX_UPDATE_ERROR);
|
|
|
|
- }
|
|
|
|
- tEtaIndexPushTask.setLatestDataDate(new SimpleDateFormat(ETAConstants.DATE_PATTERN).format(latestDataDate));
|
|
|
|
- int currentCount = tEtaIndexPushTask.getDataCount() == null ? 0 : tEtaIndexPushTask.getDataCount();
|
|
|
|
- tEtaIndexPushTask.setDataCount(currentCount + list.size());
|
|
|
|
- tEtaIndexPushTask.setUpdateTime(new Date());
|
|
|
|
- tEtaIndexPushTask.setLastPushTime(new Date());
|
|
|
|
- etaPushDataService.updateTaskByIndexCode(tEtaIndexPushTask);
|
|
|
|
- });
|
|
|
|
|
|
+ } else {
|
|
|
|
+ log.info("当前任务正在被处理,{}", indexCode);
|
|
|
|
+ }
|
|
|
|
+ }).thenRun(() -> {
|
|
|
|
+ log.info("同步指标任务表完成");
|
|
|
|
+ lock.unlock();
|
|
|
|
+ });
|
|
|
|
+ future.exceptionally(exception -> {
|
|
|
|
+ lock.unlock();
|
|
|
|
+ log.error("同步指标任务表失败:", exception);
|
|
|
|
+ return null;
|
|
|
|
+ });
|
|
|
|
+ } else {
|
|
|
|
+ dwIndexDTO = dwIndexFrameService.getIndex(indexCode);
|
|
|
|
+
|
|
|
|
+ boolean isLock;
|
|
|
|
+ isLock = lock.tryLock();
|
|
|
|
+ if (isLock) {
|
|
|
|
+ try {
|
|
|
|
+ CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
|
|
|
|
+ List<DWIndexDataDTO> dataList = dwIndexFrameService.getIndexData(indexCode);
|
|
|
|
+ dataList.sort(Comparator.comparingLong(o -> o.getDate().getTime()));
|
|
|
|
+ dwIndexDTO.setDataList(dataList);
|
|
|
|
+ //api数据发送
|
|
|
|
+ if (dwIndexDTO.getDataList().size() > ETAConstants.ETA_PUSH_DATA_LIMIT) {
|
|
|
|
+ log.info("数据量超过限制,分批推送");
|
|
|
|
+ Lists.partition(dataList, ETAConstants.ETA_PUSH_DATA_CHUNK).forEach(list -> {
|
|
|
|
+ Date latestDataDate = list.stream().map(DWIndexDataDTO::getDate).max(Comparator.comparing(Date::getTime)).get();
|
|
|
|
+ //防止数据漏更新,先推送,再更新本地任务记录,下游做好冗余和幂等
|
|
|
|
+ DWIndexDTO dataDTO = new DWIndexDTO();
|
|
|
|
+ dataDTO.setIndexCode(dwIndexDTO.getIndexCode());
|
|
|
|
+ dataDTO.setFrequency(dwIndexDTO.getFrequency());
|
|
|
|
+ dataDTO.setIndexName(dwIndexDTO.getIndexName());
|
|
|
|
+ dataDTO.setRemark(dwIndexDTO.getRemark());
|
|
|
|
+ dataDTO.setSourceName(dwIndexDTO.getSourceName());
|
|
|
|
+ dataDTO.setUnit(dwIndexDTO.getUnit());
|
|
|
|
+ dataDTO.setDataList(list);
|
|
|
|
+ List<?> result = apiServiceHolder.runApi(ETAInterfaceEnum.PUSH_ETA_INDEX, dataDTO);
|
|
|
|
+ if (result == null) {
|
|
|
|
+ log.error("调用ETA接口失败,停止更新");
|
|
|
|
+ throw new ETAException(ErrorEnum.PUSH_TO_ETA_ERROR);
|
|
|
|
+ }
|
|
|
|
+ try {
|
|
|
|
+ Thread.sleep(500);
|
|
|
|
+ } catch (InterruptedException e) {
|
|
|
|
+ log.warn("线程中断异常");
|
|
|
|
+ }
|
|
|
|
+ TEtaIndexPushTask tEtaIndexPushTask = etaPushDataService.getETAPushTaskByIndexCode(dataDTO.getIndexCode());
|
|
|
|
+ if (tEtaIndexPushTask == null) {
|
|
|
|
+ log.error("未找到对应指标任务记录:{}", dataDTO.getIndexCode());
|
|
|
|
+ throw new ETAException(ErrorEnum.INDEX_UPDATE_ERROR);
|
|
|
|
+ }
|
|
|
|
+ tEtaIndexPushTask.setLatestDataDate(new SimpleDateFormat(ETAConstants.DATE_PATTERN).format(latestDataDate));
|
|
|
|
+ int currentCount = tEtaIndexPushTask.getDataCount() == null ? 0 : tEtaIndexPushTask.getDataCount();
|
|
|
|
+ tEtaIndexPushTask.setDataCount(currentCount + list.size());
|
|
|
|
+ tEtaIndexPushTask.setUpdateTime(new Date());
|
|
|
|
+ tEtaIndexPushTask.setLastPushTime(new Date());
|
|
|
|
+ etaPushDataService.updateTaskByIndexCode(tEtaIndexPushTask);
|
|
|
|
+ });
|
|
|
|
+ } else {
|
|
|
|
+ apiServiceHolder.runApi(ETAInterfaceEnum.PUSH_ETA_INDEX, dwIndexDTO);
|
|
|
|
+ TEtaIndexPushTask tEtaIndexPushTask = etaPushDataService.getETAPushTaskByIndexCode(dwIndexDTO.getIndexCode());
|
|
|
|
+ if (tEtaIndexPushTask == null) {
|
|
|
|
+ log.error("未找到对应指标任务记录:{}", dwIndexDTO.getIndexCode());
|
|
|
|
+ throw new ETAException(ErrorEnum.INDEX_UPDATE_ERROR);
|
|
|
|
+ }
|
|
|
|
+ Date latestDataDate = dwIndexDTO.getDataList().stream().map(DWIndexDataDTO::getDate).max(Comparator.comparing(Date::getTime)).get();
|
|
|
|
+ tEtaIndexPushTask.setLatestDataDate(new SimpleDateFormat(ETAConstants.DATE_PATTERN).format(latestDataDate));
|
|
|
|
+ int currentCount = tEtaIndexPushTask.getDataCount() == null ? 0 : tEtaIndexPushTask.getDataCount();
|
|
|
|
+ tEtaIndexPushTask.setDataCount(currentCount + dwIndexDTO.getDataList().size());
|
|
|
|
+ tEtaIndexPushTask.setUpdateTime(new Date());
|
|
|
|
+ tEtaIndexPushTask.setLastPushTime(new Date());
|
|
|
|
+ etaPushDataService.updateTaskByIndexCode(tEtaIndexPushTask);
|
|
|
|
+ }
|
|
|
|
+ }).thenRun(() -> {
|
|
|
|
+ log.info("同步指标任务表完成");
|
|
|
|
+ });
|
|
|
|
+ future.exceptionally(exception -> {
|
|
|
|
+ log.error("同步指标任务表失败:", exception);
|
|
|
|
+ return null;
|
|
|
|
+ });
|
|
|
|
+ future.get();
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ throw new ETAException(ErrorEnum.INDEX_DEAL_LOCK_FAIL);
|
|
|
|
+ } finally {
|
|
|
|
+ lock.unlock();
|
|
|
|
+ }
|
|
} else {
|
|
} else {
|
|
- apiServiceHolder.runApi(ETAInterfaceEnum.PUSH_ETA_INDEX, dwIndexDTO);
|
|
|
|
|
|
+ log.error("同步指标任务表失败,当前正在被其他任务处理中请稍后再试");
|
|
}
|
|
}
|
|
- }).thenRun(() -> {
|
|
|
|
- log.info("同步指标任务表完成");
|
|
|
|
- });
|
|
|
|
- future.exceptionally(exception -> {
|
|
|
|
- log.error("同步指标任务表失败:", exception);
|
|
|
|
- return null;
|
|
|
|
- });
|
|
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|