欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java注解如何基于Redission實(shí)現(xiàn)分布式鎖

 更新時間:2020年01月15日 11:21:31   作者:深藏的豆沙包  
這篇文章主要介紹了Java注解如何基于Redission實(shí)現(xiàn)分布式鎖,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

這篇文章主要介紹了Java注解如何基于Redission實(shí)現(xiàn)分布式鎖,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

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;
   //這里需要修改對應(yīng)的包名
  @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 {
      // 當(dāng)前線程獲取到鎖再釋放鎖
      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);
  }

  /**
   * 獲取redis lock key
   *
   * @param proceedingJoinPoint
   * @return
   */
  public String getLocalKey(ProceedingJoinPoint proceedingJoinPoint, DistributedLock rlockInfo) {
    StringBuilder localKey = new StringBuilder("Rlock");
    final Object[] args = proceedingJoinPoint.getArgs();
    String businessNo = "";

    // 如果沒有設(shè)置鎖值
    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;
          }
        }
        // 如果沒有獲取到業(yè)務(wù)編號,則使用方法簽名
        if (StringUtils.isEmpty(businessNo)) {
          businessNo = methodName;
        }
      }
    }
    return businessNo;
  }
}

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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 圖文詳解SpringBoot中Log日志的集成

    圖文詳解SpringBoot中Log日志的集成

    這篇文章主要給大家介紹了關(guān)于SpringBoot中Log日志的集成的相關(guān)資料,文中通過實(shí)例代碼以及圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2021-12-12
  • Spring JdbcTemplate執(zhí)行數(shù)據(jù)庫操作詳解

    Spring JdbcTemplate執(zhí)行數(shù)據(jù)庫操作詳解

    JdbcTemplate是Spring框架自帶的對JDBC操作的封裝,目的是提供統(tǒng)一的模板方法使對數(shù)據(jù)庫的操作更加方便、友好,效率也不錯,這篇文章主要介紹了Spring JdbcTemplate執(zhí)行數(shù)據(jù)庫操作,需要的朋友可以參考下
    2022-10-10
  • Spring Boot中使用RabbitMQ的示例代碼

    Spring Boot中使用RabbitMQ的示例代碼

    本篇文章主要介紹了Spring Boot中使用RabbitMQ的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-04-04
  • java 獲取項(xiàng)目文件路徑實(shí)現(xiàn)方法

    java 獲取項(xiàng)目文件路徑實(shí)現(xiàn)方法

    以下是對java中獲取項(xiàng)目文件路徑的實(shí)現(xiàn)方法進(jìn)行了介紹,需要的朋友可以過來參考下
    2013-09-09
  • Mybatis之collection標(biāo)簽中javaType和ofType屬性的區(qū)別說明

    Mybatis之collection標(biāo)簽中javaType和ofType屬性的區(qū)別說明

    這篇文章主要介紹了Mybatis之collection標(biāo)簽中javaType和ofType屬性的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • Mybatis Plus查詢時sql字段名大小寫報錯的解決

    Mybatis Plus查詢時sql字段名大小寫報錯的解決

    這篇文章主要介紹了Mybatis Plus查詢時sql字段名大小寫報錯的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Spring Boot 定制URL匹配規(guī)則的方法

    Spring Boot 定制URL匹配規(guī)則的方法

    本篇文章主要介紹了Spring Boot 定制URL匹配規(guī)則的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-02-02
  • java中Base64編碼原理實(shí)例講解

    java中Base64編碼原理實(shí)例講解

    這篇文章主要介紹了java中Base64編碼原理實(shí)例講解,文章講解的很清晰,有對于這方面不太懂的同學(xué)可以研究下
    2021-02-02
  • Java日志框架之logback使用詳解

    Java日志框架之logback使用詳解

    這篇文章主要介紹了Java日志框架之logback使用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • Spring Properties的使用和配置方法

    Spring Properties的使用和配置方法

    這篇文章主要介紹了Spring Properties的使用和配置方法,本文不是原理分析、源碼分析文章,只是希望可以幫助讀者更好地理解和使用 Spring Properties,有興趣的可以了解一下
    2018-01-01

最新評論