I am trying to get the result from my CompletableFuture
based on different timeouts. The second call to the CompletableFuture would ideally be fire and forget.
Example:
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
try {
TimeUnit.Seconds.sleep(10);
return "success"
} catch (InterruptedException e) {
return "fail";
}
}
public String method(CompletableFuture<String> future) {
try {
return future.get(1, TimeUnit.SECONDS);
} catch (Exception e) {
ExecutorService.execute(() -> {
String result = future.get(10, TimeUnit.SECONDS);
}
}
return "Initial Future timed out"
}
I still would want the Initial Future timed out
string to be returned, just another thread to reprocess in a fire and forget way.
Addionally, is there a cleaner way of doing something like this. Maybe being able to wrap it all inside the initial CompletableFuture
?