I have been exploring methods of dynamically running groovy scripts in the java application.
I've gone through methods like: 1. Groovy Shell
binding.setVariable("x", 5);
String script = "y = x * x"
GroovyShell gs = new GroovyShell();
Script script = gs.parse(script);
script.run();
- Using GroovyClassLoader.
There are two java applications that I have. One of them is a backend for the GUI that stores data while the executes it. There's no common cache between the two. In order to avoid the overhead of compilation everytime, I am considering to compile the application first in the application B, and then cache it (so it avoids memory leak as well). Also, I tried storing the class files generated by compiling the script initially and then storing it in the database in form of bytes but haven't had any success with it till now.
However, I am not sure if this would be an optimization or if the Groovy Shell/ Groovy Class Loader already caches generated class files. What should be the best way to go about it?
Also, the scripts are not anticipated to be simple, so which mechanism would be better one here Groovy Shell or the Groovy Class Loader? (Also, exploring GrooyScriptEngine, but it seems it won't be needed for simple scripts).