javamavenopen-libertyhot-reloadhotswap

Can maven open-liberty plugin hot-reload single class file without stopping and restarting the whole application?


https://github.com/eugenp/tutorials/tree/master/microservices-modules/open-liberty

I am using the above code to test maven open-liberty plugin.

mvn liberty:dev

can start the app, no problem here.

Then when one jave file gets editted, it gets detected and recompiled, also good.

However, the whole application gets stopped and restarted.

Can open-liberty be configured to only hot-reload one single class file without rebooting the whole app?


Solution

  • You can't update an application class without app restart but you can use debugger HCR

    The reason you can't update an application "on disk" without an app restart isn't because of the Liberty Maven/Gradle plugins, but because of the Liberty runtime itself. The application classloader doesn't allow for a new version of a single class to be swapped into a started application.

    (Though you can update static content like .html files without an app update).

    Use Debugger Hot Code Replace (HCR) to avoid app restart

    In many cases, you can leverage the debugger HCR to replace a class implementation at a "lower level".

    There are limitations and only a subset of all possible changes can be successfully replaced this way, e.g. as mentioned here you can generally do things like replace a method body and generally not do things like change method signatures.

    Use IDE tools

    Though you can certainly use a Java debugger without Liberty-specific IDE tooling, for completeness, let me point you to our "Liberty Tools" IDE tools for the three popular editors:

    There is also an older Eclipse feature, "Liberty Developer Tools". For newer Maven/Gradle apps we generally recommend the newer "Liberty Tools for Eclipse" feature mentioned above, with support for Jakarta EE 9/10 and MicroProfile development. That said, the app update support is a bit nicer in Liberty Developer Tools at this point in time, (as noted briefly below).

    Disable application monitor updates

    If you're using some sort of rapid iterative development environment, like Liberty "dev mode" then with the default Liberty config, the app will update upon every class update. Application update can, depending on the app, take a significant amount of time, which is likely exactly what led you to asking this question in the first place.

    You can disable this behavior by configuring an <applicationMonitor> element in your server.xml:

     <applicationMonitor updateTrigger="mbean" ... /> 
    

    Alternately, you could use "disabled" in place of "mbean" as the attribute value.

    Explanation

    At the moment, in Liberty Tools there is no special assistance for app update, so there's no difference between "mbean" and "disabled". In the older, "Liberty Developer Tools" we have some "smarter" function in this area and so "mbean" would be a better choice.