gpt4 book ai didi

java - 使用 CompletableFuture 构建具有多种方法的对象

转载 作者:搜寻专家 更新时间:2023-11-01 03:45:44 24 4
gpt4 key购买 nike

我正在尝试了解 CompletableFuture 以及如何利用它来构建一个包含从多个端点获取的信息的对象。我遇到过几个例子,但没有一个是完全适合我的问题的。例如,this one正在并行运行相同的方法以获取字符串列表,我想在其中并行运行多个方法来构建和返回一个对象。

我为员工创建了一个简单的 DTO:

@Builder
@Data
@AllArgsConstructor
public class EmployeeDTO {

private String name;
private String accountNumber;
private int salary;

}

我创建了一个服务来模拟对三个独立 API 的调用,以设置 DTO 的值并等待相当长的时间:

public class EmployeeService {

public EmployeeDTO setName() {
try {
Thread.sleep(10 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return EmployeeDTO.builder().name("John Doe").build();
}

public EmployeeDTO setAccountNumber() {
try {
Thread.sleep(10 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return EmployeeDTO.builder().accountNumber("1235").build();
}

public EmployeeDTO setSalary() {
try {
Thread.sleep(10 * 1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
return EmployeeDTO.builder().salary(100000).build();
}
}

我可以supplyAsync 所有三种方法,然后运行allOf 但它什么也没做:

    private void run() {
EmployeeService employeeService = new EmployeeService();

CompletableFuture<EmployeeDTO> employeeCfWithName = CompletableFuture
.supplyAsync(employeeService::setName);
CompletableFuture<EmployeeDTO> employeeCfWithAccountNumber = CompletableFuture
.supplyAsync(employeeService::setAccountNumber);
CompletableFuture<EmployeeDTO> employeeCfWithSalary = CompletableFuture
.supplyAsync(employeeService::setSalary);

CompletableFuture allCompletableFutures = CompletableFuture.allOf(employeeCfWithName, employeeCfWithAccountNumber, employeeCfWithSalary);
}

如何将结果合并到一个 EmployeeDTO 中?

最佳答案

您必须将三个 CompletableFuture 的结果合并到一个 EmployeeDTO 中。这不是 allOf 神奇地完成的。

尝试这样的事情(未经测试):

CompletableFuture allCompletableFutures = CompletableFuture.allOf(
employeeCfWithName, employeeCfWithAccountNumber, employeeCfWithSalary);

// Wait for all three to complete.
allCompletableFutures.get();

// Now join all three and combine the results.
EmployeeDTO finalResult = EmployeeDTO.builder()
.name(new employeeCfWithName.join())
.accountNumber(new employeeCfWithAccountNumber.join())
.salary(new employeeCfWithSalary.join())
.build();

这看起来有点傻。我们在每个方法中都使用一个构建器,只是为了使用第四个构建器组合它们的结果。

关于java - 使用 CompletableFuture 构建具有多种方法的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57305987/

24 4 0