JAVA-分布式锁

news/2024/7/7 22:39:37

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

原理是通过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;
    }
*/


}




http://lihuaxi.xjx100.cn/news/1693122.html

相关文章

DAC8563数模转换模块的使用介绍

前言 DAC8563为16位低功耗、电压输出、双通道的数模转换器&#xff0c;其包括一个2.5V4ppm/C 内部基准&#xff0c;从而提供了一个 2.5V 或 5V 的满量程输出电压范围。 此内部基准有一精度&#xff0c;并且能够在 VREFIN/VREFOUT引脚上提供或吸收高个 5mV 的初始达 20mA 的电流…

Go 存储系列:LSM存储引擎 LevelDB

概念介绍 LSM-Tree 被是一种面向写多读少应用场景的数据结构 &#xff0c;被 Hbase、RocksDB 等强力 NoSQL 数据库采用作为底层文件组织方式。 简单的LSM-Tree 包含 2 层树状数据结构&#xff1a; Memtable 并完全驻留在内存中&#xff08;假设 T0&#xff09; SStables 存储…

2、Flowable基础表结构

Flowable基础表结构 1.表结构讲解 工作流程的相关操作都是操作存储在对应的表结构中&#xff0c;为了能更好的弄清楚Flowable的实现原理和细节&#xff0c;我们有必要先弄清楚Flowable的相关表结构及其作用。在Flowable中的表结构在初始化的时候会创建五类表结构&#xff0c;具…

typescript实现一个简单的区块链

TypeScript 是一种由 Microsoft 推出的开源编程语言&#xff0c;它是 JavaScript 的超集&#xff0c;允许程序员使用面向对象的方式编写代码&#xff0c;并提供类型检查和语法提示等优秀的开发体验。区块链技术是一种分布式的、可靠的、不可篡改的数据库技术&#xff0c;用于记…

蓝桥杯每日一题2023.10.19

题目描述 完全二叉树的权值 - 蓝桥云课 (lanqiao.cn) 题目分析 我们以每一个节点的坐标来将这一深度的权值之和相加从而算出权值和 要清楚每一个深度的其实节点和末尾节点&#xff0c;使用双指针将这个深度节点的权值和计算出来&#xff0c;记录所 需要的深度即可 #includ…

GO 工程下载依赖操作流程(go mod)

1. 写一个main.go文件 package main import ("fmt""net/http""github.com/ClickHouse/clickhouse-go" ) func main() {fmt.Println("服务启动......")http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Requ…

乙酰基四肽-3/Acetyl Tetrapeptide-3——刺激毛囊,长出新头发,有效防止秃头

社会对头发很着迷。从圣经人物参孙&#xff08;他从头发中获得力量&#xff0c;并说如果剃光头他就会失去力量&#xff09;&#xff0c;到社交媒体上无休无止地谈论名人的标志性风格&#xff0c;头发是一个永恒的话题。 为什么痴迷&#xff1f;好吧&#xff0c;我们的头发是外…

滚动视图ScrollView

滚动视图分为两类&#xff1a;ScrollView(水平滚动视图) HorizontalScrollView(垂直滚动视图) 注意&#xff1a; &#xff08;1&#xff09;垂直滚动&#xff0c;layout_width要设为match_parent&#xff0c;layout_height要设为wrap_content。 &#xff08;2&#xff09;…