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

org.glowroot.central.util.MoreFutures Maven / Gradle / Ivy

There is a newer version: 0.14.0-beta.3
Show newest version
/*
 * Copyright 2016-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.glowroot.central.util;

import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;

import com.datastax.driver.core.exceptions.DriverException;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.MoreExecutors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MoreFutures {

    private static final Logger logger = LoggerFactory.getLogger(MoreFutures.class);

    private MoreFutures() {}

    // not using guava Futures.allAsList().get() because it logs every error
    public static void waitForAll(List> futures) throws Exception {
        Exception exception = null;
        for (Future future : futures) {
            if (exception != null) {
                future.cancel(true);
            } else {
                try {
                    future.get();
                } catch (InterruptedException | ExecutionException e) {
                    logger.debug(e.getMessage(), e);
                    exception = e;
                }
            }
        }
        if (exception == null) {
            return;
        }
        if (exception instanceof ExecutionException) {
            throw unwrapDriverException((ExecutionException) exception);
        }
        throw exception;
    }

    public static  CompletableFuture onFailure(ListenableFuture future,
            Runnable onFailure) {
        CompletableFuture chainedFuture = new CompletableFuture<>();
        Futures.addCallback(future, new FutureCallback() {
            @Override
            public void onSuccess(V result) {
                chainedFuture.complete(result);
            }
            @Override
            public void onFailure(Throwable t) {
                logger.debug(t.getMessage(), t);
                onFailure.run();
                chainedFuture.completeExceptionally(t);
            }
        }, MoreExecutors.directExecutor());
        return chainedFuture;
    }

    public static  CompletableFuture onFailure(CompletableFuture future,
            Runnable onFailure) {
        return future.whenComplete((result, t) -> {
            if (t != null) {
                onFailure.run();
            }
        });
    }

    public static  CompletableFuture toCompletableFuture(ListenableFuture future) {
        CompletableFuture chainedFuture = new CompletableFuture<>();
        Futures.addCallback(future, new FutureCallback() {
            @Override
            public void onSuccess(V result) {
                chainedFuture.complete(result);
            }
            @Override
            public void onFailure(Throwable t) {
                logger.debug(t.getMessage(), t);
                chainedFuture.completeExceptionally(t);
            }
        }, MoreExecutors.directExecutor());
        return chainedFuture;
    }

    public static  CompletableFuture submitAsync(Callable callable,
            ExecutorService executor) {
        CompletableFuture future = new CompletableFuture<>();
        executor.execute(() -> {
            try {
                future.complete(callable.call());
            } catch (Throwable t) {
                future.completeExceptionally(t);
            }
        });
        return future;
    }

    public static Exception unwrapDriverException(ExecutionException e) {
        Throwable cause = e.getCause();
        if (cause instanceof DriverException) {
            // see com.datastax.driver.core.DriverThrowables.propagateCause()
            return ((DriverException) cause).copy();
        } else {
            return e;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy