gpt4 book ai didi

java - ExecutorService 的 future 返回值

转载 作者:行者123 更新时间:2023-12-02 11:40:20 26 4
gpt4 key购买 nike

各位程序员大家好,

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

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(); 返回的值。当将其保留为字符串变量时,我可以轻松地将结果写入 csv 文件。但我需要返回一个带有电子邮件+状态的字符串。

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

有人知道我需要如何更改架构吗?

据我见过类似的问题,我可能会使用 Callable,但无法让它工作以从 ldap_call() 函数获取值

最佳答案

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

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

关于java - ExecutorService 的 future 返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48622786/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com