From 9abed472e99fbab9bc36a73f2b3e2784d5839e35 Mon Sep 17 00:00:00 2001 From: Gary Tierney Date: Sat, 3 Oct 2015 23:20:05 +0100 Subject: [PATCH] Handle errors thrown by SynchronousDecoder tasks Check for errors thrown execution of any SynchronousDecoder tasks, and rethrow any ExecutionExceptions as SynchronousDecoderExceptions. --- .../game/fs/decoder/SynchronousDecoder.java | 26 +++++++++++++------ .../decoder/SynchronousDecoderException.java | 16 ++++++++++++ 2 files changed, 34 insertions(+), 8 deletions(-) create mode 100644 game/src/main/org/apollo/game/fs/decoder/SynchronousDecoderException.java diff --git a/game/src/main/org/apollo/game/fs/decoder/SynchronousDecoder.java b/game/src/main/org/apollo/game/fs/decoder/SynchronousDecoder.java index acc88f53..acc0dd86 100644 --- a/game/src/main/org/apollo/game/fs/decoder/SynchronousDecoder.java +++ b/game/src/main/org/apollo/game/fs/decoder/SynchronousDecoder.java @@ -4,9 +4,9 @@ import org.apollo.util.ThreadUtil; import java.util.Arrays; import java.util.List; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.TimeUnit; +import java.util.concurrent.*; + +import static java.util.stream.Collectors.toList; /** * A composite decoder that executes each child in parallel. @@ -44,11 +44,21 @@ public final class SynchronousDecoder { * Starts this SynchronousDecoder. * * @throws InterruptedException If a decoder is still running after {@link #TIMEOUT} ms. + * @throws SynchronousDecoderException If a decoder failed to complete successfully. */ - public void block() throws InterruptedException { - runnables.forEach(executor::submit); - executor.shutdown(); - executor.awaitTermination(TIMEOUT, TimeUnit.MILLISECONDS); - } + public void block() throws InterruptedException, SynchronousDecoderException { + List futureList = runnables.stream() + .map(executor::submit) + .collect(toList()); + executor.awaitTermination(TIMEOUT, TimeUnit.MILLISECONDS); + + for (Future future : futureList) { + try { + future.get(); + } catch (ExecutionException e) { + throw new SynchronousDecoderException("Unable to run all decoder tasks", e.getCause()); + } + } + } } \ No newline at end of file diff --git a/game/src/main/org/apollo/game/fs/decoder/SynchronousDecoderException.java b/game/src/main/org/apollo/game/fs/decoder/SynchronousDecoderException.java new file mode 100644 index 00000000..96a63264 --- /dev/null +++ b/game/src/main/org/apollo/game/fs/decoder/SynchronousDecoderException.java @@ -0,0 +1,16 @@ +package org.apollo.game.fs.decoder; + +/** + * An exception thrown when a {@link SynchronousDecoder} fails to complete successfully. + * + * @author garyttierney + */ +public class SynchronousDecoderException extends Exception { + public SynchronousDecoderException(String message) { + super(message); + } + + public SynchronousDecoderException(String message, Throwable cause) { + super(message, cause); + } +}