17.5.1 Automatic Restart

There are various ways to achieve reloading of classes on the JVM, all have their advantages and disadvantages. The following are possible ways to achieve reloading without restarting the JVM:

  • JVM Agents - A JVM agent like JRebel can be used, however these can produce unusual errors, may not support all JDK versions and can result in cached / stale classes.

  • ClassLoader Reloading - Class Loader based reloading is a popular solution used by most JVM frameworks, however it once again can lead to cached / stale classes, memory leaks and weird errors if the incorrect classloader is used at any moment.

  • Debugger HotSwap - The Java debugger supports hotswapping of changes at runtime, but only supports a few use cases.

Given the problems with existing solutions and a lack of a way built into the JVM to reload changes, the safest and best solution to reloading, and the one recommended by the Micronaut team, is to use automatic application restart via third-party tool.

Micronaut’s startup time is fast and automatic restart leads to a clean slate without potential hard to debug problems cropping up or memory leaks.

Maven Restart

To have automatic application restarts with Maven use the Micronaut Maven plugin (including by default when creating new Maven projects) and run the following command:

Using the Micronaut Maven Plugin

  1. $ ./mvnw mn:run

Every time you change a class Micronaut’s plugin will automatically restart the server.

Gradle Restart

For Gradle restart you can create a new application with the file-watch feature already configured using the CLI with the command mn create-app myapp --features file-watch

This essentially introduces the following configuration to application.yml:

File Watch Configuration

  1. micronaut:
  2. io:
  3. watch:
  4. paths: src/main
  5. restart: true

By setting micronaut.io.watch.paths what happens is that Micronaut will fire a FileChangedEvent if any file is changed. Setting micronaut.io.watch.restart tells Micronaut to stop the server if a file is changed.

The micronaut.io.watch.paths setting can be used for more than just automatic restart, you can use this setting to monitor for file changes within your production application.

Once this is in place to have Gradle automatically restart on any change add run the following command:

Using Gradle for Automatic Restart

  1. ./gradlew run --continuous

Every time you make a change to class or resources Gradle will recompile and restart the application.

File Watch and Mac OS X

The native JVM implementation of the WatchService interface for Mac OS X using polling which is slow. To improve file watch performance add the following dependencies to your build if you are using OS X:

Gradle - Configuring Native File Watch on Mac OS X

  1. configurations {
  2. developmentOnly
  3. }
  4. dependencies {
  5. // Automatically added with the 'file-watch' feature in the CLi
  6. developmentOnly "io.micronaut:micronaut-runtime-osx"
  7. ...
  8. developmentOnly "net.java.dev.jna:jna"
  9. developmentOnly "io.methvin:directory-watcher"
  10. }
  11. run.classpath += configurations.developmentOnly
A custom developmentOnly configuration is used to ensure the dependencies don’t go into the production JAR.

Or with Maven:

  1. <dependency>
  2. <groupId>io.methvin</groupId>
  3. <artifactId>directory-watcher</artifactId>
  4. <scope>provided</scope>
  5. </dependency>
  6. <dependency>
  7. <groupId>net.java.dev.jna</groupId>
  8. <artifactId>jna</artifactId>
  9. <scope>provided</scope>
  10. </dependency>
The provided scope is used to ensure the dependencies don’t go into the production JAR.