<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;
}
}
有啥好的办法或者思路吗?
你这trylock设置了一个100毫秒的等待,如果不需要等待锁的释放,可以设置为0,一旦获取不到锁直接结束。
而且感觉你这个文件io应该是在获取锁的情况下执行,你这样写不论是否获取都执行是否没什么意义