简体   繁体   中英

How to safely reload server side javascript functions on the fly?

I am experimenting with Nashorn and calling js functions from Java code. I have a WatchService that reloads changed files and reevaluates the ScriptEngine

engine.eval(Files.newBufferedReader(Paths.get(scriptFile), StandardCharsets.UTF_8));

It seems to work fairly well, however, there have been a couple times where I saved the javascript file with unsuspecting typos inside and the subsequent attempt to invoke function

invoker.invokeFunction("printServiceReport", tesla);

causes a javax.script.ScriptException to be thrown thereby killing my running code. Looking for a workable solution to reload logic in real-time without negatively disrupting my application.

Wrap your eval of each code file in a try catch as well as your invokes.

If you need to, you can run through all your functions on reload to see if errors occur - and then use the old version instead if errors are found (if detecting errors at runtime is unacceptable).

We do something similar to that when we reload our serverside scripts.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM