Poorman's solution to quick prototyping and code reloading

25 October 2015

The problem

Sometimes it can be very handy to be able to quickly prototype code and try a quick implementation of an idea or fix without having to go through the whole cycle of build, deploy and restart the app, what typically happens on any app of significant size is that build and deploy are OK, but then the restart is lengthy since it needs to load the initial state into memory, when this cycle is lengthy, it can be a productivity killer and demotivate. JRebel is a very good product and makes this possible, like a breeze, but when you can’t afford the license fee what do you do ? Well the following is an idea, that could be enough to get you going.

Groovy to the rescue !

Use groovy, integrate groovy for the parts of the code you what to quickly prototype or change as groovy is a super set of java it will allow to have something at the end that will be easily convertible into java if your can’t deploy groovy code in the end result. Integrating groovy is as easy as 3 steps:

Make the groovy embeddeable jar available Use the GroovyScriptEngine to call the groovy code you whant to run Read back the results from the call to the groovy code One of the great advantages of Groovy is the excellent integration with Java and all the popular IDE’s that make even possible to have debug, code completion and even refractory while you benefit of dynamic scripting.

Step 1

To materialize a bit more the previous three steps if you are building your code with Gradle, making the step one will be as simple as adding the groovy-all jar in the dependencies

Step 1

Step 2 & 3

Make the call to the to the groovy code using the GroovyScriptEngine, this is a regular java class that you create an instance and tell it where is the groovy source code to be executed:

Step 2 & 3

The cool thing here is that the Groovy code been called actually implements a pure Java interface, so we can cross language boundaries using a type safe interface, from a "statically typed" to "optionally dynamically typed". Bellow is the Goovy code as you can see it looks a lot like Java, and you can literally copy a Java class to a Groovy class without changing any code and it will run.

The groovy code

The following is the source directory where you can see that there is both Java and Groovy code.

The package structure

The following screenshot is a sample Spring Boot application running with Intellj IDE, this sample demonstrates the idea where the RestController is written in Java and call the Groovy code using the GroovyScriptEngine, and from what you can see in the image the debugger in stopped in a breakpoint in the Groovy code.

Running with the debugger

Conclusion

Now with this setup one can do the quick prototype or fix in the groovy code, test debug and refactor and move the result in the end to java code and break away with lengthy code change cycles!

Example solution code

Example code available at my repo:

Disclaimer

The opinions expressed where are my own, and subject to change, please provide examples here this is not valid and I will review and amend the text. Thank you stay Groovy !


Older posts are available in the archive.