我们需要在一次操作中更新两个远程对象需要什么样的服务

问题描述 投票:0回答:1

两个远程对象是相关的,应该在一个操作中更新。如果您假设两个对象可以位于不同的系统上并且多个操作可以同时发生,那么您需要什么样的服务?

java concurrency rmi
1个回答
0
投票

例如,您可以使用CompletableFuture


// create promises to get product
    CompletableFuture<List<String>> product1 = CompletableFuture.completedFuture(callService1());
    CompletableFuture<List<String>> product2 = CompletableFuture.completedFuture(callService2());
    CompletableFuture<List<String>> product3 = CompletableFuture.completedFuture(callService3());

    // collect promises just for convenience
    List<CompletableFuture<List<String>>> allFutures = Arrays.asList(product1, product2, product3);

    // wait until all cars will be obtained
    CompletableFuture<List<String>> listCompletableFuture =
            CompletableFuture.allOf(product1, product2, product3)
            .thenApply(avoid -> allFutures  //start to collect them
                    .stream()
                    .flatMap(f -> f.join().stream()) //get List from feature. Here these cars has been obtained, therefore non blocking
                    .collect(Collectors.toList())
    );

© www.soinside.com 2019 - 2024. All rights reserved.