Details
-
Bug
-
Status: Closed
-
Major
-
Resolution: Fixed
-
1.7.3
-
None
Description
If you run the following java code and alter the executed script while running, it is unpredictable when GroovyScriptEngine recompiles the script. Sometimes it works, sometimes not.
GroovyScriptEngineTest.java
public class GroovyScriptEngineTest { public static void main(String[] args) throws Exception { GroovyScriptEngine groovyScriptEngine = new GroovyScriptEngine("/home/peddn/groovy"); groovyScriptEngine.getConfig().setMinimumRecompilationInterval(0); while (true) { Class<?> groovyClass = groovyScriptEngine.loadScriptByName("TestPrint.groovy"); GroovyObject groovyObject = (GroovyObject) groovyClass.newInstance(); Object[] groovyArgs = {}; groovyObject.invokeMethod("testPrint", groovyArgs); //check for recompile every half second Thread.sleep(500L); } } }
class TestPrint { public void testPrint() { System.out.println("hello World"); } }
here some suggestions from a discussion at the mailing list:
I fact I think its kind of bug in GroovyScriptEngine here is code
snippet:
protected boolean isSourceNewer(ScriptCacheEntry entry) throws ResourceException { if (entry == null) return true; long time = System.currentTimeMillis(); ScriptCacheEntry newEntry = new ScriptCacheEntry(depEntry.scriptClass, time, depEntry.dependencies); scriptCache.put(scriptName, newEntry); }
I think more right solution is using aomthing like this:
protected boolean isSourceNewer(ScriptCacheEntry entry) throws ResourceException { if (entry == null) return true; long time = long time = <get entry file lastModified timestamp> ScriptCacheEntry newEntry = new ScriptCacheEntry(depEntry.scriptClass, time, depEntry.dependencies); scriptCache.put(scriptName, newEntry); }