View Javadoc

1   package com.atlassian.vcache;
2   
3   import java.util.concurrent.CompletableFuture;
4   
5   public abstract class AsyncTesting {
6       public static void main(String[] args) {
7           /*
8           System.out.println("And away");
9   
10          System.out.println("retrieved: " + retrieve().unsafeJoin());
11  
12          System.out.println("retrieved: " + retrieve().thenApply(s -> ">>" + s + "<<").unsafeJoin());
13  
14          System.out.println("isDone: " + willFail().isDone());
15          System.out.println("isCancelled: " + willFail().isCancelled());
16          System.out.println("isCompletedExceptionally: " + willFail().isCompletedExceptionally());
17          CompletableFuture<String> outer = willFail().thenApply(s -> ">>" + s + "<<");
18          System.out.println("isDone: " + outer.isDone());
19          System.out.println("isCancelled: " + outer.isCancelled());
20          System.out.println("isCompletedExceptionally: " + outer.isCompletedExceptionally());
21  
22          System.out.println("--> " + retrieve().handle((s, throwable) -> {
23              if (throwable != null && throwable instanceof RuntimeException)
24              {
25                  throw (RuntimeException) throwable;
26              }
27              return s;
28          }).unsafeJoin());
29          System.out.println("--> " + willFail().handle((s, ex) -> (ex != null) ? "failed!!" : s).unsafeJoin());
30  */
31  //        final CompletableFuture<String> get1 = willFail("first stage");
32          final CompletableFuture<String> get1 = retrieve("get1");
33  //        final CompletableFuture<String> get2 = willFail(); //retrieve();
34  //        final CompletableFuture<String> combined = get1.thenCombine(get2, (s1, s2) -> s1 + " <--> " + s2);
35  //        get1.runAfterEither(get2, () -> System.out.println("Finished"));
36  
37  //        final CompletableFuture<String> combined = get1.thenCompose(s -> retrieve("get2--" + s));
38          final CompletableFuture<String> combined = get1.thenCompose(s -> willFail("second stage"));
39  
40          final String combinedStr = combined.handle((val, err) -> {
41              System.out.println("\n================");
42              System.out.println("\tval = " + val);
43              System.out.println("\terr = " + err);
44              System.out.println("================");
45              return val;
46          }).join();
47  
48          System.out.println("combinedStr = " + combinedStr);
49      }
50  
51      private static CompletableFuture<String> retrieve(String arg) {
52          return CompletableFuture.completedFuture(arg);
53      }
54  
55      private static CompletableFuture<String> willFail(String stage) {
56          final CompletableFuture<String> result = new CompletableFuture<>();
57          result.completeExceptionally(new RuntimeException(stage));
58  //        result.completeExceptionally(new ExternalCacheException(
59  //                new ExternalCacheResult.Failure(ExternalCacheResult.Failure.Reason.UNCLASSIFIED_FAILURE)));
60          return result;
61      }
62  }