JAVA-分布式锁

今天给大家带来一个项目中单模块加锁的方法

原理是通过springAOP前后置任务对需要调用的方法加锁,不懂aop的可以先去学习一下aop,这里就直接上锁代码了



//配置文件还没配置

package com.cfood.order.aop;

import io.lettuce.core.RedisClient;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.checkerframework.checker.units.qual.A;
import org.redisson.Redisson;
import org.redisson.RedissonFairLock;
import org.redisson.RedissonLock;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.UUID;


@Component
@Aspect
public class RedisLockAop {


    @Autowired(required = false)
    private RedissonClient redisson;


    private static String uuid;

    @Pointcut("@annotation(com.cfood.order.aop.RedisLockface)")
    public void point() {
    }

    // 前置通知
    @Before("point()")
    public void before(JoinPoint joinPoint) {
        // 获取当前方法所属的类对象
        Class declaringType = joinPoint.getSignature().getDeclaringType();
        System.out.println("当前方法所属的类对象:" + declaringType.getName());

        // 获取当前方法的名称
        String methodName = joinPoint.getSignature().getName();
        System.out.println("当前方法的名称:" + methodName);

        //当前使用非公平锁(待定)
        uuid = methodName;


        RLock lock = redisson.getLock(uuid);
        lock.lock();
        boolean locked = lock.isLocked();
        System.out.println("locked = " + locked);

        boolean heldByCurrentThread = lock.isHeldByCurrentThread();
        System.out.println("heldByCurrentThread = " + heldByCurrentThread);

        System.out.println("----------获得锁-----------");
    }

    // 后置通知     目标方法能正常执行
    @AfterReturning("point()")
    public void afterReturning() {

        RLock lock = redisson.getLock(uuid);

        if (lock.isHeldByCurrentThread()){
            System.out.println("heldByCurrentThread = " + lock.isHeldByCurrentThread());
            lock.unlock();
        }else if(lock.isLocked()){
            System.out.println("lock = " + lock.isLocked());
            lock.unlock();
        }

        System.out.println("----------释放锁-----------");
    }

    // 最终通知   始终要执行
    //@After("point()")
    public void after() {

    }

    //异常通知
    //@AfterThrowing("point()")
    public void afterThrowing() {
        RLock lock = redisson.getLock(uuid);
        lock.unlock();
        System.out.println("----------spring aop 异常通知-----------");
    }

    // 环绕通知
/*

  @Around("point()")
    public Object aroundAdvice(ProceedingJoinPoint joinPoint){

        Object proceed = null;
        try {
            System.out.println("----------spring aop 环绕 前通知-----------");
            proceed = joinPoint.proceed();
            Signature signature = joinPoint.getSignature();
            String declaringTypeName = joinPoint.getSignature().getDeclaringTypeName();
            Class aClass = Class.forName(declaringTypeName);

        } catch (Throwable throwable) {
            throwable.printStackTrace();
            System.out.println("----------spring aop 环绕 异常通知-----------");
        }finally {

        }
        return proceed;
    }
*/


}



你可能感兴趣的:(分布式锁)