feat(Property): 优化定时任务执行逻辑并添加时间限制
This commit is contained in:
@@ -266,17 +266,17 @@ public class TbMeterInfoServiceImpl implements ITbMeterInfoService {
|
||||
*/
|
||||
@Override
|
||||
public void getMeterStatus(Long meterType, Long floorId) {
|
||||
// 参数校验
|
||||
if (meterType == 0L || floorId == 0L) {
|
||||
heartbeatTasks.stopTask("Meter_Status_Reading");
|
||||
return;
|
||||
}
|
||||
|
||||
// 获取当前登录用户
|
||||
LoginUser user = LoginHelper.getLoginUser();
|
||||
String tokenValue = StpUtil.getTokenValue();
|
||||
if (user == null) {
|
||||
heartbeatTasks.stopTask("Meter_Status_Reading");
|
||||
heartbeatTasks.stopTask(tokenValue);
|
||||
return;
|
||||
}
|
||||
|
||||
// 参数校验
|
||||
if (meterType == 0L || floorId == 0L) {
|
||||
heartbeatTasks.stopTask(tokenValue);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -292,7 +292,7 @@ public class TbMeterInfoServiceImpl implements ITbMeterInfoService {
|
||||
|
||||
// 如果没有仪表信息,直接返回
|
||||
if (meterInfoVoList.isEmpty()) {
|
||||
heartbeatTasks.stopTask("Meter_Status_Reading");
|
||||
heartbeatTasks.stopTask(tokenValue);
|
||||
jsonObject.put("readingTime", DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
|
||||
jsonObject.put("data", new ArrayList<>());
|
||||
remoteMessageService.sendMessage(user.getUserId(), tokenValue, jsonObject.toString());
|
||||
@@ -309,7 +309,7 @@ public class TbMeterInfoServiceImpl implements ITbMeterInfoService {
|
||||
}
|
||||
|
||||
// 启动定时任务
|
||||
heartbeatTasks.startHeartbeatTask("Meter_Status_Reading", () -> {
|
||||
heartbeatTasks.startHeartbeatTask(tokenValue, () -> {
|
||||
jsonObject.put("readingTime", DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
|
||||
|
||||
List<MeterResult> meterResults;
|
||||
@@ -325,7 +325,7 @@ public class TbMeterInfoServiceImpl implements ITbMeterInfoService {
|
||||
|
||||
// 处理仪表读数和状态
|
||||
processMeterResults(user, jsonObject, meterResults, meterInfoVoList, tokenValue);
|
||||
}, 30000);
|
||||
}, 30000,300000);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -212,12 +212,8 @@ public class TbMeterRecordServiceImpl implements ITbMeterRecordService {
|
||||
// 设置上次读数
|
||||
if (hasOldRecords) {
|
||||
TbMeterRecord oldRecord = oldRecordMap.get(info.getId());
|
||||
if (oldRecord != null) {
|
||||
record.setPreviousReading(oldRecord.getCurrentReading());
|
||||
} else {
|
||||
// 如果没有找到对应的旧记录,使用默认值
|
||||
record.setPreviousReading(record.getCurrentReading());
|
||||
}
|
||||
// 如果没有找到对应的旧记录,使用默认值
|
||||
record.setPreviousReading(Objects.requireNonNullElse(oldRecord, record).getCurrentReading());
|
||||
} else {
|
||||
record.setPreviousReading(record.getCurrentReading());
|
||||
}
|
||||
@@ -308,7 +304,6 @@ public class TbMeterRecordServiceImpl implements ITbMeterRecordService {
|
||||
private Map<String, Object> trendMonthData(String floorId, String meterId, Long meterType, String year) {
|
||||
Map<String, Object> resultMap = new HashMap<>();
|
||||
List<Map<String, Object>> monthList = baseMapper.getMonthTrend(StrUtil.isBlank(floorId) ? null : Long.parseLong(floorId), StrUtil.isBlank(meterId) ? null : Long.parseLong(meterId), meterType, year);
|
||||
log.info("year:{},monthList:{}", year, monthList);
|
||||
List<String[]> monthData = new ArrayList<>();
|
||||
monthList.forEach(item -> monthData.add(new String[]{item.get("month").toString(), item.get("total_consumption").toString()}));
|
||||
Float total = monthList.stream().map(map -> new BigDecimal(map.get("total_consumption").toString())).reduce(BigDecimal::add).orElse(BigDecimal.ZERO).floatValue();
|
||||
|
@@ -15,14 +15,36 @@ public class HeartbeatTasks {
|
||||
private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
|
||||
private final Map<String, ScheduledFuture<?>> tasks = new ConcurrentHashMap<>();
|
||||
|
||||
public void startHeartbeatTask(String taskId, Runnable task, long intervalMs) {
|
||||
public void startHeartbeatTask(String taskId, Runnable task, long intervalMs, long durationMs) {
|
||||
// 先停止同名任务(如果存在)
|
||||
stopTask(taskId);
|
||||
|
||||
// 创建新任务
|
||||
ScheduledFuture<?> future = scheduler.scheduleAtFixedRate(
|
||||
task, 0, intervalMs, TimeUnit.MILLISECONDS);
|
||||
// 如果 durationMs 为 0,表示任务无需时间限制
|
||||
if (durationMs <= 0) {
|
||||
ScheduledFuture<?> future = scheduler.scheduleAtFixedRate(
|
||||
task,
|
||||
0,
|
||||
intervalMs,
|
||||
TimeUnit.MILLISECONDS
|
||||
);
|
||||
tasks.put(taskId, future);
|
||||
return;
|
||||
}
|
||||
|
||||
// 创建一个包装任务,带时长检查
|
||||
long startTime = System.currentTimeMillis();
|
||||
ScheduledFuture<?> future = scheduler.scheduleAtFixedRate(
|
||||
() -> {
|
||||
long elapsedTime = System.currentTimeMillis() - startTime;
|
||||
if (elapsedTime >= durationMs) {
|
||||
stopTask(taskId);
|
||||
}
|
||||
task.run();
|
||||
},
|
||||
0,
|
||||
intervalMs,
|
||||
TimeUnit.MILLISECONDS
|
||||
);
|
||||
tasks.put(taskId, future);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user