Embedded JRuby in Java

Hi,

I want to embed JRuby in Java in order to execute ruby code that is
provided by the user (can be cached/compiled), unique scripts don’t
expect
to be in the 10s.

The JSR / BSF options are problematic, due to the performance
implication
of them (basically, the lifecycle they provide is not amazing). What I
am
doing is allowing to provide a script that will run hunders of thousands
of
hits as part of a single search request (sharded and all, but still).

In the other two langs that I have already integrated, using the low
level API made a big difference. Reusing the same Script instance in
rhino /
groovy for a single search request (a request search is single threaded)
was
a huge win. In groovy, I can set the “parameters” for the script on the
script object itself (they mostly don’t change between invocation on
each
hit), and in rhino I can set them on a scope that I create per search
request and reuse per hit. (I don’t want to rub anything in here, just
trying to explain better what I after though it).

I was wondering if its possible in jruby, I can’t seem to work out
how to
do something similar. The point that I am at now is that I will need to
create ScriptingContainers myself, maintain a pool of them, compile and
cache scripts per container, just so I can provide though custom
parameters
per search request. This really complicates things on my end, since I
need
to introduce additional custom lifecycle events to my custom scripting
API,
which are hard to execute (one to obtain a scripting container from a
pool,
and one to return it). Is that really what I need to do? Or is there an
option to set parameters on an “instance” of a compiled script (The
Script
itself in groovy, a scope in rhino)?

Thanks,
shay.banon

View this message in context:
http://old.nabble.com/Embedded-JRuby-in-Java-tp29900719p29900719.html
Sent from the JRuby - User mailing list archive at Nabble.com.


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

On Wed, Oct 6, 2010 at 4:52 PM, Shay B. [email protected] wrote:

hits as part of a single search request (sharded and all, but still).
I was wondering if its possible in jruby, I can’t seem to work out how to
do something similar. The point that I am at now is that I will need to
create ScriptingContainers myself, maintain a pool of them, compile and
cache scripts per container, just so I can provide though custom parameters
per search request. This really complicates things on my end, since I need
to introduce additional custom lifecycle events to my custom scripting API,
which are hard to execute (one to obtain a scripting container from a pool,
and one to return it). Is that really what I need to do? Or is there an
option to set parameters on an “instance” of a compiled script (The Script
itself in groovy, a scope in rhino)?

Did you try to use JRuby’s ScriptingContainer in default setting only
or threadsafe, too? From what you talked about, threadsafe setting of
ScriptingContainer seems to work. If you choose
LocalContextScope.THREADSAFE, ScriptingContainer creates Ruby runtimes
in each thread and isolates global/instance/local vars and Constants
as well as evaluations. If you create or have a thread pool, each
thread will have its own runtime and state.

Is “compile” the same idea of JSR223? Other than that, JRuby has a
feature to compile Ruby class to Java bytecode, jrubyc and
become_java!. These are expected to help performance.

-Yoko

Hi Shay,

On Fri, Oct 15, 2010 at 8:24 PM, Shay B. [email protected] wrote:

Once I do need a different ScriptContainer means that I need to compile
each script per ScriptContainer. It also means that I need to control the
lifecycle of using that ScriptContainer. For example, a search request (in
my case) arrives, I need to “start” processing it (by obtaining a script
container, possibly compiling the script), and then know when I need to stop
it. A single search request can potentially call hundreds of thousands of
times the script. This means a lifecycle control from the search request
down to the inner (architecturally) components that might use scripts. Of
course, not something that can’t be implemented, just a bit more complex.

Are the threads to “instantiate” ScriptingContainer created in your
code? I mean, instantiation of ScriptingContaier “per thread” is
controllable? If so, singlethread model might work in your case. When
you choose singlethread context model and instantiate
ScriptingContainer for each one of threads (as a thread local
variable), each thread will have one ScriptingContainer as well as one
Ruby runtime. When you want to terminate “Ruby runtime,”
ScriptingContainer’s terminate method should work. You should call
ScrriptingContainer#terminate() in ThreadLocal#remove() method before
set null to the ScriptingContainer. This may be what you want to do?

The reason that I posted this question is because I managed to do it with
other langs (rhino, jython, and groovy) in a much simpler manner, since I
don’t need to control the lifecycle “globally” (rhino with scopes, jython
with LocalParams, and Groovy with a Script instance). So, I was wondering if
I am missing something fundamental here with jruby.

I think other languages just have singlethread context model of JRuby
whareas the default of JRuby is singleton model.

-Yoko

Hi,

Yea, that was my question. I wanted to know if what I need to do 

that is
to basically have a different ScriptContainer per thread execution (the
thread pool model is a bit broken to be honest, as for a system that
basically touches different threads but does not create enough load to
really concurrently require all of them will cause more load then
needed, a
better mode IMO is a blocking queue where containers are pooled and is
agnostic to the thread pool size, but adaptive to it).

Once I do need a different ScriptContainer means that I need to
compile
each script per ScriptContainer. It also means that I need to control
the
lifecycle of using that ScriptContainer. For example, a search request
(in
my case) arrives, I need to “start” processing it (by obtaining a script
container, possibly compiling the script), and then know when I need to
stop
it. A single search request can potentially call hundreds of thousands
of
times the script. This means a lifecycle control from the search request
down to the inner (architecturally) components that might use scripts.
Of
course, not something that can’t be implemented, just a bit more
complex.

The reason that I posted this question is because I managed to do it
with
other langs (rhino, jython, and groovy) in a much simpler manner, since
I
don’t need to control the lifecycle “globally” (rhino with scopes,
jython
with LocalParams, and Groovy with a Script instance). So, I was
wondering if
I am missing something fundamental here with jruby.

-shay.banon

yokolet wrote:

The JSR / BSF options are problematic, due to the performance
was
cache scripts per container, just so I can provide though custom
itself in groovy, a scope in rhino)?
feature to compile Ruby class to Java bytecode, jrubyc and
Sent from the JRuby - User mailing list archive at Nabble.com.


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


View this message in context:
http://old.nabble.com/Embedded-JRuby-in-Java-tp29900719p29976448.html
Sent from the JRuby - User mailing list archive at Nabble.com.