Monday, 29 December 2014

DCEVM + Hotswap Agent + Maven Multi Module

Background

Programming is fun. But the time spent on restarting tomcat can be unproductive and sometimes break the momentum. A colleague recommended this hotswapping to me by using tools like JRebel and Spring Loaded. It is a great idea so I give it a try. At that point of time, JRebel is expensive and Spring Loaded cannot handle class hierarchy change. Here comes the third option: DCEVM + Hotswap Agent.

This post is served as a documentation on the simple setup that works with Apache Maven multi-module project (webapps). Complete and updated guides can be found at Hotswap Agent official site e.g. https://github.com/HotswapProjects/HotswapAgent.

Date

The following setup guide is applicable as of 30 December 2014 SGT.

Setup

1. Install DCEVM 


Visit https://github.com/dcevm/dcevm and download the installer for your JDK. Run the executable jar by running "java -jar installer-light-jdk7u71.2.jar" and you should see something like this :


I choose to install it as altjvm. The rest of this guide will assume this option is chosen.

2. Download Hotswap Agent


Visit https://github.com/HotswapProjects/HotswapAgent to download hotswap agent. Save it somewhere e.g. C:/hotswap-agent.jar.

3. Configure Hotswap Agent in your Maven project


Assuming you have the following project structure:

parent
   |----- module-A
   |----- module-B
   |----- module-C

where module-C is your webapp and depends on module-A and module-B.

Add hotswap-agent.properties in module-C/src/main/resources with the following properties:

# Enable hotswap so that changes in module-C will be automatically reloaded
autoHotswap=true
# Monitor dependencies, assuming using standard Maven output folder
extraClasspath=module-A/target/classes, module-B/target/classes

4. Start Tomcat with Hotswap Agent 


First, export

MAVEN_OPTS="-XXaltjvm=dcevm -javaagent:C:/hotswap-agent.jar -XX:+UnlockDiagnosticVMOptions -XX:+EnableInvokeDynamic"

then at parent folder, run "mvn -pl module-C tomcat7:run".

Now, after changing your code, run "mvn compile" in another terminal will reload the changes.

Notes


  1. The "-XX:+UnlockDiagnosticVMOptions -XX:+EnableInvokeDynamic" are used for static fields, without them static variable will becomes null after reloading. This could be JVM-specific configuration, official guide does not include them. 
  2. Integration with IDE such as IntelliJ and Netbeans also can be found at Hotswap Agent official site. 


1 comment:

  1. Hi, I've checked the -XX:+UnlockDiagnosticVMOptions -XX:+EnableInvokeDynamic on jvm7u75 but the static fields are left null after hotswap. May be I've missed something.

    ReplyDelete