Discussion:
[groovy-user] Understanding GroovyScriptEngine bindings
Adam Mc
2015-02-10 01:57:38 UTC
Permalink
Hi,

I am very new to Groovy, and I am trying to understand how default bindings
work for the GroovyScriptEngine.

If I have a single GroovyScriptEngine instance that is called from multiple
threads, and each time I run the the engine, I do so with a fresh Binding
(see following code). Are those Bindings only visible to the script during
that run of the script, or do the bindings stay around for the life of the
engine? Consider the following:

gse = getSharedInstanceOfGrooveScriptEngine() // psudo code here
Binding binding = new Binding();
binding.setVariable("obj", myObj); // this object is unique for
each run, but bound to same string name
gse.run("test.groovy", binding);

Considering the above code will run from multiple threads at the same time
(but with different instance objects put into the bindings), will concurrent
running scripts see only their own unique objects in the bindings that were
set it its unique "run".






--
View this message in context: http://groovy.329449.n5.nabble.com/Understanding-GroovyScriptEngine-bindings-tp5722441.html
Sent from the groovy - user mailing list archive at Nabble.com.

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

http://xircles.codehaus.org/manage_email
Jochen Eddelbüttel
2015-02-11 20:54:25 UTC
Permalink
Adam,

running a script with run will create an instance of the groovy.lang.Script
subclass that the script is compiled into. Each instance should get its own
binding, at least when you're creating one as in your code. The compilation
only happens once, as long as the source file date doesn't change. The
binding is basically a map on the heap. It only needs to be around during
that single execution of the script and is eligible for garbage collection
afterwards. Nothing to worry about here. GSE is really stable and resource
friendly, definitely in the current stable 2.3.9 release that I'm using in
production.

Cheers

Jochen

-----Ursprüngliche Nachricht-----
Von: Adam Mc [mailto:***@cs.miami.edu]
Gesendet: Dienstag, 10. Februar 2015 02:58
An: ***@groovy.codehaus.org
Betreff: [groovy-user] Understanding GroovyScriptEngine bindings

Hi,

I am very new to Groovy, and I am trying to understand how default bindings
work for the GroovyScriptEngine.

If I have a single GroovyScriptEngine instance that is called from multiple
threads, and each time I run the the engine, I do so with a fresh Binding
(see following code). Are those Bindings only visible to the script during
that run of the script, or do the bindings stay around for the life of the
engine? Consider the following:

gse = getSharedInstanceOfGrooveScriptEngine() // psudo code here
Binding binding = new Binding();
binding.setVariable("obj", myObj); // this object is unique for
each run, but bound to same string name
gse.run("test.groovy", binding);

Considering the above code will run from multiple threads at the same time
(but with different instance objects put into the bindings), will concurrent
running scripts see only their own unique objects in the bindings that were
set it its unique "run".






--
View this message in context:
http://groovy.329449.n5.nabble.com/Understanding-GroovyScriptEngine-bindings
-tp5722441.html
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
Adam Mc
2015-02-14 21:55:48 UTC
Permalink
Jochen and group,

Thanks for your reply. From what was communicated, it sounds like the
GroovyScriptEngine is fairly thread safe and a single instance of the engine
can be shared among multiple threads, and that concurrent"runs" of the
engine do not "mess" with each other, or each other's bindings.

If the above does not sound correct to anyone, please let me know. Thanks.

-Adam





--
View this message in context: http://groovy.329449.n5.nabble.com/Understanding-GroovyScriptEngine-bindings-tp5722441p5722612.html
Sent from the groovy - user mailing list archive at Nabble.com.

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

http://xircles.codehaus.org/manage_email

Loading...