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

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

我为员工创建了一个简单的 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?


ibeautiful
浏览 99回答 2
2回答

www说

您必须将三个CompletableFutures 的结果合并为一个EmployeeDTO. 这不是魔术般地完成的allOf。尝试这样的事情(未经测试):CompletableFuture allCompletableFutures = CompletableFuture.allOf(&nbsp; employeeCfWithName, employeeCfWithAccountNumber, employeeCfWithSalary);// Wait for all three to complete.allCompletableFutures.get();// Now join all three and combine the results.EmployeeDTO finalResult = EmployeeDTO.builder()&nbsp; .name(new employeeCfWithName.join())&nbsp; .accountNumber(new employeeCfWithAccountNumber.join())&nbsp; .salary(new employeeCfWithSalary.join())&nbsp; .build();这看起来有点傻。我们在每种方法中都使用一个构建器,只是为了使用第四个构建器组合它们的结果。

茅侃侃

让您的员工服务函数将EmployeeDTO.builder()作为输入,然后在 run() 函数中创建一个构建器,并在所有 supplyAsync 调用中将该构建器传递给服务。还要确保在调用 allOf() 之后调用 build() ,这保证了每个服务调用都完成了它的一部分。也不要内置单独的服务功能 -public class EmployeeService {&nbsp; &nbsp; public EmployeeDTO setName(EmployeeDTO.EmployeeDTOBuilder builder) {&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Thread.sleep(10 * 1000);&nbsp; &nbsp; &nbsp; &nbsp; } catch (InterruptedException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return builder.name("John Doe");&nbsp; &nbsp; }&nbsp; &nbsp; public EmployeeDTO setAccountNumber(EmployeeDTO.EmployeeDTOBuilder builder) {&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Thread.sleep(10 * 1000);&nbsp; &nbsp; &nbsp; &nbsp; } catch (InterruptedException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return builder.accountNumber("1235");&nbsp; &nbsp; }&nbsp; &nbsp; public EmployeeDTO setSalary(EmployeeDTO.EmployeeDTOBuilder builder) {&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Thread.sleep(10 * 1000);&nbsp; &nbsp; &nbsp; &nbsp; } catch (InterruptedException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new RuntimeException(e);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return builder.salary(100000);&nbsp; &nbsp; }}private void run() {&nbsp; &nbsp; &nbsp; &nbsp; EmployeeService employeeService = new EmployeeService();&nbsp; &nbsp; &nbsp; &nbsp; EmployeeDTO.EmployeeDTOBuilder builder = EmployeeDTO.builder();&nbsp; &nbsp; &nbsp; &nbsp; CompletableFuture<EmployeeDTO> employeeCfWithName = CompletableFuture&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .supplyAsync(()-> emoplyeeService.setName(builder));&nbsp; &nbsp; &nbsp; &nbsp; CompletableFuture<EmployeeDTO> employeeCfWithAccountNumber = CompletableFuture&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .supplyAsync(()-> emoplyeeService.setAccount(builder));&nbsp; &nbsp; &nbsp; &nbsp; CompletableFuture<EmployeeDTO> employeeCfWithSalary = CompletableFuture&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .supplyAsync(()-> emoplyeeService.setSalary(builder));&nbsp; &nbsp; &nbsp; &nbsp; CompletableFuture allCompletableFutures = CompletableFuture.allOf(employeeCfWithName, employeeCfWithAccountNumber, employeeCfWithSalary);&nbsp; &nbsp; &nbsp; &nbsp; builder.build();&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java