GroovyScriptEngine doesn't recompile updateted script

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

GroovyScriptEngine doesn't recompile updateted script

peddn
Hey Folks,

im involking a method every 100ms. Within this method i execute the following code:


Class<?> groovyClass = groovyScriptEngine.loadScriptByName("TestScript.groovy");

GroovyObject groovyObject = (GroovyObject) groovyClass.newInstance();

Object[] args = {};
		
groovyObject.invokeMethod("testString", args);


Here is the TestScript.groovy


public class TestScript {
	
	public void testString() {
		
		System.out.println("Hello World");
		
	}

}


But when i alter my script (e.g. the string to print) while running, GroovyScriptEngine seems not to recompile my script. The output is the same as before. When i restart the programm, the updated version of TestScript.groovy will get executed. But the documentation speaks of automatic recompilation when a script is altered.

Can someone help me understand, whats going wrong here?

Greetigs,

Peddn
Reply | Threaded
Open this post in threaded view
|

Re: GroovyScriptEngine doesn't recompile updateted script

Sargis
Hi:

public class GroovyScriptEngineTest {

   public static void main(String[] args) throws Exception {
       GroovyScriptEngine groovyScriptEngine =3D new
GroovyScriptEngine("/home/sargis");
       while (true) {
           Class<?> groovyClass =3D
groovyScriptEngine.loadScriptByName("TestScript.groovy");

           GroovyObject groovyObject =3D (GroovyObject)
groovyClass.newInstance();
           Object[] gargs =3D {};

           groovyObject.invokeMethod("testString", gargs);
           Thread.sleep(5000L);
       }
   }

}

I tried this code and it worked normally, reloaded script I mean,
after changing file please be sure that you saved it.

Also try:

GroovyScriptEngine groovyScriptEngine =3D new GroovyScriptEngine("/
home/sargis");
groovyScriptEngine.getConfig().setMinimumRecompilationInterval(0);

seems there is flag to control MinimumRecompilationInterval :-).

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: GroovyScriptEngine doesn't recompile updateted script

Sargis
Sorry code was not formatted well

public class GroovyScriptEngineTest {

    public static void main(String[] args) throws Exception {
        GroovyScriptEngine groovyScriptEngine = new
GroovyScriptEngine("/home/sargis");
 
groovyScriptEngine.getConfig().setMinimumRecompilationInterval(0);
        while (true) {
            Class<?> groovyClass =
groovyScriptEngine.loadScriptByName("TestScript.groovy");

            GroovyObject groovyObject = (GroovyObject)
groovyClass.newInstance();
            Object[] gargs = {};

            groovyObject.invokeMethod("testString", gargs);
            Thread.sleep(5000L);
        }
    }

}

As I understand after first loading you are changing script file too
quickle and because of flag setMinimumRecompilationInterval, Groovy
considering that file is not changed and does not reloading it

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: GroovyScriptEngine doesn't recompile updateted script

peddn
In reply to this post by Sargis
I tested your code and it seems to work fine...

Than i altered the code the following

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[] gargs = {};

			groovyObject.invokeMethod("testString", gargs);
                        //now every half second
			Thread.sleep(500L);
		}
	}
}


Now the reload seems to be unpredictable... Sometime it works, sometimes not...

I am confused ;)

Greetings,

Peddn
Reply | Threaded
Open this post in threaded view
|

Re: GroovyScriptEngine doesn't recompile updateted script

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



On Jun 29, 8:04 pm, peddn <[hidden email]> wrote:

> I tested your code and it seems to work fine...
>
> Than i altered the code the following
>
> 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[] gargs = {};
>
>                         groovyObject.invokeMethod("testString", gargs);
>                         //now every half second
>                         Thread.sleep(500L);
>                 }
>         }
>
> }
>
> Now the reload seems to be unpredictable... Sometime it works, sometimes
> not...
>
> I am confused ;)
>
> Greetings,
>
> Peddn
> --
> View this message in context:http://groovy.329449.n5.nabble.com/GroovyScriptEngine-doesn-t-recompi...
> Sent from the groovy - user mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: GroovyScriptEngine doesn't recompile updateted script

Sargis
Also in

        public Class parseClass(GroovyCodeSource codeSource, boolean
shouldCacheSource) throws CompilationFailedException {
             ...........................................
             long time = System.currentTimeMillis();
             ...........................................
             ScriptCacheEntry cacheEntry = new
ScriptCacheEntry(clazz,time,value);
             scriptCache.put(entryName,cacheEntry);
        }

I think should be

       long time = = <get entry/script file lastModified timestamp>

I am not sure that problem is here, because need to test and look at
code more, which I will try to do later :-).

On Jun 30, 9:47 am, Sargis <[hidden email]> wrote:

> 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);
>     }
>
> On Jun 29, 8:04 pm, peddn <[hidden email]> wrote:
>
>
>
>
>
> > I tested your code and it seems to work fine...
>
> > Than i altered the code the following
>
> > 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[] gargs = {};
>
> >                         groovyObject.invokeMethod("testString", gargs);
> >                         //now every half second
> >                         Thread.sleep(500L);
> >                 }
> >         }
>
> > }
>
> > Now the reload seems to be unpredictable... Sometime it works, sometimes
> > not...
>
> > I am confused ;)
>
> > Greetings,
>
> > Peddn
> > --
> > View this message in context:http://groovy.329449.n5.nabble.com/GroovyScriptEngine-doesn-t-recompi...
> > Sent from the groovy - user mailing list archive at Nabble.com.
>
> > ---------------------------------------------------------------------
> > To unsubscribe from this list, please visit:
>
> >    http://xircles.codehaus.org/manage_email
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: GroovyScriptEngine doesn't recompile updateted script

peddn
I've made another observation:

If you make a new GroovyScriptEngine every loop, the code seems to work...

here my code:
public class GroovyScriptEngineTest {

	public static void main(String[] args) throws Exception {
		
		//groovyScriptEngine.getConfig().setMinimumRecompilationInterval(0);
		
		while (true) {
			
			GroovyScriptEngine groovyScriptEngine = new GroovyScriptEngine("/home/peddn/groovy");
						
			Class<?> groovyClass = groovyScriptEngine.loadScriptByName("TestPrint.groovy");

			GroovyObject groovyObject = (GroovyObject) groovyClass.newInstance();

			Object[] groovyArgs = {};

			groovyObject.invokeMethod("testPrint", groovyArgs);

			Thread.sleep(500L);

		}

	}

}

To use the file lastmodified timestamp is a good idea.
It would make more sense to me than to use System time.

I look foreward to your tests :)


Reply | Threaded
Open this post in threaded view
|

Re: GroovyScriptEngine doesn't recompile updateted script

Sargis
Of course if you move GroovyScriptEngine inside loop it will create
each time new GroovyScriptEngine and reload script. I think there is
really bug if GroovyScriptEngine deisgned to reload groovy script it
should work in predictable way and I think really problem in:  long
time = System.currentTimeMillis(). Probably you can try to submit bug
in Groovy JIRA.

On 30 juin, 19:24, peddn <[hidden email]> wrote:

> I've made another observation:
>
> If you make a new GroovyScriptEngine every loop, the code seems to work...
>
> here my code:
>
> public class GroovyScriptEngineTest {
>
>         public static void main(String[] args) throws Exception {
>
>                 //groovyScriptEngine.getConfig().setMinimumRecompilationInterval(0);
>
>                 while (true) {
>
>                         GroovyScriptEngine groovyScriptEngine = new
> GroovyScriptEngine("/home/peddn/groovy");
>
>                         Class<?> groovyClass =
> groovyScriptEngine.loadScriptByName("TestPrint.groovy");
>
>                         GroovyObject groovyObject = (GroovyObject) groovyClass.newInstance();
>
>                         Object[] groovyArgs = {};
>
>                         groovyObject.invokeMethod("testPrint", groovyArgs);
>
>                         Thread.sleep(500L);
>
>                 }
>
>         }
>
> }
>
> To use the file lastmodified timestamp is a good idea.
> It would make more sense to me than to use System time.
>
> I look foreward to your tests :)
>
> --
> View this message in context:http://groovy.329449.n5.nabble.com/GroovyScriptEngine-doesn-t-recompi...
> Sent from the groovy - user mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email