Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
public class CompleteOnTimeoutDemo {
public static void main(String[] args) {
CompletableFuture orTimeout = CompletableFuture.supplyAsync(() -> {
try {
return getUsers();
} catch (Exception ex) {
return null;
.completeOnTimeout(getUsersFallback(), 1, TimeUnit.SECONDS);
private static List getUsers() throws Exception {
// In this case fallback method will be invoked, if this value is lesser than 1000, i.e 1 second, it will invoke the service only.
List users = Arrays.asList("Deepak", "Ayush", "Nitesh", "Santosh", "Simran");
return users;
private static List getUsersFallback() {
return Arrays.asList("Fallback List");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment