From d5eb65bdc20314e620293c2b07145452d9b2a4e8 Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Mon, 17 Jun 2019 21:58:44 +0100 Subject: [PATCH] do not print stacktrace on clean shutdown --- .../muwire/core/files/DirectoryWatcher.groovy | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/core/src/main/groovy/com/muwire/core/files/DirectoryWatcher.groovy b/core/src/main/groovy/com/muwire/core/files/DirectoryWatcher.groovy index ca97b10b..51081fce 100644 --- a/core/src/main/groovy/com/muwire/core/files/DirectoryWatcher.groovy +++ b/core/src/main/groovy/com/muwire/core/files/DirectoryWatcher.groovy @@ -19,6 +19,7 @@ class DirectoryWatcher { private final EventBus eventBus private final Thread watcherThread private WatchService watchService + private volatile boolean shutdown DirectoryWatcher(EventBus eventBus) { this.eventBus = eventBus @@ -32,6 +33,7 @@ class DirectoryWatcher { } void stop() { + shutdown = true watcherThread.interrupt() watchService.close() } @@ -45,15 +47,20 @@ class DirectoryWatcher { StandardWatchEventKinds.ENTRY_DELETE) } - + private void watch() { - while(true) { - WatchKey key = watchService.take() - key.pollEvents().each { - if (it.kind() == StandardWatchEventKinds.ENTRY_MODIFY) - processModified(key.watchable(), it.context()) + try { + while(!shutdown) { + WatchKey key = watchService.take() + key.pollEvents().each { + if (it.kind() == StandardWatchEventKinds.ENTRY_MODIFY) + processModified(key.watchable(), it.context()) + } + key.reset() } - key.reset() + } catch (InterruptedException e) { + if (!shutdown) + throw e } }