Plug and Play JRuby


#1

Plug and Play JRuby:
http://recipes.mentaframework.org/posts/list/61.page

I have recently worked with JRuby to make the Mentawai support for the
Ruby
language. I wanted to call any Ruby code from my web application as well
as
be able to have actions fully implemented in Ruby. I also wanted to have
a
special directory where any Ruby file would be reloaded when modified.
JRuby
was up for the task and I have decided to offer my solution as a
standalone
library so that other people can easily leverage all the power of JRuby
in a
simple plug and play Java class.

The features of my JRubyInterpreter class are:

  • Simple API to call ruby methods on any Ruby object from Java
  • JRubyWrapper for making ruby method calls even easier
  • Auto-reload any ruby file from the loadpath
  • Load any ruby file from the classpath (even from jars) with the
    loadFromClasspath method
  • Get a singleton instance of the JRubyInterpreter anywhere in your code
    with the JRubyInterpreter.getInstance() method
  • Support all JRuby environment variables and provide defaults for them.
    Ex:
    JRUBY_OPTS for -rubygems and -Ku , JRUBY_SHELL, JRUBY_SCRIPT, JRUBY_LIB
    and
    JRUBY_HOME.
  • Windows and Linux support

Code speaks better than 1000 words, so here are an example:
http://recipes.mentaframework.org/posts/list/61.page

Please feel free to reply with comments, suggestions and improvements.

-Sergio Oliveira Jr.


#2

On Thu, Dec 4, 2008 at 7:22 PM, Sergio Oliveira
removed_email_address@domain.invalid wrote:

The features of my JRubyInterpreter class are:

  • Get a singleton instance of the JRubyInterpreter anywhere in your code
    with the JRubyInterpreter.getInstance() method

I needed exactly this to provide a Rails instance to my EJB code - I
ended up with something much less elaborate, just access to a
singleton instance and an eval() method on that. Something like this
library would be ideal for when I need better access to JRuby.

One thing I needed to be careful of was instantiating only one JRuby
in a threaded environment. I might be mistaken but this code seems to
require that the caller of getInstance() take care of any
synchronisation requirements? Perhaps a thread-safe singleton
initialiser is something that could be added?

Chris.


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

#3

Sergio Oliveira wrote:

Code speaks better than 1000 words, so here are an
example: http://recipes.mentaframework.org/posts/list/61.page

Please feel free to reply with comments, suggestions and improvements.

It looks very nice! Have you seen the JavaEmbedUtils class we have in
JRuby proper? Perhaps there’s a way we can combine your work and the
work we’ve done there into a really excellent entry point we can ship
with JRuby?

  • Charlie

To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

#4

I am using JavaEmbedUtils internally. JavaEmbedUtils does a great job. I
think the advantage of using JRubyInterpreter would be to simplify a
bunch
of common JRuby tasks in the same class and provide an idiot-proof API.
It
is just a higher level of abstraction.
Plus the auto-reload, method chaining, load ruby file from classpath
(even
jars), environment variables setup, etc. are very nice to have for
starters
that are jumping to JRuby.

Feel free to add this class to JRuby build. It will be a pleasure for me
to
have some of my stuff added to JRuby. :slight_smile: I can write something in the
Wiki
for starters using this class as an entry point if you want me to.

And of course we can improve the API with methods to create class
methods,
instance methods, convert stuff from Ruby to Java, etc. And again we can
use
JavaEmbedUtils internally for that…

-Sergio Oliveira Jr.

On Thu, Dec 4, 2008 at 5:36 PM, Charles Oliver N. <


#5

I might be mistaken but this code seems to require that the caller of
getInstance() take care of any synchronisation requirements?
Does not hurt to synchronize the getInstance method, I agree.

Just one innocent question: Is JRuby runtime (org.jruby.JRuby)
thread-safe
or not? The JRubyInterpreter class is using a single instance for
everything
concurrently, so I am assuming the JRuby is thread-safe, but I may be
mistaken.

-Sergio


#6

Sergio Oliveira wrote:

Does not hurt to synchronize the getInstance method, I agree.

Just one innocent question: Is JRuby runtime (org.jruby.JRuby)
thread-safe or not? The JRubyInterpreter class is using a single
instance for everything concurrently, so I am assuming the JRuby is
thread-safe, but I may be mistaken.

It should be. Any new threads entering a runtime will get their own
ThreadContext, which contains all the state for a given thread executing
code. Individual Ruby data structures may not be completely safe (though
we’ve tried to do a good job on that), but execution should be fine.

  • Charlie

To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

#7

I can confirm Jruby being thread-safe enough to throw a few hundred
glassfish threads at it at a time without a problem. Not the most
scientific test, of course, but the core logic seems thread-safe enough.

I’ve had problems with thread-safe requires and startups (ie creating
multiple org.jruby.JRuby objects at the same time), though that seems to
be fixed in 1.1.5.

Sergio Oliveira wrote:

-Sergio

in a threaded environment. I might be mistaken but this code seems to
http://xircles.codehaus.org/manage_email


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email