0

Two remote objects are related and should be updated in one operation. What kind of service do you need for this if you assume that both objects can be on different systems and that multiple operations can happen concurrently?

  • You can use CompletableFuture. – howie Apr 18 '19 at 12:30
  • It might be a "Distributed Transaction" https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-jta.html https://en.wikipedia.org/wiki/X/Open_XA I think the major issues are , what if one service commits but the other fails? What do you mean by "concurrently"? Like both operations to each object can be performed concurrently? but they are still part of the same logical transaction? ie both operations? Is there an aspect of atomicity to both updates? – dm03514 Apr 18 '19 at 14:55

1 Answers1

0

You can use CompletableFuture , for example


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

howie
  • 2,587
  • 3
  • 27
  • 43