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

时间:2019-04-18 12:24:20

标签: java concurrency rmi

两个远程对象是相关的,应在一次操作中进行更新。如果假设两个对象都可以在不同的系统上并且可以同时进行多个操作,那么您需要哪种服务?

1 个答案:

答案 0 :(得分: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())
    );