Example usage for java.util.concurrent ExecutionException addSuppressed

List of usage examples for java.util.concurrent ExecutionException addSuppressed

Introduction

In this page you can find the example usage for java.util.concurrent ExecutionException addSuppressed.

Prototype

public final synchronized void addSuppressed(Throwable exception) 

Source Link

Document

Appends the specified exception to the exceptions that were suppressed in order to deliver this exception.

Usage

From source file:Main.java

/**
 * Await the completion of tasks interruptibly. The execution
 * exceptions are combined into a single exception, but interrupted
 * exception breaks the wait.//from  w  ww .  ja va  2 s . c om
 * @param tasks the tasks to wait
 * @throws ExecutionException on execution errors
 * @throws InterruptedException when the wait is interrupted
 */
public static void awaitAllInterruptibly(Iterable<? extends Future<?>> tasks)
        throws ExecutionException, InterruptedException {
    ExecutionException ee = null;
    for (Future<?> f : tasks) {
        try {
            f.get();
        } catch (ExecutionException ex) {
            if (ee != null) {
                ee.addSuppressed(ex);
            } else {
                ee = ex;
            }
        }
    }
    if (ee != null) {
        throw ee;
    }
}

From source file:Main.java

/**
 * Await all of the future tasks indefinitely.
 * The exceptions are combined into a single exception
 * (by adding subsequent exceptions as addsuppressed).
 * @param tasks the sequence of tasks//  w  w w .  j av a2s.  c om
 * @throws ExecutionException if the task failed
 * @throws InterruptedException if the wait was interrupted
 */
public static void awaitAll(Iterable<? extends Future<?>> tasks)
        throws ExecutionException, InterruptedException {
    ExecutionException ee = null;
    InterruptedException ie = null;
    for (Future<?> f : tasks) {
        try {
            f.get();
        } catch (ExecutionException ex) {
            if (ee != null) {
                ee.addSuppressed(ex);
            } else {
                ee = ex;
            }
        } catch (InterruptedException ex) {
            if (ie != null) {
                ie.addSuppressed(ex);
            } else {
                ie = ex;
            }
        }
    }
    if (ee != null && ie != null) {
        ee.addSuppressed(ie);
        throw ee;
    } else if (ee != null) {
        throw ee;
    } else if (ie != null) {
        throw ie;
    }
}

From source file:Main.java

public static void invokeAll(final List<Runnable> tasks, final ExecutorService executor)
        throws InterruptedException, ExecutionException {
    ExecutionException saved = null;

    if (executor != null) {
        final List<Future<?>> futures = new ArrayList<>();
        for (final Runnable task : tasks)
            futures.add(executor.submit(task));

        for (final Future<?> future : futures) {
            try {
                future.get();/*from  w  w w  .j av  a2 s  . com*/
            } catch (InterruptedException e) {
                throw e;
            } catch (ExecutionException e) {
                if (saved == null) {
                    saved = e;
                } else {
                    saved.addSuppressed(e);
                }
            }
        }
    } else {
        for (final Runnable task : tasks) {
            try {
                task.run();
            } catch (Exception e) {
                if (saved == null) {
                    saved = new ExecutionException(e);
                } else {
                    saved.addSuppressed(e);
                }
            }
        }
    }

    if (saved != null)
        throw saved;
}

From source file:org.nuxeo.connect.tools.report.client.ReportConnector.java

<A> void connect(Consumer consumer) throws IOException, InterruptedException, ExecutionException {
    ExecutorService executor = Executors.newCachedThreadPool(new ThreadFactory() {

        @Override/*w w  w .ja va 2  s. c o  m*/
        public Thread newThread(Runnable target) {
            Thread thread = new Thread(target, "connect-report");
            thread.setDaemon(true);
            return thread;
        }
    });
    try {
        for (ReportServer server : new Discovery()) {
            try (ServerSocket callback = new ServerSocket(0)) {
                final Future<?> consumed = executor.submit(new Runnable() {

                    @Override
                    public void run() {

                        String name = Thread.currentThread().getName();
                        Thread.currentThread().setName("connect-report-consumer-" + server);
                        try (InputStream source = callback.accept().getInputStream()) {
                            consumer.consume(Json.createParser(source));
                        } catch (IOException | JsonParsingException cause) {
                            throw new AssertionError("Cannot consume connect report", cause);
                        } finally {
                            Thread.currentThread().setName(name);
                        }
                        LogFactory.getLog(ReportConnector.class).info("Consumed " + server);
                    }
                });
                final Future<?> served = executor.submit(new Runnable() {

                    @Override
                    public void run() {
                        String name = Thread.currentThread().getName();
                        Thread.currentThread().setName("connect-report-server-" + server);
                        InetSocketAddress address = (InetSocketAddress) callback.getLocalSocketAddress();
                        try {
                            server.run(address.getHostName(), address.getPort());
                        } catch (IOException cause) {
                            throw new AssertionError("Cannot run connect report", cause);
                        } finally {
                            Thread.currentThread().setName(name);
                        }
                    }

                });
                ExecutionException consumerError = null;
                try {
                    consumed.get();
                } catch (ExecutionException cause) {
                    consumerError = cause;
                }
                try {
                    served.get();
                } catch (ExecutionException cause) {
                    if (consumerError != null) {
                        consumerError.addSuppressed(cause);
                        throw consumerError;
                    }
                    throw cause;
                }
            }
        }
    } finally {
        executor.shutdownNow();
    }
}