方法工具类封装

封装工具类UtilProcess

UtilProcess主要是使用函数式变成,提高代码的可读性、扩展性,用于处理方法内部异常捕获和接入第三方异常日志记录平台,同时还兼容了分布式锁,可根据业务以及开发需要,灵活调用不同的方法。话不多说,上代码

引入jar包

		<dependency>
            <groupId>org.redisson</groupId>
            <artifactId>redisson</artifactId>
            <version>3.17.4</version> <!-- 按照最新版本替换 -->
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.22</version> <!-- 按照最新版本替换 -->
            <scope>provided</scope>
        </dependency>

UtilProcess类

@Slf4j
public class UtilProcess {

    @Autowired
    private static RedissonClient redissonClient;

    /**
     * 主处理方法,支持执行逻辑并返回结果,带分布式锁。
     *
     * @param methodName 方法名(用于日志记录)
     * @param cls 类名
     * @param func       执行逻辑的函数接口
     * @param lockName   分布式锁名称
     * @param <T>        返回结果的类型
     * @return 执行结果
     */
    public static <T> T mainProcess(String methodName, String cls, Func<T> func, String lockName) {
        RLock lock = redissonClient.getLock(lockName);

        return executeWithLock(methodName, cls, func, lock);
    }

    /**
     * 主处理方法,支持执行逻辑并返回结果,不带分布式锁。
     *
     * @param methodName 方法名(用于日志记录)
     * @param cls 类名
     * @param func       执行逻辑的函数接口
     * @param <T>        返回结果的类型
     * @return 执行结果
     */
    public static <T> T mainProcess(String methodName, String cls, Func<T> func) {
        try {
            return func.execute();
        }catch (Exception e) {
            logError(cls, methodName, e);
            throw new RuntimeException("执行方法异常", e);
        }
    }

    /**
     * 加分布式锁处理
     * @param methodName
     * @param cls
     * @param func
     * @param lock
     * @return
     * @param <T>
     */
    private static <T> T executeWithLock(String methodName, String cls, Func<T> func, RLock lock) {
        try {
            boolean lockAcquired = lock.tryLock(10, 30, TimeUnit.SECONDS);
            if (lockAcquired) {
                try {
                    return func.execute();
                } finally {
                    lock.unlock();
                }
            } else {
                throw new RuntimeException("加锁失败:" + lock.getName());
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            logError(methodName, cls , e);
            throw new RuntimeException("等待锁时被中断", e);
        } catch (Exception e) {
            logError(methodName, cls , e);
            throw new RuntimeException("执行方法异常", e);
        }
    }

    /**
     * 接入日志平台
     * @param methodName
     * @param cls
     * @param e
     */
    private static void logError(String methodName, String cls, Exception e) {
        //接入日志平台
        log.error("类 " + cls + " 中的方法 " + methodName + " 执行过程中发生异常: " + e.getMessage(), e);
    }

    @FunctionalInterface
    interface Func<T> {
        T execute();
    }
}

测试类

public class TestUtilProcess {

    public static void main(String[] args) {
        TestUtilProcess test = new TestUtilProcess();
        test.testA();

        test.getUserInfo("1001");
    }

    public void testA() {
        UtilProcess.mainProcess("testA", "TestUtilProcess", () -> {
            int i = 0;
            int result = i / 0;
            System.out.println(result);
            return null;
        });
    }

    public User getUserInfo(String userId) {
        return UtilProcess.mainProcess("getUserInfo", "TestUtilProcess", () -> {
            List<User> users =new ArrayList<>();
            users.add(new User("1001", "李四", 12));
            users.add(new User("1002", "张三", 12));
            User res = users.stream().filter(e -> e.getUserId().equals(userId)).findFirst().orElse(null);
            return res;
        }, "");
    }
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值