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

io.proximax.utils.TimeoutUtils Maven / Gradle / Ivy

The newest version!
package io.proximax.utils;

import io.proximax.exceptions.RetrievalTimeoutException;

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

/**
 * The utility class for handling timeouts
 */
public class TimeoutUtils {

    private TimeoutUtils() {}

    /**
     * Timeout retrieval
     * @param supplier the supplier
     * @param timeout timeout length
     * @param timeUnit time unit of timeout
     * @param  the class the supplier provide
     * @return the retrieved type
     */
    public static  T get(Supplier supplier, long timeout, TimeUnit timeUnit) {
        try {
            return CompletableFuture.supplyAsync(supplier).get(timeout, timeUnit);
        } catch (TimeoutException e) {
            throw new RetrievalTimeoutException("Retrieval timed out", e);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy