Skip to content

Commit fe8a2fb

Browse files
committed
jooby run: improve restart processs
- unload clases when compilation is required - property changes only restart the application - update maven/gradle - ref #3018
1 parent 2334c8e commit fe8a2fb

File tree

6 files changed

+34
-19
lines changed

6 files changed

+34
-19
lines changed

modules/jooby-gradle-plugin/src/main/java/io/jooby/gradle/RunTask.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public void run() throws Throwable {
109109
compiler.run(new ResultHandler<Void>() {
110110
@Override public void onComplete(Void result) {
111111
getLogger().debug("Restarting application on file change: " + path);
112-
joobyRun.restart();
112+
joobyRun.restart(path);
113113
}
114114

115115
@Override public void onFailure(GradleConnectionException failure) {
@@ -118,7 +118,7 @@ public void run() throws Throwable {
118118
});
119119
} else if (config.isRestartExtension(path)) {
120120
getLogger().debug("Restarting application on file change: " + path);
121-
joobyRun.restart();
121+
joobyRun.restart(path);
122122
} else {
123123
getLogger().debug("Ignoring file change: " + path);
124124
}

modules/jooby-maven-plugin/src/main/java/io/jooby/maven/RunMojo.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@ protected void doExecute(List<MavenProject> projects, String mainClass) throws T
9090
getLog().debug("Compilation error found: " + path);
9191
} else {
9292
getLog().debug("Restarting application on file change: " + path);
93-
joobyRun.restart();
93+
joobyRun.restart(path);
9494
}
9595
} else if (options.isRestartExtension(path)) {
9696
getLog().debug("Restarting application on file change: " + path);
97-
joobyRun.restart();
97+
joobyRun.restart(path);
9898
} else {
9999
getLog().debug("Ignoring file change: " + path);
100100
}

modules/jooby-run/src/main/java/io/jooby/run/JoobyModuleFinder.java renamed to modules/jooby-run/src/main/java/io/jooby/internal/run/JoobyModuleFinder.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
44
* Copyright 2014 Edgar Espina
55
*/
6-
package io.jooby.run;
6+
package io.jooby.internal.run;
77

88
import static java.util.Collections.emptySet;
99
import static java.util.Collections.singleton;
@@ -22,13 +22,15 @@
2222
import org.jboss.modules.ModuleLoader;
2323
import org.jboss.modules.ModuleSpec;
2424

25-
class JoobyModuleFinder implements ModuleFinder {
25+
import io.jooby.run.JoobyRun;
26+
27+
public class JoobyModuleFinder implements ModuleFinder {
2628
private static final String JARS = "jars";
2729
private final Set<Path> resources;
2830
private final Set<Path> jars;
2931
private final String name;
3032

31-
JoobyModuleFinder(String name, Set<Path> resources, Set<Path> jars) {
33+
public JoobyModuleFinder(String name, Set<Path> resources, Set<Path> jars) {
3234
this.name = name;
3335
this.resources = new LinkedHashSet<>(resources.size() + 1);
3436
this.resources.addAll(resources);

modules/jooby-run/src/main/java/io/jooby/run/JoobyModuleLoader.java renamed to modules/jooby-run/src/main/java/io/jooby/internal/run/JoobyModuleLoader.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
44
* Copyright 2014 Edgar Espina
55
*/
6-
package io.jooby.run;
6+
package io.jooby.internal.run;
77

88
import org.jboss.modules.Module;
99
import org.jboss.modules.ModuleFinder;
1010
import org.jboss.modules.ModuleLoader;
1111

12-
class JoobyModuleLoader extends ModuleLoader {
12+
public class JoobyModuleLoader extends ModuleLoader {
1313

14-
JoobyModuleLoader(ModuleFinder finder) {
14+
public JoobyModuleLoader(ModuleFinder finder) {
1515
super(finder);
1616
}
1717

modules/jooby-run/src/main/java/io/jooby/run/ModuleSpecHelper.java renamed to modules/jooby-run/src/main/java/io/jooby/internal/run/ModuleSpecHelper.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
44
* Copyright 2014 Edgar Espina
55
*/
6-
package io.jooby.run;
6+
package io.jooby.internal.run;
77

88
import static org.jboss.modules.ResourceLoaderSpec.createResourceLoaderSpec;
99
import static org.jboss.modules.ResourceLoaders.createJarResourceLoader;
@@ -22,6 +22,8 @@
2222
import org.jboss.modules.ResourceLoaderSpec;
2323
import org.jboss.modules.filter.PathFilters;
2424

25+
import io.jooby.run.JoobyRun;
26+
2527
final class ModuleSpecHelper {
2628

2729
private ModuleSpecHelper() {}

modules/jooby-run/src/main/java/io/jooby/run/JoobyRun.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import org.slf4j.Logger;
3333
import org.slf4j.LoggerFactory;
3434

35+
import io.jooby.internal.run.JoobyModuleFinder;
36+
import io.jooby.internal.run.JoobyModuleLoader;
3537
import io.methvin.watcher.DirectoryChangeEvent;
3638
import io.methvin.watcher.DirectoryWatcher;
3739

@@ -47,7 +49,7 @@
4749
*/
4850
public class JoobyRun {
4951

50-
private record Event(long time) {}
52+
private record Event(Path path, long time) {}
5153

5254
private static class AppModule {
5355
private final Logger logger;
@@ -161,11 +163,18 @@ public boolean isStarting() {
161163
return s > CLOSED && s < RUNNING;
162164
}
163165

164-
public void restart() {
166+
public void restart(boolean unload) {
165167
if (state.compareAndSet(RUNNING, RESTART)) {
168+
// Shutdown
166169
closeServer();
167-
unloadModule();
170+
if (unload) {
171+
// unload only when a class has changed
172+
unloadModule();
173+
}
174+
// Start
168175
start();
176+
// Run gc
177+
System.gc();
169178
} else {
170179
debugState("Already restarting.");
171180
}
@@ -239,12 +248,12 @@ private void debugState(String message) {
239248
default:
240249
throw new IllegalStateException("BUG");
241250
}
242-
logger.debug(message + " state: {}", name);
251+
logger.debug("{} state: {}", message, name);
243252
}
244253
}
245254
}
246255

247-
static final String SERVER_REF = "io.jooby.run.ServerRef";
256+
public static final String SERVER_REF = "io.jooby.run.ServerRef";
248257

249258
static final String SERVER_REF_STOP = "stop";
250259

@@ -374,8 +383,8 @@ public void start() throws Throwable {
374383
}
375384

376385
/** Restart the application. */
377-
public void restart() {
378-
queue.offer(new Event(clock.millis()));
386+
public void restart(Path path) {
387+
queue.offer(new Event(path, clock.millis()));
379388
}
380389

381390
private synchronized void actualRestart() {
@@ -387,12 +396,14 @@ private synchronized void actualRestart() {
387396
if (e == null) {
388397
return; // queue was empty
389398
}
399+
var unload = false;
390400
for (; e != null && (t - e.time) > waitTimeBeforeRestartMillis; e = queue.peek()) {
401+
unload = unload || options.isCompileExtension(e.path);
391402
queue.poll();
392403
}
393404
// e will be null if the queue is empty which means all events were old enough
394405
if (e == null) {
395-
module.restart();
406+
module.restart(unload);
396407
}
397408
}
398409

0 commit comments

Comments
 (0)