Commit a2d01ed5 authored by tianyiming's avatar tianyiming

refactor: 增加批量处理逻辑以优化设备状态更新和消息发送

parent 5983dd6b
...@@ -143,19 +143,23 @@ public class SafetyProblemTracingGenServiceImpl{ ...@@ -143,19 +143,23 @@ public class SafetyProblemTracingGenServiceImpl{
return; return;
} }
List<String> records = mapList.stream().map(m -> m.get("RECORD").toString()).collect(Collectors.toList()); List<String> records = mapList.stream().map(m -> m.get("RECORD").toString()).collect(Collectors.toList());
int batchSize = 10000;
for (int i = 0; i < records.size(); i += batchSize) {
List<String> batch = records.subList(i, Math.min(i + batchSize, records.size()));
// 更新设备状态为未处理(异常) // 更新设备状态为未处理(异常)
idxBizJgOtherInfoService.lambdaUpdate() idxBizJgOtherInfoService.lambdaUpdate()
.in(IdxBizJgOtherInfo::getRecord, records) .in(IdxBizJgOtherInfo::getRecord, batch)
.set(IdxBizJgOtherInfo::getStatus, SafetyProblemStatusEnum.UNHANDLED.getCode()) .set(IdxBizJgOtherInfo::getStatus, SafetyProblemStatusEnum.UNHANDLED.getCode())
.update(); .update();
List<ESEquipmentCategoryDto> esEquipmentCategoryDto = Lists.newArrayList(); List<ESEquipmentCategoryDto> esEquipmentCategoryDto = Lists.newArrayList();
for (ESEquipmentCategoryDto equipmentCategoryDto : esEquipmentCategory.findAllById(records)) { for (ESEquipmentCategoryDto equipmentCategoryDto : esEquipmentCategory.findAllById(batch)) {
equipmentCategoryDto.setProblemStatus(SafetyProblemStatusEnum.UNHANDLED.getCode()); equipmentCategoryDto.setProblemStatus(SafetyProblemStatusEnum.UNHANDLED.getCode());
esEquipmentCategoryDto.add(equipmentCategoryDto); esEquipmentCategoryDto.add(equipmentCategoryDto);
} }
esEquipmentCategory.saveAll(esEquipmentCategoryDto); esEquipmentCategory.saveAll(esEquipmentCategoryDto);
sendSafetyProblemMessage(mapList, safetyProblemTypeEnum); sendSafetyProblemMessage(mapList, safetyProblemTypeEnum);
} }
}
private void updateEnterpriseAndSendMessage(List<Map<String, Object>> mapList) { private void updateEnterpriseAndSendMessage(List<Map<String, Object>> mapList) {
if (ValidationUtil.isEmpty(mapList)) { if (ValidationUtil.isEmpty(mapList)) {
...@@ -191,13 +195,10 @@ public class SafetyProblemTracingGenServiceImpl{ ...@@ -191,13 +195,10 @@ public class SafetyProblemTracingGenServiceImpl{
* @param safetyProblemTypeEnum * @param safetyProblemTypeEnum
*/ */
private void sendSafetyProblemMessage(List<Map<String, Object>> mapList, SafetyProblemTypeEnum safetyProblemTypeEnum) { private void sendSafetyProblemMessage(List<Map<String, Object>> mapList, SafetyProblemTypeEnum safetyProblemTypeEnum) {
int batchSize = 10000;
for (int i = 0; i < mapList.size(); i += batchSize) {
List<Map<String, Object>> batch = mapList.subList(i, Math.min(i + batchSize, mapList.size()));
if (CollectionUtil.isNotEmpty(mapList)){ if (CollectionUtil.isNotEmpty(mapList)){
try { try {
logger.info("发送安全追溯问题主题---->{}", safetyProblemTypeEnum.getTopic()); logger.info("发送安全追溯问题主题---->{}", safetyProblemTypeEnum.getTopic());
emqKeeper.getMqttClient().publish(safetyProblemTypeEnum.getTopic(), JSON.toJSONBytes(batch), 2, false); emqKeeper.getMqttClient().publish(safetyProblemTypeEnum.getTopic(), JSON.toJSONBytes(mapList), 2, false);
logger.info("发送安全追溯问题消息成功---->"); logger.info("发送安全追溯问题消息成功---->");
} catch (MqttException e) { } catch (MqttException e) {
logger.error("发送安全追溯问题设备信息消息失败---->{}", e.getMessage()); logger.error("发送安全追溯问题设备信息消息失败---->{}", e.getMessage());
...@@ -206,8 +207,6 @@ public class SafetyProblemTracingGenServiceImpl{ ...@@ -206,8 +207,6 @@ public class SafetyProblemTracingGenServiceImpl{
} }
} }
}
// @Scheduled(cron = "0 0 1 * * ?") // @Scheduled(cron = "0 0 1 * * ?")
// @SchedulerLock(name = "executeOverDesignLifeCheck", lockAtMostFor = "PT5H", lockAtLeastFor = "PT10M") // @SchedulerLock(name = "executeOverDesignLifeCheck", lockAtMostFor = "PT5H", lockAtLeastFor = "PT10M")
public void executeOverDesignLife() { public void executeOverDesignLife() {
......
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