Permalink
Browse files

CompilationManager becames Closeable

Executors used by CompilationManager, WatcherService and
EventCoalescor were not closed, so the JVM could not stop
properly.
So from no on, EventCoalescor implements Closeable, so do the
CompilationManager. And they shutdown their executors in their
'close' methods.
  • Loading branch information...
a-peyrard committed Jan 16, 2015
1 parent e6cde56 commit d2bcae682788a825b076a3db4764d6b95396865a
@@ -189,6 +189,7 @@ void processEvents() {
@Override
public void close() throws IOException {
watcher.close();
+ coalescor.close();
}
}
@@ -26,6 +26,7 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
+import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.*;
@@ -47,7 +48,7 @@
*
* It also trigger events whenever a compilation ends.
*/
-public class CompilationManager {
+public class CompilationManager implements Closeable {
private static final Runnable NO_OP = new Runnable() {
@Override
public void run() {
@@ -98,6 +99,7 @@ public int autoCompileCoalescePeriod() {
private final Map<Path, SourceHash> hashes = new HashMap<>();
private ExecutorService watcherExecutor;
+ private final List<Closeable> closeableWatchers = new ArrayList<>();
private volatile boolean compiling;
@@ -111,7 +113,7 @@ public CompilationManager(EventBus eventBus, Iterable<Path> sourceRoots, Path de
this(eventBus, sourceRoots, destination, DEFAULT_SETTINGS);
}
- public CompilationManager(EventBus eventBus, Iterable<Path> sourceRoots, Path destination, CompilationSettings settings) {
+ public CompilationManager(EventBus eventBus, final Iterable<Path> sourceRoots, Path destination, CompilationSettings settings) {
this.eventBus = checkNotNull(eventBus);
this.sourceRoots = checkNotNull(sourceRoots);
this.destination = checkNotNull(destination);
@@ -254,7 +256,7 @@ public void startAutoCompile() {
for (Path sourceRoot : sourceRoots) {
if (sourceRoot.toFile().exists()) {
watched.add(sourceRoot);
- MoreFiles.watch(sourceRoot, eventBus, watcherExecutor, new WatcherSettings() {
+ closeableWatchers.add(MoreFiles.watch(sourceRoot, eventBus, watcherExecutor, new WatcherSettings() {
@Override
public int coalescePeriod() {
return settings.autoCompileCoalescePeriod();
@@ -264,7 +266,7 @@ public int coalescePeriod() {
public boolean recurse() {
return true;
}
- });
+ }));
} else {
logger.info("source root {} does not exist - IGNORED", sourceRoot);
}
@@ -281,6 +283,14 @@ public void stopAutoCompile() {
watcherExecutor.shutdownNow();
watcherExecutor = null;
}
+ if (closeableWatchers.size() > 0) {
+ for (Closeable closeableWatcher : closeableWatchers) {
+ try {
+ closeableWatcher.close();
+ } catch (IOException ignored) { }
+ }
+ closeableWatchers.clear();
+ }
}
}
@@ -702,4 +712,10 @@ private SourceHash parse(String str) {
Long.parseLong(parts.next())
);
}
+
+ @Override
+ public void close() throws IOException {
+ stopAutoCompile(); // in case ato-compile was started, stop it
+ compileExecutor.shutdownNow();
+ }
}
@@ -6,6 +6,7 @@
import restx.common.watch.WatcherServiceLoader;
import restx.common.watch.WatcherSettings;
+import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
@@ -69,9 +70,9 @@ public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOEx
}
}
- public static void watch(Path dir, EventBus eventBus,
+ public static Closeable watch(Path dir, EventBus eventBus,
ExecutorService executor, WatcherSettings watcherSettings) {
- WatcherServiceLoader.getWatcherService().watch(eventBus, executor, dir, watcherSettings);
+ return WatcherServiceLoader.getWatcherService().watch(eventBus, executor, dir, watcherSettings);
}
public static void copyDir(final Path sourceDir, final Path targetDir) throws IOException {
@@ -2,6 +2,8 @@
import com.google.common.eventbus.EventBus;
+import java.io.Closeable;
+import java.io.IOException;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.concurrent.Executors;
@@ -20,7 +22,7 @@
* Other similar events occuring within the period are simply not discarded.
* </p>
*/
-public class EventCoalescor {
+public class EventCoalescor implements Closeable {
private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
private final EventBus eventBus;
private final long coalescePeriod;
@@ -50,4 +52,9 @@ public void run() {
}
}
}
+
+ @Override
+ public void close() throws IOException {
+ executor.shutdownNow();
+ }
}
@@ -171,6 +171,7 @@ void processEvents() {
@Override
public void close() throws IOException {
watcher.close();
+ coalescor.close();
}
}
}

0 comments on commit d2bcae6

Please sign in to comment.