当前位置: 首页 > news >正文

使用lambda表达式提取共用代码使其更加简洁

1、在开发预下单接口访问并发问题出现需要加锁代码如下

        RLock lock = redissonClient.getLock(String.format(appointmentKey, activityId, studentId));try {boolean tryLock = lock.tryLock(10, 20, TimeUnit.SECONDS);if (tryLock) {AppointmentMallOrderInfoDTO appointmentMallOrderInfoDTO =mallOrderInfoRepository.findByActivityId(studentId,activityId);MallOrderInfo mallOrderInfo = MallOrderInfoFactory.createMallOrderInfo(classId, studentId, activityId);mallOrderInfo.appointment(mallActivity, mallProduct);mallOrderInfoRepository.save(mallOrderInfo);if (Objects.nonNull(appointmentMallOrderInfoDTO))mallOrderInfoRepository.deleteById(appointmentMallOrderInfoDTO.getId());}} catch (InterruptedException e) {e.printStackTrace();} finally {lock.unlock();}

问题说明:加锁是一套固定的写法,在不同地方重复写代码冗余,于是就想着重构。

2、使用抽象类,加抽象方法暴露业务逻辑。
封装类

public abstract class RedissonLockUtils<T> {private String key;private RedissonClient redissonClient;private RedissonLockUtils() {}public RedissonLockUtils(String key) {this.key = key;this.redissonClient = ApplicationContextUtils.getBean(RedissonClient.class);}public abstract <T> T handlerMethod();public <T> T lockMethod() {RLock lock = redissonClient.getLock(key);try {boolean tryLock = lock.tryLock(10, 20, TimeUnit.SECONDS);if (tryLock) {return handlerMethod();}} catch (InterruptedException e) {e.printStackTrace();} finally {lock.unlock();}return null;}
}

使用方法
在这里插入图片描述

3、因为目前使用的是DDD的方式开发,这种代码不够简洁。业务逻辑不是很清晰。于是想到了使用lambda方式在进行改造
封装类

public class LambdaRedissonLockUtils {private static Logger logger = LoggerFactory.getLogger(LambdaRedissonLockUtils.class);private RedissonClient redissonClient;private RLock lock;public LambdaRedissonLockUtils() {this.redissonClient = ApplicationContextUtils.getBean(RedissonClient.class);}public <P extends String> LambdaRedissonLockUtils key(Supplier<P> keySupplier){lock = redissonClient.getLock(keySupplier.get());return this;}public void noReturnHandlerMethod(NoReturnConsumer consumer) {try {boolean tryLock = lock.tryLock(10, 20, TimeUnit.SECONDS);if (tryLock) {consumer.apply();}} catch (InterruptedException e) {e.printStackTrace();logger.error(e.getMessage());} finally {lock.unlock();}}public <T> Optional<T> returnHandlerMethod(ReturnConsumer consumer){try {boolean tryLock = lock.tryLock(10, 20, TimeUnit.SECONDS);if (tryLock) {return consumer.apply();}} catch (InterruptedException e) {e.printStackTrace();logger.error(e.getMessage());} finally {lock.unlock();}return Optional.ofNullable(null);}
}
@FunctionalInterface
public interface ReturnConsumer<T> {Optional<T> apply();
}
@FunctionalInterface
public interface NoReturnConsumer {void apply();
}

使用方式
无返回值写法:
在这里插入图片描述

有返回值写法:
在这里插入图片描述

http://www.lryc.cn/news/136207.html

相关文章:

  • 【八股】2023秋招八股复习笔记3(智力题 非技术题50道)
  • 服务器卡顿如何排查?
  • 设计模式——开闭原则
  • 服务器能运行什么应用
  • Linux TCP协议
  • pytorch 入门1-tensor 广播 view reshape
  • Spring参数注解,支持数组入参(List)校验
  • 如何使用ArcGIS进行可视化分析
  • 计算机竞赛 基于LSTM的天气预测 - 时间序列预测
  • uniapp 回退到指定页面 保存页面状态
  • ansible(1)-- 部署ansible连接被控端
  • Log4j反序列化命令执行漏洞(CVE-2017-5645)Apache Log4j2 lookup JNDI 注入漏洞(CVE-2021-44228)
  • echarts 之 科技感进度条
  • 基于gin关于多级菜单的处理
  • Oracle/PL/SQL奇技淫巧之Lable标签与循环控制
  • Docker基础操作
  • AMBA总线协议(8)——AHB(六):分割传输
  • 时序分解 | MATLAB实现基于SWD群体分解的信号分解分量可视化
  • 【makefile】自动化变量的简述及实例
  • IntelliJ IDEA 官方网站 idea官网 http://www.jetbrains.com/idea/
  • C#,《小白学程序》第一课:初识程序
  • LeetCode--HOT100题(38)
  • C语言:指针(超深度讲解)
  • Docker详解
  • 软件开发方法:复用与扩展
  • C++新经典09--函数新特性、inline内联函数与const详解
  • C++中机器人应用程序的行为树(ROS2)
  • 像Vuex一样使用redux
  • 关于模板的大致认识【C++】
  • C#如何遍历类的属性,并获取描述/注释