Commit 5983dd6b authored by tianbo's avatar tianbo

refactor(jg): 优化安全问题追溯消息发送逻辑

-移除了不必要的导入 - 重构了 sendSafetyProblemMessage 方法,使用批量发送提高效率 - 优化了 JSON 序列化方式,直接使用 toJSONBytes 方法
parent 0ca323f3
...@@ -2,7 +2,6 @@ package com.yeejoin.amos.boot.module.jg.biz.service.impl; ...@@ -2,7 +2,6 @@ package com.yeejoin.amos.boot.module.jg.biz.service.impl;
import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
...@@ -27,7 +26,6 @@ import org.springframework.stereotype.Service; ...@@ -27,7 +26,6 @@ import org.springframework.stereotype.Service;
import org.typroject.tyboot.component.emq.EmqKeeper; import org.typroject.tyboot.component.emq.EmqKeeper;
import org.typroject.tyboot.core.foundation.utils.ValidationUtil; import org.typroject.tyboot.core.foundation.utils.ValidationUtil;
import java.nio.charset.StandardCharsets;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -193,17 +191,21 @@ public class SafetyProblemTracingGenServiceImpl{ ...@@ -193,17 +191,21 @@ 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) {
JSONArray jsonArray = JSON.parseArray(JSON.toJSONString(mapList)); int batchSize = 10000;
if (CollectionUtil.isNotEmpty(jsonArray)){ for (int i = 0; i < mapList.size(); i += batchSize) {
try { List<Map<String, Object>> batch = mapList.subList(i, Math.min(i + batchSize, mapList.size()));
logger.info("发送安全追溯问题主题---->{}", safetyProblemTypeEnum.getTopic()); if (CollectionUtil.isNotEmpty(mapList)){
emqKeeper.getMqttClient().publish(safetyProblemTypeEnum.getTopic(), jsonArray.toString().getBytes(StandardCharsets.UTF_8), 2, false); try {
logger.info("发送安全追溯问题消息成功---->"); logger.info("发送安全追溯问题主题---->{}", safetyProblemTypeEnum.getTopic());
} catch (MqttException e) { emqKeeper.getMqttClient().publish(safetyProblemTypeEnum.getTopic(), JSON.toJSONBytes(batch), 2, false);
logger.error("发送安全追溯问题设备信息消息失败---->{}", e.getMessage()); logger.info("发送安全追溯问题消息成功---->");
throw new RuntimeException(e); } catch (MqttException e) {
logger.error("发送安全追溯问题设备信息消息失败---->{}", e.getMessage());
throw new RuntimeException(e);
}
} }
} }
} }
// @Scheduled(cron = "0 0 1 * * ?") // @Scheduled(cron = "0 0 1 * * ?")
......
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