0

For a Java application, is there a better way to support configuration files based on a custom DSL (such as relying on Groovy or Kotlin) than using JSR223 ScriptEngine's eval or compile methods?

Loading script's content as String and then using it in a code like that is working but maybe there are better and more efficient ways of doing it.

String type = "groovy"; // or "kts"
String script = "...";  // code of DSL script
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByExtension(type);
Compilable compiler = (Compilable) engine;
CompiledScript code = compiler.compile(script);
Object result = code.eval();
scrutari
  • 1,378
  • 2
  • 17
  • 33

1 Answers1

1

Actually as JMeter, you should save your compiled script in cache and use it:

CompiledScript compiledScript = compiledScriptsCache.get(newCacheKey);

Same for ScriptEngine

You can share a ScriptEngine and CompiledScript objects across threads. They are threadsafe. Actually, you should share them, as a single engine instance is a holder for a class cache and for JavaScript objects' hidden classes, so by having only one you cut down on repeated compilation.

Ori Marko
  • 56,308
  • 23
  • 131
  • 233