You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何重构仅单一行不同的LDAP并行操作重复方法?

问题描述

我正在开发针对LDAP服务器的并行操作方法,目前存在多个重复方法,这些方法仅内部调用的LdapTransactions方法不同,且被调用方法的签名不一致。示例代码如下:

public static void addEntriesInParallel(LdapConnParams ldapConnParams, List<LDAPEntry> entries) {
    ExecutorService executor = Executors.newCachedThreadPool();
    CountDownLatch latch = new CountDownLatch(batchCount(entries.size()));
    LdapConnParamsBuilder connParamsBuilder = new LdapConnParamsBuilder(ldapConnParams);

    for (List<LDAPEntry> entryBatch : Lists.partition(entries, BATCH_SIZE)) {
        LdapConnParams threadConnParams = connParamsBuilder.build();

        executor.submit(() -> {
            try {
                LdapTransactions.addEntries(threadConnParams, entryBatch);
                latch.countDown();
            } catch (LDAPException ex) {
                // Exception handling
            }
        });
    }

    try {
        latch.await();
    } catch (InterruptedException ex) {
        // Exception handling
    }
}

public static void deleteAttributeInParallel(LdapConnParams ldapConnParams, String attribute, List<LDAPEntry> entries) {
    ExecutorService executor = Executors.newCachedThreadPool();
    CountDownLatch latch = new CountDownLatch(batchCount(entries.size()));
    LdapConnParamsBuilder connParamsBuilder = new LdapConnParamsBuilder(ldapConnParams);

    for (List<LDAPEntry> entryBatch : Lists.partition(entries, BATCH_SIZE)) {
        LdapConnParams threadConnParams = connParamsBuilder.build();

        executor.submit(() -> {
            try {
                LdapTransactions.deleteAttribute(threadConnParams, attribute, entryBatch);
                latch.countDown();
            } catch (LDAPException ex) {
                // Exception handling
            }
        });
    }

    try {
        latch.await();
    } catch (InterruptedException ex) {
        // Exception handling
    }
}

两个方法的唯一差异分别是调用LdapTransactions.addEntries(threadConnParams, entryBatch);和LdapTransactions.deleteAttribute(threadConnParams, attribute, entryBatch);。请问如何改写代码消除重复,同时保证代码可读性?

优化方案

核心思路是抽离通用的并行执行框架,将差异化的LDAP操作逻辑通过函数式接口传入,利用Java的lambda表达式适配不同签名的操作方法。

步骤1:定义通用并行执行方法

把重复的线程池创建、批量拆分、CountDownLatch控制等逻辑抽成私有通用方法,接收差异化的操作逻辑作为参数:

private static void executeLdapOperationInParallel(
        LdapConnParams ldapConnParams,
        List<LDAPEntry> entries,
        BiConsumer<LdapConnParams, List<LDAPEntry>> batchOperation) {
    ExecutorService executor = Executors.newCachedThreadPool();
    int batchCount = batchCount(entries.size());
    CountDownLatch latch = new CountDownLatch(batchCount);
    LdapConnParamsBuilder connParamsBuilder = new LdapConnParamsBuilder(ldapConnParams);

    for (List<LDAPEntry> entryBatch : Lists.partition(entries, BATCH_SIZE)) {
        LdapConnParams threadConnParams = connParamsBuilder.build();

        executor.submit(() -> {
            try {
                batchOperation.accept(threadConnParams, entryBatch);
                latch.countDown();
            } catch (LDAPException ex) {
                // 统一的异常处理逻辑,可根据需求调整
            }
        });
    }

    try {
        latch.await();
    } catch (InterruptedException ex) {
        // 统一的中断异常处理
    } finally {
        executor.shutdown(); // 关闭线程池,避免资源泄漏
    }
}

步骤2:简化原有业务方法

原有两个并行操作方法只需调用通用方法,通过lambda表达式传入具体的LDAP操作逻辑,利用lambda的闭包特性捕获额外参数(比如deleteAttribute需要的attribute):

public static void addEntriesInParallel(LdapConnParams ldapConnParams, List<LDAPEntry> entries) {
    executeLdapOperationInParallel(ldapConnParams, entries, 
        (conn, batch) -> LdapTransactions.addEntries(conn, batch));
}

public static void deleteAttributeInParallel(LdapConnParams ldapConnParams, String attribute, List<LDAPEntry> entries) {
    executeLdapOperationInParallel(ldapConnParams, entries, 
        (conn, batch) -> LdapTransactions.deleteAttribute(conn, attribute, batch));
}

方案优势

  • 消除重复代码:所有通用的并行控制逻辑只维护一份,降低后续维护成本
  • 可读性高:业务方法只保留核心操作逻辑,一目了然;通用方法专注于并行框架的实现
  • 扩展性强:新增其他LDAP并行操作时,只需新增一个简单的包装方法,传入对应的lambda即可

内容的提问来源于stack exchange,提问作者Samuel Toman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 23:24:37