Authored by Lixiaodi

加分布式锁

... ... @@ -253,6 +253,8 @@ public class UnionServiceImpl implements IUnionService,ApplicationEventPublisher
@Override
public UnionResponse activateUnion(ActivateUnionRequestBO request) throws ServiceException {
String ut = null;
String k = null;
try {
activeUnion.info("activateUnion with request is {}", request);
// 检查输入参数
... ... @@ -398,11 +400,19 @@ public class UnionServiceImpl implements IUnionService,ApplicationEventPublisher
log.warn("activateUnion error user not click info. with param is {}", request);
return new UnionResponse(200, "user not click",new JSONObject());
}
noCheckActive.info("Active record without any check. client is {}, udid is {}, idfa is {}, imei is {}", request.getClient_type(), request.getUdid(), request.getIdfa(), request.getImei());
// 把存储的字符串变为对象
ClickUnionRequestBO click = JSON.parseObject(value, ClickUnionRequestBO.class);
ut = click.getUnion_type();
k = key;
if (!getDistributeLock(ut, k)) {
log.info("getDistributeLock failed key is {}", key);
return new UnionResponse(200, "getDistributeLock failed", new JSONObject());
}
//判断是否需要判断激活,90天活跃
boolean isNeedCheckActive = true;
if(NO_NEED_CHECK_ACTIVE_UNION.equals(click.getUnion_type()) || "4".equals(click.getUnion_type())){
... ... @@ -671,6 +681,8 @@ public class UnionServiceImpl implements IUnionService,ApplicationEventPublisher
} catch (Exception e) {
log.error("activateUnion error with request={}", request, e);
return new UnionResponse(200, e.getMessage(),new JSONObject());
} finally {
deleteDistributeLock(ut, k);
}
}
... ... @@ -1450,4 +1462,37 @@ public class UnionServiceImpl implements IUnionService,ApplicationEventPublisher
pageUnionActivityLogsRspBO.setList(unionActivityLogsRspBOList);
return pageUnionActivityLogsRspBO;
}
private boolean getDistributeLock(String unionType, String deviceType) {
String key = UNION_KEY + "NX:" + unionType + ":" + deviceType;
try {
yhValueOperations.setIfAbsent(key, "lock");
redisTemplate.longExpire(key, 30, TimeUnit.SECONDS);
return true;
} catch (Exception e) {
log.error("getDistributeLock: {} error, err is {}", key, e.getMessage());
try {
redisTemplate.delete(key);
} catch (Exception e1) {
log.error(key, e1);
}
return false;
}
}
private boolean deleteDistributeLock(String unionType, String deviceType) {
String key = UNION_KEY + "NX:" + unionType + ":" + deviceType;
try {
redisTemplate.delete(key);
return true;
} catch (Exception e) {
log.error("deleteDistributeLock: {} error, err is {}", key, e.getMessage());
try {
redisTemplate.delete(key);
} catch (Exception e1) {
log.error(key, e1);
}
return false;
}
}
}
... ...