|
| 1 | +package org.example.service.completablefuture_approach; |
| 2 | + |
| 3 | +import org.example.service.HelloWorldService; |
| 4 | + |
| 5 | +import java.util.concurrent.CompletableFuture; |
| 6 | + |
| 7 | +import static org.example.util.CommonUtil.delay; |
| 8 | +import static org.example.util.CommonUtil.startTimer; |
| 9 | +import static org.example.util.CommonUtil.timeTaken; |
| 10 | +import static org.example.util.LoggerUtil.log; |
| 11 | + |
| 12 | +/** |
| 13 | + * @author milad mofidi |
| 14 | + |
| 15 | + * user: miladm on 2/17/2023 |
| 16 | + */ |
| 17 | +public class CompletableFutureHelloWorldException |
| 18 | +{ |
| 19 | + HelloWorldService helloWorldService; |
| 20 | + |
| 21 | + public CompletableFutureHelloWorldException(HelloWorldService helloWorldService) |
| 22 | + { |
| 23 | + this.helloWorldService = helloWorldService; |
| 24 | + } |
| 25 | + |
| 26 | + public String helloWorld_3_async_calls_handle() |
| 27 | + { |
| 28 | + startTimer(); |
| 29 | + CompletableFuture<String> hello = CompletableFuture.supplyAsync(() -> helloWorldService.hello()); |
| 30 | + CompletableFuture<String> world = CompletableFuture.supplyAsync(() -> helloWorldService.world()); |
| 31 | + CompletableFuture<String> hiCompletableFuture = CompletableFuture.supplyAsync(() -> { |
| 32 | + delay(1000); |
| 33 | + return " Hi CompletableFuture!"; |
| 34 | + }); |
| 35 | + String result = hello |
| 36 | + .handle((res, exception) -> { //handle exception for hello completable future here if there is a exception from hello() method since we have returned the empty string for hello cf so the result would be="" |
| 37 | + log("res is: " + res); |
| 38 | + if (exception != null) |
| 39 | + { |
| 40 | + log("Exception is: " + exception.getMessage()); |
| 41 | + return ""; |
| 42 | + } |
| 43 | + else |
| 44 | + { |
| 45 | + return res; |
| 46 | + } |
| 47 | + }) |
| 48 | + .thenCombine(world, (h, w) -> h + w) |
| 49 | + .handle((res, exception) -> { //handle exception for world completable future |
| 50 | + log("res is: " + res); |
| 51 | + if (exception != null) |
| 52 | + { |
| 53 | + log("Exception after world is: " + exception.getMessage()); |
| 54 | + return ""; |
| 55 | + } |
| 56 | + else |
| 57 | + { |
| 58 | + return res; |
| 59 | + } |
| 60 | + }) |
| 61 | + .thenCombine(hiCompletableFuture, (previous, current) -> previous + |
| 62 | + current) //if there is exception in world() method the result of this code will be " Hi CompletableFuture!" |
| 63 | + .thenApply(String::toUpperCase) //the result of this code will be "HELLO WORLD! HI COMPLETABLEFUTURE!" |
| 64 | + .join(); |
| 65 | + timeTaken(); |
| 66 | + return result; |
| 67 | + } |
| 68 | + |
| 69 | + public String helloWorld_3_async_calls_exceptionally() |
| 70 | + { |
| 71 | + startTimer(); |
| 72 | + CompletableFuture<String> hello = CompletableFuture.supplyAsync(() -> helloWorldService.hello()); |
| 73 | + CompletableFuture<String> world = CompletableFuture.supplyAsync(() -> helloWorldService.world()); |
| 74 | + CompletableFuture<String> hiCompletableFuture = CompletableFuture.supplyAsync(() -> { |
| 75 | + delay(1000); |
| 76 | + return " Hi CompletableFuture!"; |
| 77 | + }); |
| 78 | + String result = hello |
| 79 | + .exceptionally( |
| 80 | + exception -> { //handle exception for hello completable future here if there is a exception from hello() method since we have returned the empty string for hello cf so the result would be="" |
| 81 | + log("Exception is: " + exception.getMessage()); |
| 82 | + return ""; |
| 83 | + }) |
| 84 | + .thenCombine(world, (h, w) -> h + w) |
| 85 | + .exceptionally(exception -> { //handle exception for world completable future |
| 86 | + log("Exception after world is: " + exception.getMessage()); |
| 87 | + return ""; |
| 88 | + }) |
| 89 | + .thenCombine(hiCompletableFuture, (previous, current) -> previous + |
| 90 | + current) //if there is exception in world() method the result of this code will be " Hi CompletableFuture!" |
| 91 | + .thenApply(String::toUpperCase) //the result of this code will be "HELLO WORLD! HI COMPLETABLEFUTURE!" |
| 92 | + .join(); |
| 93 | + timeTaken(); |
| 94 | + return result; |
| 95 | + } |
| 96 | + |
| 97 | + public String helloWorld_3_async_calls_whenComplete() |
| 98 | + { |
| 99 | + startTimer(); |
| 100 | + CompletableFuture<String> hello = CompletableFuture.supplyAsync(() -> helloWorldService.hello()); |
| 101 | + CompletableFuture<String> world = CompletableFuture.supplyAsync(() -> helloWorldService.world()); |
| 102 | + CompletableFuture<String> hiCompletableFuture = CompletableFuture.supplyAsync(() -> { |
| 103 | + delay(1000); |
| 104 | + return " Hi CompletableFuture!"; |
| 105 | + }); |
| 106 | + String result = hello |
| 107 | + .whenComplete( |
| 108 | + (res, exception) -> { //handle exception for hello completable future here if there is a exception from hello() method since we have returned the empty string for hello cf so the result would be="" |
| 109 | + log("res is: " + res); |
| 110 | + if (exception != null) |
| 111 | + { |
| 112 | + log("Exception is: " + exception.getMessage()); |
| 113 | + } |
| 114 | + }) |
| 115 | + .thenCombine(world, (h, w) -> h + w) |
| 116 | + .whenComplete((res, exception) -> { //handle exception for world completable future |
| 117 | + log("res is: " + res); |
| 118 | + if (exception != null) |
| 119 | + { |
| 120 | + log("Exception after world is: " + exception.getMessage()); |
| 121 | + } |
| 122 | + }) |
| 123 | + .exceptionally( |
| 124 | + exception -> { |
| 125 | + log("Exception after thenCombine is: " + exception.getMessage()); |
| 126 | + return ""; |
| 127 | + }) |
| 128 | + .thenCombine(hiCompletableFuture, (previous, current) -> previous + |
| 129 | + current) //if there is exception in world() method the result of this code will be " Hi CompletableFuture!" |
| 130 | + .thenApply(String::toUpperCase) //the result of this code will be "HELLO WORLD! HI COMPLETABLEFUTURE!" |
| 131 | + .join(); |
| 132 | + timeTaken(); |
| 133 | + return result; |
| 134 | + } |
| 135 | +} |
0 commit comments