使用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();
}
使用方式
无返回值写法:
有返回值写法: