Hot replacement of your JBoss – WildFly classes using Fakereplace

In this tutorial we will learn how to replace your application’s Java classes running in WildFly / JBoss EAP without re-deployment using a tool named Fakereplace.

Replacing your applications while developing them is a need for every developer. Some commerical solutions like JRebel have enhanced this process by making possible it possible for Java developers to instantly see any code change made to an app without redeploying. JRebel lets you see code changes instantly, versioning classes and resources individually and updating one at a time instead of as a lump application redeploy. When developers make a change to any class or resource in their IDE, the change is immediately reflected in the deployed application, skipping the build and redeploy phases.

We have discussed about JRebel in this tutorial: Integrate JRebel with JBoss

A free opensource solution named Fakereplace is also available and its integration with WildFly or JBoss AS is quite simple.

First of all, how Fakereplace can do this magic ? In a nutshell Fakereplace works by registering a java agent at JVM startup. A java agent is a class the implements java.lang.instrument.ClassFileTransformer. This class can do two things:

  • replace method bodies of existing classes (but nothing else, it is essentially the same as hotswap).
  • modify classes before they get loaded into the java virtual machine

Getting started with Fakereplace in less than 5 minutes

Grab the Maven project of Fakereplace at: https://github.com/fakereplace/fakereplace .Once that you have built it, you will have a file named fakereplace.jar in the folder: dist/target

You will need to place this JAR file as follow in your JAVA_OPTS of the application server as a javaagent:

JAVA_OPTS="$JAVA_OPTS -javaagent:/path/to/project/fakereplace/dist/target/fakereplace.jar=server"

Now start the application server as usual. You will see, at the top of the logs, a message informing you that the tool has started its Socket Server:

Fakereplace listening on port 6555

Great. The Github page of the project details several options for passing modified class files to the Agent. The simplest one is do add the Maven plugin to the pom.xml of your projects you want to deploy:

<plugin>
	<groupId>org.fakereplace</groupId>
	<artifactId>fakereplace-maven-plugin</artifactId>
	<version>1.0.0.Alpha5</version>
	<executions>
	  <execution>
	    <phase>package</phase>
	    <goals>
	      <goal>fakereplace</goal>
	    </goals>
	  </execution>
	</executions>
</plugin>

For example, we will add this plugin to one of our projects available on Github: https://github.com/fmarchioni/mastertheboss/tree/master/javaee/javaee7example

Now let’s change one of the files, maybe just a cosmetic change! Once done, push the changes to the fakereplace server:

$ mvn package

As a result, you will see in the logs that class changes have been captured:

Replace Bytecode in WildFly classes

We can check our application has been updated with no deploy!

Replace Bytecode in WildFly classes

Found the article helpful? if so please follow us on Socials