-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathExceptionHandlingUsecase.java
72 lines (56 loc) · 2.48 KB
/
ExceptionHandlingUsecase.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.javatechie.async;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
public class ExceptionHandlingUsecase {
public static void main(String[] args) throws ExecutionException, InterruptedException {
// CompletableFuture.supplyAsync(() -> {
// // Code which might throw an exception
// gracefullyShutDown("");
// return "Some result";
// }).thenApply(result -> {
// return "processed result";
// }).thenApply(result -> {
// return "result after further processing";
// }).thenAccept(result -> {
// // do something with the final result
// });
// update employee DB
CompletableFuture<String> employeeDataFuture = CompletableFuture
.supplyAsync(() -> {
gracefullyShutDown("Employee");
return "Employee information update in DB";
});
// .exceptionally(ex -> {
// System.out.println("unable to update employee information in DB");
// return "500 Internal Server Error";
// });
CompletableFuture<String> employeeDocumentFuture = CompletableFuture
.supplyAsync(() -> {
//gracefullyShutDown("S3");
return "Employee document update in S3";
});
// .exceptionally(ex -> {
// System.out.println("unable to update employee document in s3");
// return "500 Internal Server Error";
// });
//flow 3
//flow 4
CompletableFuture<String> combineFuture = employeeDataFuture
.thenCombine(employeeDocumentFuture, (result1, result2) -> {
return result1 + "\n" + result2;
})
//Global Exception Handling
.handle((res, ex) -> {
if (ex != null) {
System.out.println("An error occurred during processing employee data " + ex.getMessage());
return "Operation Failed ! ";
}
return res;
});
System.out.println(combineFuture.get());
//update employee document to S3
}
private static void gracefullyShutDown(String apiName) {
throw new RuntimeException(apiName + " service temporarily unavailable. Please try again later.");
}
}