Hot Reloading Using JRebel For Java Application

Technology

 
JRebel is one of the Java agents, which helps make development much faster, by skipping the packaging and deploying step,s of the application during the development process.  JRebel is one of the plugins for IDE on top of the JDK Hot Swap.
 
JRebel makes a developer's life easier.  If we change the code, the changes are auto-deployed and can be viewed in real-time.  JRebel uses Rebellion Technology to instantly load the developer made changes to Java classes, skipping redeploying the full application.   A developer saves the changes and refreshes the web application, to see those changes.  It is as simple as that.
 
For achieving this, JRebel does not wrap Java Classloaders around the Java classes, but it patches the existing Java class loaders to load the classes through reloadable, using Java instrumentation.  JRebel tracks the changes in resources and updates the same on-the-fly, without overriding the whole resource, so that it would cut the time to see the changes.
 

Why we use JRebel

  1. Real-Time Changes - When we save the Java code and resources, it is loaded in application real-time, with no downtime.
  2. Faster development - No waiting is required for building and redeploying the application.  Save the changes and refresh the page, to see the changes.
  3. Measurable ROI (Return on Investment) - Reduces the unexpected and not-estimated expenses and saves the measurable developer time each day.
  4. Maintain Application State - If we do the traditional way of re-deployment, we need to perform some of the actions to go to the previous state, but with JRebel, the application state is maintained.

Advantages over JDK Hot-swap

 
As JDK embedded hot-swap only supports method body changes.  It does not support adding, changing, or deleting of the methods, inside a class.  JRebel supports more changes compared to Hot-swap.
  1. Adding, Modifying, deleting the methods.
  2. Adding, Modifying, deleting the fields.
  3. Adding, Modifying, deleting the constructors.
  4. Adding, Modifying, deleting the annotation on class level and method level.
  5. Adding, removing Java Classes.
  6. Replacing superclasses.
  7. Modifying Interfaces.
  8. Adding/removing implemented interfaces.
  9. Adding, Removing Enum values
  10. Initializes new instance fields.
      

Installing JRebel in STS

 
Open Eclipse Marketplace from the Help menu.
Hot Reloading Using JRebel For Java Application 
 
Search for JRebel and install the plug-in.
 
Hot Reloading Using JRebel For Java Application 
 
Once we install JRebel, we can see a new Submenu under the Help menu.
 
Hot Reloading Using JRebel For Java Application 
 
Click on Activation and give license information for JRebel.  If we don’t have the license, we can choose a trial version by providing the details.
Click on "Configuration" from JRebel menu and click on the Startup tab.
 
Hot Reloading Using JRebel For Java Application 
 
 
If we want the JRebel initiate, via IDE, we can choose the default one.  Otherwise, we can add Java EE Server, where we want to include JRebel, as a Java agent.
 

Usage of JRebel in Spring Boot application

 
Navigate to https://start.spring.io/ to create a Spring Boot application and import that into IDE where we installed the JRebel plugin.
Right-click on the project to enable JRebel for application, as seen below. 
 
Hot Reloading Using JRebel For Java Application 
 
Once we enable the JRebel for the project, we can get the below pop-up. 
 
Hot Reloading Using JRebel For Java Application 
 
And the rebel.xml file will be generated in the src/main/resources folder which is used by JRebel for hot-deploy.

 

Launching Spring Boot Application using JRebel

 
Launch the application as a Spring Boot application. Now, we will get the below pop-up to use JRebel or execute normally without using JRebel.
 
Hot Reloading Using JRebel For Java Application 
 
If we select JRebel, we will see some log statements related to JRebel.
 
Hot Reloading Using JRebel For Java Application
 
Where we can see the below statement,
2018-09-20 11:36:09 JRebel: Directory 'C:\Sravan\WS\SpringBoot-JRebel\target\classes' will be monitored for changes.
If any of the files reside in a folder, then we can see the changes in real-time.  If we change any of the Java code and click on Save, we can see the changes are propagated to the Server.
Hot Reloading Using JRebel For Java Application 
 
The modified Java classes will be reloaded, and if it is a spring bean, then bean will be reloaded in applicationContext.

 

Conclusion

 
JRebel is one of the best tools for hot reloading of the resources by skipping the packing, deployment steps of the application.  It provides plugins for all major IDE's, and for major application servers.  It will work for almost all the frameworks like spring, Structs, Wicket, GWT, etc.