2016-04-11 28 views

回答

0

我找不到CompletableStage的方法來做到這一點,所以做了它自己:

public static <A, B> void handleEitherException(CompletionStage<A> a, CompletionStage<B> b, Consumer<Throwable> onException) { 
    AtomicBoolean aCompletionStageHasFailed = new AtomicBoolean(); 
    handleEitherException(a, onException, aCompletionStageHasFailed); 
    handleEitherException(b, onException, aCompletionStageHasFailed); 
} 

private static <U> void handleEitherException(CompletionStage<U> u, Consumer<Throwable> onException, AtomicBoolean aCompletionStageHasFailed) { 
    u.handle((a, t) -> { 
     if (t != null) { 
      if (aCompletionStageHasFailed.compareAndSet(false, true)) { 
       onException.accept(t); 
       throw new RuntimeException(t); 
      } 
     } 
     return null; 
    }); 
} 

```

相關問題