View Javadoc

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