This project contains the source code to a presentation about Just-in-Time Patching Java Applications.
Please note that it only works with a Java 8 runtime environment so far.
The demo application is containing a defect:
- Start the application's main class
- The application will start on
http://localhost:8080
- Navigate through the dates with the arrow indicators
- Select a Friday 13th in the future by entering a URL, e.g.
http://localhost:8080/day/2020/11/13/10
. - Reload the page some times. You will see an error.
- Reload again, the error is gone and will appear again after reloading some times.
Now you can patch this code multiple times without restarting the application. (To simply fix the bug, just follow the steps in "Replace a Broken Method by a Fixed One". The intermediate sections show how you could use the patching mechanism to find and reproduce the bug).
- Build log-agent with Maven.
- Attach the agent with the agent-attacher.
- You will realize log statements on the WeatherServer console, especially you will see a stacktrace when the error occurs.
- Build recording-agent with Maven.
- Attach the agent with the agent-attacher.
- If you like, detach the LogAgent with the agent-attacher. The log statements from before will no longer occur.
- Unit tests will be recorded on every refresh from now. They capture the invocation of a certain method, i.e. state before, input, output and state after the call.
- The console will report where the tests are written. In your IDE, mark that directory as source path so that you can run the tests.
- When the error occurs again, it will be recorded to a unit test which can be used to reproduce, analyze and fix the error.
- Write your patch code or simply watch how the defect could be fixed:
- Build patch-agent with Maven.
- Again, detach the previous agent(s) if you like.
- Attach the agent with the agent-attacher.
- The defect is fixed, without restarting the program. Every time the error would have occurred, you can see a log statement instead.
All of these actions could be easily applied to other programs including servlet containers or Spring Boot applications. Applications using frameworks that depend on byte code instrumentation (as Spring or Hibernate) are patchable, too. Yet, writing a correct patch can be quite hard since the byte code already generated by these frameworks is often not transparently accessible.
Please provide feedback - especially if something does not work as you expect it.