栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java注解如何基于Redission实现分布式锁

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java注解如何基于Redission实现分布式锁

这篇文章主要介绍了Java注解如何基于Redission实现分布式锁,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

1、定义注解类

@Target({ ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@documented
public @interface DistributedLock {
  //锁名称
  String lockName() default "";

  //释放时间
  long releaseTime() default 5*1000;

  //时间单位
  TimeUnit timeUnit() default TimeUnit.MILLISECONDS;
}

2、定义切面拦截 DistributedLock 注解

@Aspect
@Component
@Slf4j
public class DistributedLockAspect {

  @Autowired
  private RedissonClient redissonClient;
   //这里需要修改对应的包名
  @Pointcut("@annotation(com.utils.annotation.DistributedLock)")
  public void RlockAspect() {
  }

  @Around("RlockAspect()")
  public Object arround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
    Object object = null;
    RLock lock = null;

    log.info("rlockAspect start ");

    try {
      DistributedLock rlockInfo = getRlockInfo(proceedingJoinPoint);
      String lockKey = getLocalKey(proceedingJoinPoint, rlockInfo);
      lock = redissonClient.getLock(lockKey);

      if (lock != null) {
 final boolean status = lock.tryLock(rlockInfo.releaseTime(), rlockInfo.timeUnit());
 if (status) {
   object = proceedingJoinPoint.proceed();
 }
      } else {
 log.info("未获取到锁:{}", lockKey);
      }
    } finally {
      // 当前线程获取到锁再释放锁
      if (lock != null && lock.isHeldByCurrentThread()) {
 lock.unlock();
      }
    }
    return object;
  }

  public DistributedLock getRlockInfo(ProceedingJoinPoint proceedingJoinPoint) {
    MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();
    return methodSignature.getMethod().getAnnotation(DistributedLock.class);
  }

  
  public String getLocalKey(ProceedingJoinPoint proceedingJoinPoint, DistributedLock rlockInfo) {
    StringBuilder localKey = new StringBuilder("Rlock");
    final Object[] args = proceedingJoinPoint.getArgs();
    String businessNo = "";

    // 如果没有设置锁值
    if (StringUtils.isNotEmpty(rlockInfo.lockName())) {
      businessNo = rlockInfo.lockName();
    } else {
      MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();
      Class[] parameters = methodSignature.getParameterTypes();
      String methodName = methodSignature.getMethod().getName();

      if (parameters != null) {
 for (int i = 0; i < parameters.length; i++) {
   Class parameter = parameters[i];
   if (parameter.getSimpleName().equals("NDevice")) {
     NDevice de = (NDevice) args[i];
     businessNo = de.getUuid() + methodName;
   }
   if (parameter.getSimpleName().equals("frameBean")) {
     frameBean de = (frameBean) args[i];
     businessNo = de.getColumn1() + methodName;
   }
 }
 // 如果没有获取到业务编号,则使用方法签名
 if (StringUtils.isEmpty(businessNo)) {
   businessNo = methodName;
 }
      }
    }
    return businessNo;
  }
}

3、使用方法:在需要用分布式锁的方法上面加 @DistributedLock 注解即可

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/135460.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号