60秒无心跳判定离线

This commit is contained in:
ZZ 2021-09-06 12:17:59 +08:00
parent 2fe8be514a
commit d690d99ab6
3 changed files with 33 additions and 22 deletions

View File

@ -1,11 +1,5 @@
package com.xhpc.common.redis.service; package com.xhpc.common.redis.service;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundSetOperations; import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.HashOperations; import org.springframework.data.redis.core.HashOperations;
@ -13,6 +7,9 @@ import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations; import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.*;
import java.util.concurrent.TimeUnit;
/** /**
* spring redis 工具类 * spring redis 工具类
* *
@ -102,11 +99,21 @@ public class RedisService
* @param collection 多个对象 * @param collection 多个对象
* @return * @return
*/ */
public long deleteObject(final Collection collection) public long deleteObject(final Collection collection) {
{
return redisTemplate.delete(collection); return redisTemplate.delete(collection);
} }
/**
* 删除单个key下的某个值
*
* @param key
*/
public void deleteSetVal(final String key, final Object val) {
redisTemplate.opsForSet().remove(key, val);
}
/** /**
* 缓存List数据 * 缓存List数据
* *
@ -114,8 +121,7 @@ public class RedisService
* @param dataList 待缓存的List数据 * @param dataList 待缓存的List数据
* @return 缓存的对象 * @return 缓存的对象
*/ */
public <T> long setCacheList(final String key, final List<T> dataList) public <T> long setCacheList(final String key, final List<T> dataList) {
{
Long count = redisTemplate.opsForList().rightPushAll(key, dataList); Long count = redisTemplate.opsForList().rightPushAll(key, dataList);
return count == null ? 0 : count; return count == null ? 0 : count;
} }

View File

@ -1,7 +1,6 @@
package com.xhpc.pp.logic; package com.xhpc.pp.logic;
import com.xhpc.common.dto.ConnectorStatusInfo; import com.xhpc.common.dto.ConnectorStatusInfo;
import com.xhpc.pp.config.EarlierBeanConf;
import com.xhpc.pp.tx.ServiceParameter; import com.xhpc.pp.tx.ServiceParameter;
import com.xhpc.pp.tx.ServiceResult; import com.xhpc.pp.tx.ServiceResult;
import com.xhpc.pp.tx.logic.ServiceLogic; import com.xhpc.pp.tx.logic.ServiceLogic;
@ -12,6 +11,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy; import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.Collection;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -39,10 +39,10 @@ public class RegisterLogic implements ServiceLogic {
log.info("pile not in whitelist ({}) ", pileNo); log.info("pile not in whitelist ({}) ", pileNo);
hexCode = ServiceResult.HEX_01; hexCode = ServiceResult.HEX_01;
resultCode = ServiceResult.FAIL; resultCode = ServiceResult.FAIL;
} else if (!EarlierBeanConf.ifcanreg(pileNo)) { // } else if (!EarlierBeanConf.ifcanreg(pileNo)) { // not a stable reliable method
log.info("pile already registered ({}) ", pileNo); // log.info("pile already registered ({}) ", pileNo);
hexCode = ServiceResult.HEX_01; // hexCode = ServiceResult.HEX_01;
resultCode = ServiceResult.FAIL; // resultCode = ServiceResult.FAIL;
} else { } else {
String pkey = "pile:".concat(pileNo); String pkey = "pile:".concat(pileNo);
Map<String, Object> cachePile = REDIS.getCacheMap(pkey); Map<String, Object> cachePile = REDIS.getCacheMap(pkey);
@ -60,7 +60,14 @@ public class RegisterLogic implements ServiceLogic {
cacheGun.put("statusInt", ConnectorStatusInfo.OFF_LINE); cacheGun.put("statusInt", ConnectorStatusInfo.OFF_LINE);
cacheGun.put("svcSrv", localIPAndPort); cacheGun.put("svcSrv", localIPAndPort);
REDIS.setCacheMap(gunkey, cacheGun); REDIS.setCacheMap(gunkey, cacheGun);
cachePileGunSvcSrv(gunkey); String svcSrvKeyNew = "svcSrvGuns:".concat(getLocalIPAndPort());
cachePileGunSvcSrv(gunkey, svcSrvKeyNew);
final Collection<String> cacheSvcSrvKeys = REDIS.keys("svcSrvGuns:*");
for (String svcSrvKey : cacheSvcSrvKeys) {
if (!svcSrvKey.equals(svcSrvKeyNew)) {
REDIS.deleteSetVal(svcSrvKey, gunkey);
}
}
} }
log.info("pile (re)registered ({}) ", pileNo); log.info("pile (re)registered ({}) ", pileNo);
} }
@ -71,12 +78,11 @@ public class RegisterLogic implements ServiceLogic {
return new ServiceResult(HexUtils.toBytes(resultStr), resultCode); return new ServiceResult(HexUtils.toBytes(resultStr), resultCode);
} }
private void cachePileGunSvcSrv(String gunkey) { private void cachePileGunSvcSrv(String gunkey, String svcSrvKey) {
String svcKey = "svcSrvGuns:".concat(getLocalIPAndPort()); Set<String> svcPileGuns = REDIS.getCacheSet(svcSrvKey);
Set<String> svcPileGuns = REDIS.getCacheSet(svcKey);
svcPileGuns.add(gunkey); svcPileGuns.add(gunkey);
REDIS.setCacheSet(svcKey, svcPileGuns); REDIS.setCacheSet(svcSrvKey, svcPileGuns);
} }
} }

View File

@ -18,7 +18,6 @@ public class HBCheckTask {
@Scheduled(fixedRate = 10000) @Scheduled(fixedRate = 10000)
protected void run() { protected void run() {
System.out.println("hbchk..");
String svcSrvKey = "svcSrvGuns".concat(getLocalIPAndPort()); String svcSrvKey = "svcSrvGuns".concat(getLocalIPAndPort());
List<String> cacheGunkeyList = REDIS.getCacheList(svcSrvKey); List<String> cacheGunkeyList = REDIS.getCacheList(svcSrvKey);
Long now = Calendar.getInstance().getTimeInMillis(); Long now = Calendar.getInstance().getTimeInMillis();