问题出现原因:候补
解决方案(待优化):
public class HotSwapWatcher extends Thread {
// 略...
protected void doRun() throws IOException {
// 略...
while (running) {
// 略...
List<WatchEvent<?>> watchEvents = watchKey.pollEvents();
for(WatchEvent<?> event : watchEvents) {
String fileName = event.context().toString();
if (fileName.endsWith(".class")) {
if (undertowServer.isStarted()) {
restartUndertow(3);
resetWatchKey();
while((watchKey = watcher.poll()) != null) {
// System.out.println("---> poll() ");
watchKey.pollEvents();
resetWatchKey();
}
break ;
}
}
}
// 略...
}
private void restartUndertow(int tryTimes) {
do {
try {
if (undertowServer.isStarted()) {
undertowServer.restart();
} else {
undertowServer.start();
}
} catch (ClassFormatError e) {
undertowServer.stop();
} catch (RuntimeException e) {
if (Arrays.stream(e.getSuppressed()).anyMatch(ClassNotFoundException.class::isInstance)) {
undertowServer.stop();
} else {
throw e;
}
} finally {
if (!undertowServer.isStarted()) {
try {
Thread.sleep(66);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
} while (!undertowServer.isStarted() && --tryTimes > 0);
}
// 略...
}