Skip to content

Commit d2bcae6

Browse files
committed
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.
1 parent e6cde56 commit d2bcae6

File tree

5 files changed

+33
-7
lines changed

5 files changed

+33
-7
lines changed

restx-barbarywatch/src/main/java/restx/common/watch/BarbaryWatchService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ private com.barbarysoftware.watchservice.WatchEvent<File> asWatchEventOfFile(
189189
@Override
190190
public void close() throws IOException {
191191
watcher.close();
192+
coalescor.close();
192193
}
193194
}
194195

restx-classloader/src/main/java/restx/classloader/CompilationManager.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.HashMap;
2727
import java.util.HashSet;
2828
import java.util.Iterator;
29+
import java.util.List;
2930
import java.util.Locale;
3031
import java.util.Map;
3132
import java.util.concurrent.*;
@@ -47,7 +48,7 @@
4748
*
4849
* It also trigger events whenever a compilation ends.
4950
*/
50-
public class CompilationManager {
51+
public class CompilationManager implements Closeable {
5152
private static final Runnable NO_OP = new Runnable() {
5253
@Override
5354
public void run() {
@@ -98,6 +99,7 @@ public Predicate<Path> classpathResourceFilter() {
9899
private final Map<Path, SourceHash> hashes = new HashMap<>();
99100

100101
private ExecutorService watcherExecutor;
102+
private final List<Closeable> closeableWatchers = new ArrayList<>();
101103

102104
private volatile boolean compiling;
103105

@@ -111,7 +113,7 @@ public CompilationManager(EventBus eventBus, Iterable<Path> sourceRoots, Path de
111113
this(eventBus, sourceRoots, destination, DEFAULT_SETTINGS);
112114
}
113115

114-
public CompilationManager(EventBus eventBus, Iterable<Path> sourceRoots, Path destination, CompilationSettings settings) {
116+
public CompilationManager(EventBus eventBus, final Iterable<Path> sourceRoots, Path destination, CompilationSettings settings) {
115117
this.eventBus = checkNotNull(eventBus);
116118
this.sourceRoots = checkNotNull(sourceRoots);
117119
this.destination = checkNotNull(destination);
@@ -254,7 +256,7 @@ public void startAutoCompile() {
254256
for (Path sourceRoot : sourceRoots) {
255257
if (sourceRoot.toFile().exists()) {
256258
watched.add(sourceRoot);
257-
MoreFiles.watch(sourceRoot, eventBus, watcherExecutor, new WatcherSettings() {
259+
closeableWatchers.add(MoreFiles.watch(sourceRoot, eventBus, watcherExecutor, new WatcherSettings() {
258260
@Override
259261
public int coalescePeriod() {
260262
return settings.autoCompileCoalescePeriod();
@@ -264,7 +266,7 @@ public int coalescePeriod() {
264266
public boolean recurse() {
265267
return true;
266268
}
267-
});
269+
}));
268270
} else {
269271
logger.info("source root {} does not exist - IGNORED", sourceRoot);
270272
}
@@ -281,6 +283,14 @@ public void stopAutoCompile() {
281283
watcherExecutor.shutdownNow();
282284
watcherExecutor = null;
283285
}
286+
if (closeableWatchers.size() > 0) {
287+
for (Closeable closeableWatcher : closeableWatchers) {
288+
try {
289+
closeableWatcher.close();
290+
} catch (IOException ignored) { }
291+
}
292+
closeableWatchers.clear();
293+
}
284294
}
285295
}
286296

@@ -702,4 +712,10 @@ private SourceHash parse(String str) {
702712
Long.parseLong(parts.next())
703713
);
704714
}
715+
716+
@Override
717+
public void close() throws IOException {
718+
stopAutoCompile(); // in case ato-compile was started, stop it
719+
compileExecutor.shutdownNow();
720+
}
705721
}

restx-common/src/main/java/restx/common/MoreFiles.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import restx.common.watch.WatcherServiceLoader;
77
import restx.common.watch.WatcherSettings;
88

9+
import java.io.Closeable;
910
import java.io.File;
1011
import java.io.FileInputStream;
1112
import java.io.IOException;
@@ -69,9 +70,9 @@ public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOEx
6970
}
7071
}
7172

72-
public static void watch(Path dir, EventBus eventBus,
73+
public static Closeable watch(Path dir, EventBus eventBus,
7374
ExecutorService executor, WatcherSettings watcherSettings) {
74-
WatcherServiceLoader.getWatcherService().watch(eventBus, executor, dir, watcherSettings);
75+
return WatcherServiceLoader.getWatcherService().watch(eventBus, executor, dir, watcherSettings);
7576
}
7677

7778
public static void copyDir(final Path sourceDir, final Path targetDir) throws IOException {

restx-common/src/main/java/restx/common/watch/EventCoalescor.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.google.common.eventbus.EventBus;
44

5+
import java.io.Closeable;
6+
import java.io.IOException;
57
import java.util.LinkedHashSet;
68
import java.util.Set;
79
import java.util.concurrent.Executors;
@@ -20,7 +22,7 @@
2022
* Other similar events occuring within the period are simply not discarded.
2123
* </p>
2224
*/
23-
public class EventCoalescor {
25+
public class EventCoalescor implements Closeable {
2426
private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
2527
private final EventBus eventBus;
2628
private final long coalescePeriod;
@@ -50,4 +52,9 @@ public void run() {
5052
}
5153
}
5254
}
55+
56+
@Override
57+
public void close() throws IOException {
58+
executor.shutdownNow();
59+
}
5360
}

restx-common/src/main/java/restx/common/watch/StdWatcherService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ void processEvents() {
171171
@Override
172172
public void close() throws IOException {
173173
watcher.close();
174+
coalescor.close();
174175
}
175176
}
176177
}

0 commit comments

Comments
 (0)