Extract method for task waiting and exception handling.
@@ -12,6 +12,7 @@ import java.util.List;
import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
import org.osgi.framework.Bundle;
@@ -86,10 +87,15 @@ public class Deconstructor implements ComponentDeconstructor {
var task = executor.schedule(new DestructComponentTask(destructibleComponents, bundles),
delay.getSeconds(), TimeUnit.SECONDS);
if (mode.equals(Mode.SHUTDOWN)) {
- // Wait for deconstruction to finish
+ waitFor(task, SHUTDOWN_DECONSTRUCT_TIMEOUT);
+ }
+ }
+ }
+
+ private void waitFor(ScheduledFuture<?> task, Duration timeout) {
try {
- log.info("Waiting up to " + SHUTDOWN_DECONSTRUCT_TIMEOUT.toSeconds() + " seconds for all components to deconstruct.");
- task.get(SHUTDOWN_DECONSTRUCT_TIMEOUT.toMillis(), TimeUnit.MILLISECONDS);
+ log.info("Waiting up to " + timeout.toSeconds() + " seconds for all components to deconstruct.");
+ task.get(timeout.toMillis(), TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
log.info("Interrupted while waiting for component deconstruction to finish.");
Thread.currentThread().interrupt();
@@ -99,8 +105,6 @@ public class Deconstructor implements ComponentDeconstructor {
log.warning("Component deconstruction timed out.");
}
}
- }
- }
private static class DestructComponentTask implements Runnable {
