Skip to content

Commit

Permalink
CompilationManager becames Closeable
Browse files Browse the repository at this point in the history
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 d2bcae6
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ private com.barbarysoftware.watchservice.WatchEvent<File> asWatchEventOfFile(
@Override
public void close() throws IOException {
watcher.close();
coalescor.close();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -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() {
Expand Down Expand Up @@ -98,6 +99,7 @@ public Predicate<Path> classpathResourceFilter() {
private final Map<Path, SourceHash> hashes = new HashMap<>();

private ExecutorService watcherExecutor;
private final List<Closeable> closeableWatchers = new ArrayList<>();

private volatile boolean compiling;

Expand All @@ -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);
Expand Down Expand Up @@ -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();
Expand All @@ -264,7 +266,7 @@ public int coalescePeriod() {
public boolean recurse() {
return true;
}
});
}));
} else {
logger.info("source root {} does not exist - IGNORED", sourceRoot);
}
Expand All @@ -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();
}
}
}

Expand Down Expand Up @@ -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();
}
}
5 changes: 3 additions & 2 deletions restx-common/src/main/java/restx/common/MoreFiles.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -50,4 +52,9 @@ public void run() {
}
}
}

@Override
public void close() throws IOException {
executor.shutdownNow();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.