From 0be47060af7c0f984191e400ffd51533453b4da0 Mon Sep 17 00:00:00 2001 From: litongjava Date: Sun, 3 Sep 2023 16:46:59 -1000 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BD=BF=E7=94=A8executorService.scheduleA?= =?UTF-8?q?tFixedRate=20=E6=9B=BF=E4=BB=A3=20=E6=AD=BB=E5=BE=AA=E7=8E=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../undertow/hotswap/HotSwapWatcher.java | 85 +++++++++++-------- 1 file changed, 49 insertions(+), 36 deletions(-) diff --git a/src/main/java/com/jfinal/server/undertow/hotswap/HotSwapWatcher.java b/src/main/java/com/jfinal/server/undertow/hotswap/HotSwapWatcher.java index 587fcab..5eeece8 100644 --- a/src/main/java/com/jfinal/server/undertow/hotswap/HotSwapWatcher.java +++ b/src/main/java/com/jfinal/server/undertow/hotswap/HotSwapWatcher.java @@ -30,6 +30,10 @@ import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + import com.jfinal.server.undertow.UndertowKit; import com.jfinal.server.undertow.UndertowServer; @@ -95,6 +99,7 @@ public class HotSwapWatcher extends Thread { } protected void doRun() throws IOException { + ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(); WatchService watcher = FileSystems.getDefault().newWatchService(); addShutdownHook(watcher); @@ -106,49 +111,57 @@ public class HotSwapWatcher extends Thread { StandardWatchEventKinds.ENTRY_CREATE ); } - - while (running) { - try { - // watchKey = watcher.poll(watchingInterval, TimeUnit.MILLISECONDS); // watcher.take(); 阻塞等待 - // 比较两种方式的灵敏性,或许 take() 方法更好,起码资源占用少,测试 windows 机器上的响应 - watchKey = watcher.take(); - - if (watchKey == null) { - // System.out.println(System.currentTimeMillis() / 1000); - continue ; - } - } catch (Throwable e) { // 控制台 ctrl + c 退出 JVM 时也将抛出异常 - running = false; - if (e instanceof InterruptedException) { // 另一线程调用 hotSwapWatcher.interrupt() 抛此异常 - // while 循环没有通过 while (running && !Thread.currentThread().isInterrupted()) 控制流程,下一行代码可以不需要 - Thread.currentThread().interrupt(); // Restore the interrupted status - } - break ; + + executorService.scheduleAtFixedRate(() -> { + watch(watcher); + }, 0, 500, TimeUnit.MILLISECONDS); + +// while (running) { +// watch(watcher); +// } + } + + private void watch(WatchService watcher) { + try { + // watchKey = watcher.poll(watchingInterval, TimeUnit.MILLISECONDS); // watcher.take(); 阻塞等待 + // 比较两种方式的灵敏性,或许 take() 方法更好,起码资源占用少,测试 windows 机器上的响应 + watchKey = watcher.take(); + + if (watchKey == null) { + // System.out.println(System.currentTimeMillis() / 1000); + //continue ; + return; } - - List> watchEvents = watchKey.pollEvents(); - for(WatchEvent event : watchEvents) { - String fileName = event.context().toString(); - if (fileName.endsWith(".class")) { - if (undertowServer.isStarted()) { - undertowServer.restart(); + } catch (Throwable e) { // 控制台 ctrl + c 退出 JVM 时也将抛出异常 + running = false; + if (e instanceof InterruptedException) { // 另一线程调用 hotSwapWatcher.interrupt() 抛此异常 + // while 循环没有通过 while (running && !Thread.currentThread().isInterrupted()) 控制流程,下一行代码可以不需要 + Thread.currentThread().interrupt(); // Restore the interrupted status + } + return; + } + List> watchEvents = watchKey.pollEvents(); + for (WatchEvent event : watchEvents) { + String fileName = event.context().toString(); + if (fileName.endsWith(".class")) { + if (undertowServer.isStarted()) { + undertowServer.restart(); + resetWatchKey(); + + while ((watchKey = watcher.poll()) != null) { + // System.out.println("---> poll() "); + watchKey.pollEvents(); resetWatchKey(); - - while((watchKey = watcher.poll()) != null) { - // System.out.println("---> poll() "); - watchKey.pollEvents(); - resetWatchKey(); - } - - break ; } + + break; } } - - resetWatchKey(); } + + resetWatchKey(); } - + private void resetWatchKey() { if (watchKey != null) { watchKey.reset(); -- Gitee From 225f5ec78307d73eef1f01cc5c16a94d422ad1cb Mon Sep 17 00:00:00 2001 From: litongjava Date: Sun, 3 Sep 2023 21:25:59 -1000 Subject: [PATCH 2/2] =?UTF-8?q?=E8=A7=84=E8=8C=83=20pom.xml=20=E6=96=87?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 8978c43..616931e 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ https://jfinal.com jfinal undertow - + UTF-8 -- Gitee