Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Hotswap Agent: Java Runtime Class and Resource Redefinition

DZone's Guide to

Hotswap Agent: Java Runtime Class and Resource Redefinition

Saving and restarting your code after each change can be a nightmare, but Hotswap Agent, a new tool with broad plugin support, can save time.

· Java Zone ·
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

The main purpose of the Hotswap Agent project is to avoid the dreaded change ->restart + wait -> check development lifecycle. Saving and reloading during development should be standard and many other languages (including C#) contain this feature.

We have finally reached version 1.0, so give it a try!

Features

In contrast to standard Java, where the hotswap is limited to in-body code changes, the DCEVM + HotswapAgent allow the following code changes:

  • Add/remove/modify class fields.
  • Add/remove/modify methods. Add/remove/modify method annotations
  • Add/remove/modify classes including anonymous classes. HotswapAgent handless correct anonymous class redefinitions.
  • Add/remove static member of classes. HotswapAgent handles static member initialization.
  • Add/remove enum values
  • Refresh framework and application server settings

The only unsupported operation is hierarchy change (change the superclass or remove an interface).

DCEVM realizes hotswapping on the JVM level. HotwapAgent does the same on the level of Java frameworks and servlet containers. Both projects, when used together, form an excellent combination for daily development — not only in Java but also in another JVM languages.

Easy to Start

Download and install the latest DCEVM Java patch + agent JAR and launch your application server with the options -XXaltjvm=dcevm -javaagent:hotswap-agent.jar to get the basic setup. You can attach agent JAR to the running JVM using the following example code snippet:

import com.sun.tools.attach.VirtualMachine;
import com.sun.tools.attach.VirtualMachineDescriptor;

public class AgentRunner {

    /*
     * This class shows how to attach hotswap-agent.jar to a running JVM process and overload classes using "extraClasspath=" property via Hotswapper plugin.
     *
     * Lets assume that:
     *  args[0] contains pid of running JVM process or a runner class name we want to attach agent to
     *  args[1] contains absolute path to agent.jar
     *  args[2] contains parameters needed to pass to agent
     */
    public static void main(String[] args) {
        if (2 > args.length) {
            System.out.println("Usage: java -cp .:$JAVA_HOME/lib/tools.jar AgentRunner JVM_PID_OR_NAME PATH_TO_JAR [PARAMS]");
        } else try {
            String pid = args[0];

            for (VirtualMachineDescriptor vmd : VirtualMachine.list()) {
                if (vmd.displayName().contains(args[1])) {
                    pid = vmd.id();
                    break;
                }
            }

            final VirtualMachine vm = VirtualMachine.attach(pid);
            vm.loadAgent(args[1], 3 > args.length || null == args[2] ? "" : args[2]);
            vm.detach();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


Optionally, add hotswap-agent.properties to your application to configure plugins and agent behavior.

Plugins

Each application framework (Spring, Hibernate, Logback, etc.) needs a special reloading mechanism to keep up-to-date after class redefinition (e.g. Hibernate configuration reload after a new entity class is introduced). Hotswap agent works as a plugin system and ships preconfigured with all major framework plugins. It is easy to write your custom plugin even as part of your application.

Contribute

This project is very complex due to the number of supported frameworks and various versions. Community contribution is mandatory to keep it alive. You can start by creating a plugin inside your application or by writing an example/integration test. There is always need for documentation improvement. Thank you for any help!

For more information, visit http://hotswapagent.org/.

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat

Topics:
hotswap ,java ,tools

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}