使用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();
}

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sunnyboy_4

你的鼓励是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值