All Downloads are FREE. Search and download functionalities are using the official Maven repository.

rpc.turbo.util.concurrent.FutureUtils Maven / Gradle / Ivy

There is a newer version: 0.0.9
Show newest version
package rpc.turbo.util.concurrent;

import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;

public class FutureUtils {

	public static  CompletableFuture withFailover(CompletableFuture future,
			Supplier> failover) {

		CompletableFuture futureWithFailover = future.newIncompleteFuture();

		future.whenComplete((result, throwable) -> {
			if (throwable != null) {
				failover.get().whenComplete((r, t) -> {
					if (t != null) {
						futureWithFailover.completeExceptionally(t);
					} else {
						futureWithFailover.complete(r);
					}
				});
			} else {
				futureWithFailover.complete(result);
			}
		});

		return futureWithFailover;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy