|
| 1 | +--- |
| 2 | +layout: page |
| 3 | +title: "Hot reloading" |
| 4 | +category: doc |
| 5 | +date: 2017-07-18 22:30:05 |
| 6 | +order: 199 |
| 7 | +--- |
| 8 | + |
| 9 | +#### How to use |
| 10 | +This feature allows you to write code, save the code on disk and reload the web page from browser without restart the application (from IDE). |
| 11 | + |
| 12 | +```java |
| 13 | +@MetaInfServices |
| 14 | +public class PippoApplication extends Application { |
| 15 | + |
| 16 | + @Override |
| 17 | + protected void onInit() { |
| 18 | + GET("/", routeContext -> routeContext.send("Hello")); |
| 19 | + } |
| 20 | + |
| 21 | +} |
| 22 | +``` |
| 23 | +```java |
| 24 | +public class PippoLauncher { |
| 25 | + |
| 26 | + public static void main(String[] args) { |
| 27 | + Pippo pippo = new Pippo(); |
| 28 | + pippo.start(); |
| 29 | + } |
| 30 | + |
| 31 | +} |
| 32 | +``` |
| 33 | + |
| 34 | +So, the code is almost the same with the version without hot reloading. The only one difference is that you must specify |
| 35 | +your **application class name** and **NOT** to create the application instance. |
| 36 | + |
| 37 | +You can supply the application class name using one of the below methods: |
| 38 | + |
| 39 | +- create by hand a `META-INF/services/ro.pippo.core.Application` file with content `mypackage.PippoApplication` (see Java [ServiceLoader](https://docs.oracle.com/javase/8/docs/api/java/util/ServiceLoader.html)) |
| 40 | +- add a `MetaInfServices` annotation on your application class (generate automatically the `META-INF/services/ro.pippo.core.Application` file) |
| 41 | +- use `-Dpippo.applicationClassName` system property |
| 42 | + |
| 43 | +#### How works |
| 44 | +To implement hot reloading, Pippo introduces two new concepts (classes in package `ro.pippo.core.reload`): |
| 45 | + |
| 46 | +- `ReloadWatcher` |
| 47 | +- `ReloadClassLoader` |
| 48 | + |
| 49 | +The `ReloadWatcher` class runs a background task that checks directories for new, modified or removed files. The default directory is `target/classes` (standard from Maven) but you can specify other target classes directory via `-Dpippo.reload.targetClasses` system property. |
| 50 | +When `ReloadWatcher` detects a modification, the method `onEvent(ReloadWatcher.Event event, Path dir, Path path)` method from `Pippo` class is called. |
| 51 | + |
| 52 | +By default, the `onEvent` method does: |
| 53 | + |
| 54 | +- stop server; |
| 55 | +- create a new instance of application using `ReloadClassLoader` |
| 56 | +- set the new application instance in `PippoFilter` |
| 57 | +- start server |
| 58 | + |
| 59 | +You can overwrite this method (onEvent) if you wish more control (not to restart the server, only to ...). |
| 60 | + |
| 61 | +The `ReloadClassLoader` class is a Java `ClassLoader` that loads classes from files. You can specify the classes that will be loaded by this loader using `-Dpippo.reload.rootPackageName` system property (for example I want to reload only classes from web layer, classes with packages that start with "mycompany.web"). |
| 62 | + |
| 63 | +By default, hot reloading mechanism is enabled if you are in `dev` (development) mode and you use the application class name instead of application instance. You can use `-Dpippo.reload.enabled` system property to enable or disable the hot reloading mechanism. |
| 64 | + |
| 65 | +**Note** If you use Idea IntelliJ as IDE, it's a good idea to remap `Ctrl-Shift-F9` (compile file) to `Ctrl-S` (save file) to compile file on save (see [post](https://intellij-support.jetbrains.com/hc/en-us/community/posts/206996845-compile-on-file-save)). |
0 commit comments