ExecutorService的未来返回值

时间:2018-02-05 12:38:06

标签: java multithreading ldap

Hello其他程序员,

我目前正致力于从为我们公司工作的用户检索信息

public class SCD_Checker {
    private final ExecutorService service = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
    private String mail = "";
    private String stat;


    private Connection conn = new Connection();

    SCD_Checker() {
        conn.setProvider("ADsDSOObject");
        conn.Open("Active Directory Provider", "", "", 0);
    }

    private void ldap_call(String email) {
        this.mail = email;
        Command objCmd = new Command();
        Recordset RS = new Recordset();
        objCmd.setActiveConnection(conn);
        String searchKeyword = "contractstatus";
        objCmd.setCommandText("XXX:LDAP_CONN)(mail=" + email + "));" + searchKeyword + ";subTree");

        RS = objCmd.Execute();
        if (RS.getBOF()) this.stat = "Not in SCD";
        else {
            RS.MoveFirst();
            Variant value = RS.getFields().getItem(0).getValue();

            if (value.getString().equals("R")) this.stat = "Retired";
            else if (value.getString().equals("A")) this.stat = "Active";
            else if (value.getString().equals("null")) this.stat = "Not in SCD";
        }
    }

    public void submit_task(String email) {
        this.service.submit(() -> ldap_call(email));
    }

    public String getMail() {
        return this.mail;
    }

    public String getStat() {
        return this.stat;
    }

    public void shutdown_and_close() throws InterruptedException {
        this.service.shutdown();
        this.service.awaitTermination(1, TimeUnit.MINUTES);
        this.conn.Close();
    }
}

这是我到目前为止工作的代码,问题是我似乎构建错误,因为如果我尝试使用 stat 变量创建一个Future,我无法保存在stat变量中从Variant value = RS.getFields().getItem(0).getValue();返回的值。将它作为String变量保留时,我可以轻松地将结果写入csv文件。但我需要返回一个带有电子邮件的字符串+状态。

我的架构中似乎有些错误,但我似乎无法知道如何修复当前程序以正确返回值,因为没有任何函数返回值。

有没有人有想法,我需要如何改变架构?

就我看到的类似问题而言,我可能会使用Callable,但无法通过 ldap_call()函数获取值

1 个答案:

答案 0 :(得分:1)

我认为你需要调整你的架构。

  1. 如果要从Java函数返回多个值,则可以 创建一个汇编类,在这里你可以创建WorkerDTO 包含统计和电子邮件:

    public class WorkerDTO {
    
        private String email;
        private String stat;
    
        public String getEmail() {
            return email;
        }
    
        public void setEmail(String email) {
            this.email = email;
        }
    
        public String getStat() {
            return stat;
        }
    
        public void setStat(String stat) {
            this.stat = stat;
        }
    }
    
  2. 你应该提取一个接受电子邮件的Callable课程 返回WorkerDTO:

    public class CheckStatusCallable implements Callable<WorkerDTO> {
    
        private Connection connection;
        private String email;
    
        public CheckStatusCallable(Connection connection, String email) {
            this.connection = connection;
            this.email = email;
        }
    
        @Override
        public WorkerDTO call() throws Exception {
            Command objCmd = new Command();
            Recordset RS = new Recordset();
            objCmd.setActiveConnection(conn);
            String searchKeyword = "contractstatus";
            objCmd.setCommandText("XXX:LDAP_CONN)(mail=" + this.email + "));" + searchKeyword + ";subTree");
    
            RS = objCmd.Execute();
            WorkerDTO workerDTO = new WorkerDTO();
            workerDTO.setEmail(this.email);
            workerDTO.setEmail(e);
            if (RS.getBOF()) workerDTO.setStat("Not in SCD");
            else {
                RS.MoveFirst();
                Variant value = RS.getFields().getItem(0).getValue();
    
                if (value.getString().equals("R")) workerDTO.setStat("Retired");
                else if (value.getString().equals("A")) workerDTO.setStat("Active");
                else if (value.getString().equals("null")) workerDTO.setStat("Not in SCD");
            }
            return workerDTO;
        }
    }
    
  3. 在SCD_Checker中,只需使用CheckStatusCallable创建新任务 并将其提交给遗嘱执行人。

    public class SCD_Checker {
        private final ExecutorService service = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
    
        private Connection conn = new Connection();
    
        SCD_Checker() {
            conn.setProvider("ADsDSOObject");
            conn.Open("Active Directory Provider", "", "", 0);
        }
    
        public Future<WorkerDTO> submit_task(String email) {
            return this.service.submit(new CheckStatusCallable(conn, email));
        }
    
        public void shutdown_and_close() throws InterruptedException {
            this.service.shutdown();
            this.service.awaitTermination(1, TimeUnit.MINUTES);
            this.conn.Close();
        }
    }
    
  4. 测试是否适用于:

    public class Test {
    
        public static void main(String[] args) {
            SCD_Checker checker = new SCD_Checker();
            Future<WorkerDTO> workerDTOFuture = checker.submit_task("test@test.com");
            WorkerDTO workerDTO = workerDTOFuture.get();
            String email = workerDTO.getEmail();
            String stat = workerDTO.getStat();
        }
    }