Make JShell shutdown more robust
authorMathieu Baudier <mbaudier@argeo.org>
Fri, 15 Dec 2023 06:48:47 +0000 (07:48 +0100)
committerMathieu Baudier <mbaudier@argeo.org>
Fri, 15 Dec 2023 06:48:47 +0000 (07:48 +0100)
org.argeo.cms.jshell/src/org/argeo/cms/jshell/CmsJShell.java

index 49283f420ec856f486e34f0026f25a23f6cea50e..f08a2a57e0035991072abd457d5c04b53234f304 100644 (file)
@@ -1,6 +1,7 @@
 package org.argeo.cms.jshell;
 
 import java.io.IOException;
+import java.nio.file.ClosedWatchServiceException;
 import java.nio.file.DirectoryStream;
 import java.nio.file.FileSystems;
 import java.nio.file.Files;
@@ -39,6 +40,8 @@ public class CmsJShell {
        private Path jtermBase;
        private Path jtermLinkedDir;
 
+       private WatchService watchService;
+
        public void start() throws Exception {
                // TODO better define application id, make it configurable
                String applicationID;
@@ -83,7 +86,7 @@ public class CmsJShell {
 
                new Thread(() -> {
                        try {
-                               WatchService watchService = FileSystems.getDefault().newWatchService();
+                               watchService = FileSystems.getDefault().newWatchService();
 
                                jshBase.register(watchService, StandardWatchEventKinds.ENTRY_CREATE,
                                                StandardWatchEventKinds.ENTRY_DELETE);
@@ -152,8 +155,11 @@ public class CmsJShell {
                                        }
                                        key.reset();
                                }
+                       } catch (ClosedWatchServiceException e) {
+                               if (log.isTraceEnabled())
+                                       log.trace("JShell file watch service was closed");
                        } catch (IOException | InterruptedException e) {
-                               e.printStackTrace();
+                               log.error("Unexpected exception in JShell file watch service", e);
                        }
                }, "JShell local sessions watcher").start();
        }
@@ -175,6 +181,12 @@ public class CmsJShell {
        }
 
        public void stop() {
+               if (watchService != null)
+                       try {
+                               watchService.close();
+                       } catch (IOException e) {
+                               log.error("Cannot close JShell watch service", e);
+                       }
                try {
                        Files.delete(jshLinkedDir);
                } catch (IOException e) {