Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-4286

GroovyScriptEngine doesn't recompile updateted script

    XMLWordPrintableJSON

Details

    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); 
          }
      

      Attachments

        Activity

          People

            guillaume Guillaume Sauthier
            peddn Peter Schmid
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: