切面中使用,Redisson框架的tryLock方法做分布式锁,来做防止重复提交,但是锁还未释放tryLock的返回值中就会出现多个true的返回结果,但是采用 lock() 方法就不会出现这样的问题?这是怎么回事?
架包版本
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-data-23</artifactId>
<version>3.15.4</version>
</dependency>
代码
@Slf4j
@Order(2)
@Aspect
@Component
public class UserDeDupAspect {
@Autowired
private RedissonClient redissonClient;
@Pointcut("@annotation(com.ruoyi.hall.annotation.UserDeDup)")
public void tenantUserDeDupAspect() {
}
@Before("tenantUserDeDupAspect()")
private Object runControllerMethod(JoinPoint joinPoint) {
Object rtnObject = null;
try {
MemberInfo memberInfo = UserAspect.MEMBER_INFO.get();
long userId;
if (memberInfo != null) {
userId = memberInfo.getMemberId();
} else {
userId = SecurityUtils.getLoginMember().getMemberId();
}
String KEY = "dedup:a=" + userId;
RLock rLock = redissonClient.getLock(KEY);
//当并发请求到达时,b结果中会返回多个true的值
boolean b = rLock.tryLock(100, 550000, TimeUnit.MILLISECONDS);
FileOutputStream f=new FileOutputStream("D:\\mao\\dfd.txt",true);
f.write(String.valueOf(b).getBytes());
f.write("\r\n".getBytes());
f.flush();
if (!b) {
throw new ServiceException("");
}
} catch (ServiceException e) {
throw new ServiceException("请勿重复提交", 700);
} catch (Throwable e) {
log.error("Exception,exception:{}", e, e);
throw new ServiceException("请勿重复提交", 700);
}
return rtnObject;
}
}
我想要达到的结果
有啥好的办法或者思路吗?