openxava / documentation / Hot code reloading

Table of contents

Hot code reloading
Supported changes
Java included in OpenXava Studio 7 R4
IntelliJ
Visual Studio Code
Performance
Extra classpaths
XavaPro
Starting from OpenXava 7.5, you can enjoy hot code reloading while developing your application. This means you can modify any part of the code, save it, go to the browser and see the updated application. Everything happens instantly, without needing to build, run Maven install, restart the application, or even reload the page in the browser.

Supported changes

These are the changes you can make to your application without restarting it. Most of them take effect without even reloading the browser page:

It's not necessary to start in debug mode for changes to be recognized, but you do need to start with the Java from OpenXava Studio 7 R4.

Java included in OpenXava Studio 7 R4

For code reloading to work, it's necessary to run the application with the Java included in OpenXava Studio 7 R4. This Java is a JDK 11 based on DCEVM that integrates HotswapAgent, and is specially tuned to work well with OpenXava's hot reloading.
This JDK is found in the OpenXava Studio 7 R4 distribution, in openxava-studio-7-r4/studio/jre.
If you run OpenXava Studio on a clean machine without Java and in a new workspace, this Java should be used by default. However, if you already have other JDKs on your computer or have a workspace where you've registered other JDKs, your application might not be using the JDK with HotswapAgent and therefore class reloading won't work.
You'll know if you're running the correct JDK if you see these lines in the log when starting your application:
HOTSWAP AGENT: 13:40:23.126 INFO (org.hotswap.agent.HotswapAgent) - Loading Hotswap agent {1.4.1} - unlimited runtime class redefinition.
Starting HotswapAgent '/home/theuser/openxava-studio-7-r4/studio/jre/lib/hotswap/hotswap-agent.jar'
If not, in OpenXava Studio, go to Window > Preferences > Java > Installed JREs and verify that you have a JRE pointing to openxava-studio-7-r4/studio/jre, as shown here:
openxava-studio-7r4-jre.png
If it's not there, add it yourself. Also, remove any other Java 11 you have registered. Try running your application again.
If it still doesn't run Java with HotswapAgent, you might have Java 1.8 recognized by OpenXava Studio and your project might be configured to work with Java 1.8, so it chooses Java 1.8 instead of Java 11 (the one from OpenXava Studio). To fix this, go to your project's pom.xml and change:
<properties>
    ...  
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>
To:
<properties>
    ...  
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
</properties>
Then right-click on your project and choose Maven > Update Project... This should make it work.
If you want to continue compiling with Java 1.8 and therefore don't want to change the pom.xml, that's not a problem. Go to Run Configurations:
eclipse-run-configurations.png
Find your application and in the JRE folder, make sure your application runs with the OpenXava Studio JDK:
run-configuration-jre.png
If not, change it - you might need to use the Alternate JRE option.

IntelliJ

You can also enjoy hot code reloading using IntelliJ. The trick is to run the application with the JDK included in OpenXava Studio 7 R4. You'll need to download OpenXava Studio, even if it's just to use its JDK.

Register the JDK included in OpenXava Studio 7 R4 in your IntelliJ. Go to File > Project Structure. There in the SDKs section add the JDK found in openxava-studio-7-r4/studio/jre:

intellij-openxava-studio-jdk-definition.png

Then in the Project section, choose this JDK as the SDK for the project:

intellij-openxava-studio-jdk-for-project.png

Keep in mind that IntelliJ does not compile the code automatically when the application is running, so after changing the code you have to press the Build button, the one with the little hammer, for it to compile and be able to see the updated changes.

Visual Studio Code

To use hot code reloading with Visual Studio Code you need to run the application with the JDK included in OpenXava Studio 7 R4, so you'll need to download OpenXava Studio, even if it's just to use its JDK.
For Visual Studio Code to recognize your JDK add this to your user's settings.json:

"java.configuration.runtimes": [
    {
        "name": "JavaSE-11",
        "path": "/home/youruser/openxava-studio-7-r4/studio/jre",
        "default": true
    }
]

Visual Studio Code compiles the code automatically as you edit it, just like OpenXava Studio, so you just have to touch the code and go to the browser to see the application changed immediately.

Performance

The hot reloading mechanism is designed for optimal performance, loading just the necessary metadata only when needed or restarting the Hibernate session only when the persistent part of an entity is modified. Additionally, in production where you use a different JDK than in development, the reloading mechanism is disabled.

Extra classpaths

Sometimes you are developing a library or a project that is a dependency of the current project, and you would like to see the results instantly when you change the library code too. For this case, you can start your application indicating a list of extra classpaths. Edit your launcher class and write something like this:

public static void main(String[] args) throws Exception {
    AppServer.run("yourapp", "../yourlib/target/classes"); 
}

Now when something changes in ../yourlib/target/classes it will also be reloaded automatically. This allows you to develop your library testing it in a final application, with the same agility as with any other application.

Indicating extra classpaths when starting the application is available since version 7.5.

XavaPro

In XavaPro 7.5, there are significant improvements regarding module availability during development. When using Hot code reloading:

This streamlines the development process, as you no longer need to manually add new modules to the "user" role to make them available. However, in production environments (where Hotswap Agent is not used), the behavior remains unchanged for security reasons: new modules must be explicitly assigned to a role to be available to users.