1.代码优化

This commit is contained in:
xy
2024-10-14 09:39:34 +08:00
parent de0d35f23d
commit 515ae0107c
5 changed files with 145 additions and 57 deletions

View File

@@ -105,17 +105,17 @@ public class RedisKeyExpirationListener extends KeyExpirationEventMessageListene
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
executeMainTask(scheduler,nDid,version);
}
//自动接入
else if (expiredKey.startsWith("autoAccess")) {
List<CsEquipmentDeliveryPO> list = csEquipmentDeliveryService.getAll();
list.forEach(item->{
String version = csTopicService.getVersion(item.getNdid());
if (!Objects.isNull(version)){
csDeviceService.devAccessAskTemplate(item.getNdid(),version,1);
redisUtil.saveByKey(AppRedisKey.DEVICE_MID + item.getNdid(),1);
}
});
}
// //自动接入
// else if (expiredKey.startsWith("autoAccess")) {
// List<CsEquipmentDeliveryPO> list = csEquipmentDeliveryService.getAll();
// list.forEach(item->{
// String version = csTopicService.getVersion(item.getNdid());
// if (!Objects.isNull(version)){
// csDeviceService.devAccessAskTemplate(item.getNdid(),version,1);
// redisUtil.saveByKey(AppRedisKey.DEVICE_MID + item.getNdid(),1);
// }
// });
// }
}
//主任务
@@ -147,15 +147,17 @@ public class RedisKeyExpirationListener extends KeyExpirationEventMessageListene
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
csLogsFeignClient.addUserLog(logDto);
}
//客户端不在线则修改装置状态,进入定时任务
else {
//装置下线
csEquipmentDeliveryService.updateRunStatusBynDid(nDid, AccessEnum.OFFLINE.getCode());
logDto.setOperate("主任务执行失败,装置下线,进入定时任务");
csLogsFeignClient.addUserLog(logDto);
log.info("客户端离线进入定时任务...");
startScheduledTask(scheduler,nDid,version);
logDto.setOperate(nDid + "客户端离线进入定时任务");
}
csLogsFeignClient.addUserLog(logDto);
}
private void startScheduledTask(ScheduledExecutorService scheduler, String nDid, String version) {
@@ -212,7 +214,7 @@ public class RedisKeyExpirationListener extends KeyExpirationEventMessageListene
onlineLogsService.updateById(record);
}
csLogsFeignClient.addUserLog(logDto);
}, 0, 2, TimeUnit.MINUTES);
}, 0, 2, TimeUnit.SECONDS);
}
}

View File

@@ -1,5 +1,10 @@
package com.njcn.access.runner;
import com.njcn.access.service.ICsEquipmentDeliveryService;
import com.njcn.access.service.ICsTopicService;
import com.njcn.access.service.impl.CsDeviceServiceImpl;
import com.njcn.csdevice.pojo.po.CsEquipmentDeliveryPO;
import com.njcn.redis.pojo.enums.AppRedisKey;
import com.njcn.redis.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
@@ -7,6 +12,11 @@ import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
* 类的介绍:用来重新发起设备的接入,存在程序意外停止了,缓存失效导致无法更新装置的状态,所以需要在程序启动时发起设备的接入
@@ -21,10 +31,30 @@ public class AccessApplicationRunner implements ApplicationRunner {
@Resource
private RedisUtil redisUtil;
@Resource
private ICsEquipmentDeliveryService csEquipmentDeliveryService;
@Resource
private ICsTopicService csTopicService;
@Resource
private CsDeviceServiceImpl csDeviceService;
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
private static final long ACCESS_TIME = 60L;
@Override
public void run(ApplicationArguments args) {
redisUtil.saveByKeyWithExpire("autoAccess",null,60L);
//项目启动60s后发起自动接入
Runnable task = () -> {
List<CsEquipmentDeliveryPO> list = csEquipmentDeliveryService.getAll();
list.forEach(item->{
String version = csTopicService.getVersion(item.getNdid());
if (!Objects.isNull(version)){
csDeviceService.devAccessAskTemplate(item.getNdid(),version,1);
redisUtil.saveByKey(AppRedisKey.DEVICE_MID + item.getNdid(),1);
}
});
};
scheduler.schedule(task, ACCESS_TIME, TimeUnit.SECONDS);
// 关闭调度程序
scheduler.shutdown();
}
}