Forum: JRuby Plug and Play JRuby

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Bfcd2ee582d9e83ab4c742509ce1010a?d=identicon&s=25 Sergio Oliveira (Guest)
on 2008-12-04 20:23
(Received via mailing list)
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.
Ede2aa10c6462f1d825143879be59e38?d=identicon&s=25 Charles Oliver Nutter (Guest)
on 2008-12-04 20:37
(Received via mailing list)
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
6fb2fc2840fa0d2f6937a412422346c0?d=identicon&s=25 Chris Andrews (Guest)
on 2008-12-04 20:55
(Received via mailing list)
On Thu, Dec 4, 2008 at 7:22 PM, Sergio Oliveira
<sergio.soujava@gmail.com> 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
Bfcd2ee582d9e83ab4c742509ce1010a?d=identicon&s=25 Sergio Oliveira (Guest)
on 2008-12-04 21:09
(Received via mailing list)
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. :-) 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 Nutter <
Bfcd2ee582d9e83ab4c742509ce1010a?d=identicon&s=25 Sergio Oliveira (Guest)
on 2008-12-04 21:19
(Received via mailing list)
>  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
Ede2aa10c6462f1d825143879be59e38?d=identicon&s=25 Charles Oliver Nutter (Guest)
on 2008-12-04 21:33
(Received via mailing list)
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
08264fc914cfb45adfea6c2f36f70b21?d=identicon&s=25 Jacob Kessler (Guest)
on 2008-12-04 22:53
(Received via mailing list)
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
This topic is locked and can not be replied to.