Java 8 Completable Futures allOf different data types

后端 未结 4 1617
你的背包
你的背包 2020-12-14 09:52

I have 3 CompletableFutures all 3 returning different data types.

I am looking to create a result object that is a composition of the result returned by all the 3 fu

相关标签:
4条回答
  • 2020-12-14 10:28

    Another way to handle this if you don't want to declare as many variables is to use thenCombine or thenCombineAsync to chain your futures together.

    public CompletableFuture<ClassD> getResultClassD()
    {
      return CompletableFuture.supplyAsync(ClassD::new)
        .thenCombine(CompletableFuture.supplyAsync(service::getClassA), (d, a) -> {
          d.setClassA(a);
          return d;
        })
        .thenCombine(CompletableFuture.supplyAsync(service::getClassB), (d, b) -> {
          d.setClassB(b);
          return d;
        })
        .thenCombine(CompletableFuture.supplyAsync(service::getClassC), (d, c) -> {
          d.setClassC(c);
          return d;
        });
    }
    

    The getters will still be fired off asynchronously and the results executed in order. It's basically another syntax option to get the same result.

    0 讨论(0)
  • 2020-12-14 10:29

    I was going down a similar route to what @Holger was doing in his answer, but wrapping the Service Calls in an Optional, which leads to cleaner code in the thenApplyAsync stage

    CompletableFuture<Optional<ClassA>> classAFuture
        = CompletableFuture.supplyAsync(() -> Optional.ofNullable(service.getClassA())));
    
    CompletableFuture<Optional<ClassB>> classBFuture
        = CompletableFuture.supplyAsync(() -> Optional.ofNullable(service.getClassB()));
    
    CompletableFuture<Optional<ClassC>> classCFuture
        = CompletableFuture.supplyAsync(() -> Optional.ofNullable(service.getClassC()));
    
    return CompletableFuture.allOf(classAFuture, classBFuture, classCFuture)
         .thenApplyAsync(dummy -> {
            ClassD resultClass = new ClassD();
    
            classAFuture.join().ifPresent(resultClass::setClassA)
            classBFuture.join().ifPresent(resultClass::setClassB)
            classCFuture.join().ifPresent(resultClass::setClassC)
    
            return resultClass;
         });
    
    0 讨论(0)
  • 2020-12-14 10:31

    Your attempt is going into the right direction, but not correct. Your method getResultClassD() returns an already instantiated object of type ClassD on which an arbitrary thread will call modifying methods, without the caller of getResultClassD() noticing. This can cause race conditions, if the modifying methods are not thread safe on their own, further, the caller will never know, when the ClassD instance is actually ready for use.

    A correct solution would be:

    public CompletableFuture<ClassD> getResultClassD() {
    
        CompletableFuture<ClassA> classAFuture
            = CompletableFuture.supplyAsync(() -> service.getClassA() );
        CompletableFuture<ClassB> classBFuture
            = CompletableFuture.supplyAsync(() -> service.getClassB() );
        CompletableFuture<ClassC> classCFuture
            = CompletableFuture.supplyAsync(() -> service.getClassC() );
    
        return CompletableFuture.allOf(classAFuture, classBFuture, classCFuture)
             .thenApplyAsync(dummy -> {
                ClassD resultClass = new ClassD();
    
                ClassA classA = classAFuture.join();
                if (classA != null) {
                    resultClass.setClassA(classA);
                }
    
                ClassB classB = classBFuture.join();
                if (classB != null) {
                    resultClass.setClassB(classB);
                }
    
                ClassC classC = classCFuture.join();
                if (classC != null) {
                    resultClass.setClassC(classC);
                }
    
                return resultClass;
             });
    }
    

    Now, the caller of getResultClassD() can use the returned CompletableFuture to query the progress state or chain dependent actions or use join() to retrieve the result, once the operation is completed.

    To address the other questions, yes, this operation is asynchronous and the use of join() within the lambda expressions is appropriate. join was exactly created because Future.get(), which is declared to throw checked exceptions, makes the use within these lambda expressions unnecessarily hard.

    Note that the null tests are only useful, if these service.getClassX() can actually return null. If one of the service calls fails with an exception, the entire operation (represented by CompletableFuture<ClassD>) will complete exceptionally.

    0 讨论(0)
  • 2020-12-14 10:36

    I ran into something similar before and created a short demo to show how I solved this issue.

    Similar concept to @Holger except I used a function to combine each individual future.

    https://github.com/te21wals/CompletableFuturesDemo

    Essentially:

        public class CombindFunctionImpl implement CombindFunction {
        public ABCData combind (ClassA a, ClassB b, ClassC c) {
            return new ABCData(a, b, c);
       }
    }
    

    ...

        public class FutureProvider {
    public CompletableFuture<ClassA> retrieveClassA() {
        return CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return new ClassA();
        });
    }
    
    public CompletableFuture<ClassB> retrieveClassB() {
        return CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(2000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return new ClassB();
        });
    }
    public CompletableFuture<ClassC> retrieveClassC() {
        return CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(3000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return new ClassC();
        });
    }
    }
    

    ......

    public static void main (String[] args){
        CompletableFuture<ClassA> classAfuture = futureProvider.retrieveClassA();
        CompletableFuture<ClassB> classBfuture = futureProvider.retrieveClassB();
        CompletableFuture<ClassC> classCfuture = futureProvider.retrieveClassC();
    
        System.out.println("starting completable futures ...");
        long startTime = System.nanoTime();
    
        ABCData ABCData = CompletableFuture.allOf(classAfuture, classBfuture, classCfuture)
                .thenApplyAsync(ignored ->
                        combineFunction.combind(
                                classAfuture.join(),
                                classBfuture.join(),
                                classCfuture.join())
                ).join();
    
        long endTime = System.nanoTime();
        long duration = (endTime - startTime);
        System.out.println("completable futures are complete...");
        System.out.println("duration:\t" + Duration.ofNanos(duration).toString());
        System.out.println("result:\t" + ABCData);
    }
    
    0 讨论(0)
提交回复
热议问题