groovy
  1. groovy
  2. GROOVY-4286

GroovyScriptEngine doesn't recompile updateted script

    Details

    • Number of attachments :
      0

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

        Activity

        Guillaume Laforge made changes -
        Field Original Value New Value
        Component/s GroovyScriptEngine [ 14484 ]
        Guillaume Laforge made changes -
        Assignee Guillaume Laforge [ guillaume ]
        Guillaume Laforge made changes -
        Status Open [ 1 ] Resolved [ 5 ]
        Fix Version/s 1.8-beta-1 [ 16013 ]
        Fix Version/s 1.7.4 [ 16563 ]
        Resolution Fixed [ 1 ]
        Paul King made changes -
        Status Resolved [ 5 ] Closed [ 6 ]

          People

          • Assignee:
            Guillaume Laforge
            Reporter:
            Peter Schmid
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: