Empty JRuby runtime from JRuby

Hello,

is it possible to create empty JRuby environment from JRuby program?

I have experimented with several variants of starting scripting
container,
e.g.

but contants (classes) are surviving among calls :frowning:

Do you have any idea?

Thanks


Marek J.

m: +420 724 255807
e: [email protected]

social: twitter https://twitter.com/#!/marek_jelen |
githubhttps://github.com/marekjelen |
linkedin http://cz.linkedin.com/in/marekjelen |
bloghttp://www.marekjelen.cz/
projects: brug http://www.brug.cz/ |
wildcloudhttps://github.com/wildcloud
| rupy.eu | noderb https://github.com/noderb

Hello,

On Sat, Mar 31, 2012 at 8:38 AM, Marek J. [email protected] wrote:

Hello,

is it possible to create empty JRuby environment from JRuby program?

I have experimented with several variants of starting scripting container,
e.g.

thick/lib/thick/loader.rb at master · marekjelen/thick · GitHub

but contants (classes) are surviving among calls :frowning:

Sounds like a singleton type container was instantiated.
What class do you get by:

container.getProvider.class

Btw, it’s very interesting way of using ScriptingContainer. :slight_smile:

-Yoko

I did something similar a while back – with the purpose of running two
different version of the same Gem in one application.

I tried several different approaches, but here’s basically what I ended
up with:

It’s been so long that I don’t remember much about why I did things the
way I did. But I did not have the problems like you describe.

I did have to watch out for :foo == :foo returning false within the
embedded jruby :slight_smile:

Interesting. Joe, thanks for the gist, it seems to help move a bit
further.
However, I am now getting very weird exception from the application, for
both … your and mine original version.

https://github.com/marekjelen/thick/blob/loader/lib/thick/loader.rb
https://github.com/rkh/rack-protection/blob/master/lib/rack/protection.rb

Any ideas?

Thanks


Marek J.

m: +420 724 255807
e: [email protected]

social: twitter https://twitter.com/#!/marek_jelen |
githubhttps://github.com/marekjelen |
linkedin http://cz.linkedin.com/in/marekjelen |
bloghttp://www.marekjelen.cz/
projects: brug http://www.brug.cz/ |
wildcloudhttps://github.com/wildcloud
| rupy.eu | noderb https://github.com/noderb

That is odd!

I was surprised that you’ve been able to just call “require ‘rack’”
without requiring rubygems or setting up your GEM_PATH inside the new
runtime. One thing I failed to show from my app was a boot.rb, which
was the first thing I ran in the embedded JRuby. It that did all that:

So i tried to tweak my example to reproduce your error, but I could not
(granted, it doesn’t do quite what you intended yet):

How are you running your app? I’d like to mess around with it and see
if I can get anywhere.

I guess I am inheriting some things from outside or some environment is
being shared. It works without anything like boot.rb file for simple
Rack
applications … with Sinatra it does not however.

  1. clone GitHub - marekjelen/thick: [Moved, check WildWeb] Lightweight web server for JRuby, enter, and checkout
    “loader”
    branch
  2. create config.ru from Simple Sinatra · GitHub
  3. run jruby bin/thick

You need to have rack and sinatra installed.

That’s all.

Thanks for help.


Marek J.

m: +420 724 255807
e: [email protected]

social: twitter https://twitter.com/#!/marek_jelen |
githubhttps://github.com/marekjelen |
linkedin http://cz.linkedin.com/in/marekjelen |
bloghttp://www.marekjelen.cz/
projects: brug http://www.brug.cz/ |
wildcloudhttps://github.com/wildcloud
| rupy.eu | noderb https://github.com/noderb

I think you’d better double check ScriptingContainer knows where all
gems are. The error you got means ScriptingContainer doesn’t know the
definition of “::Rack::Protection::FrameOptions” .

If you don’t pack up all in a jar, setting GEM_PATH on the
ScriptingContainer would work. If you will pack in a jar, you should
set each path to gems using setLoadPath() method. This is cumbersome,
but the best to avoid path related problems.

My blog post,

, or page 26 - 31 of my presentation slide,
http://redbridge-at-strangeloop2011.herokuapp.com/slideshow , may help
you to understand.

Since you (Marek) correctly instantiated a singlethreaded
ScriptingContainer, perhaps next to check is loading paths.

As far as I tried on the irb, ScriptingContainer created another
instance of Ruby runtime:

irb(main):002:0> require ‘java’
=> true
irb(main):003:0> JRuby.runtime
=> #Java::OrgJruby::Ruby:0x394a8cd1 <— irb’s
runtime
irb(main):004:0> c =
org.jruby.embed.ScriptingContainer.new(org.jruby.embed.LocalContextScope::SINGLETHREAD)
=> #Java::OrgJrubyEmbed::ScriptingContainer:0x7664e5b2
irb(main):005:0> c.getProvider.getRuntime
=> #Java::OrgJruby::Ruby:0x5f5fc606 <—
another runtime instance
irb(main):006:0> ABC = 10
=> 10
irb(main):007:0> c.run_scriptlet(“ABC=100”)
=> 100
irb(main):008:0> ABC
=> 10
<— irb says ABC is 10
irb(main):009:0> c.run_scriptlet(“ABC”)
=> 100
<— ScriptingContainer says ABC is 100

  • Yoko

As far as I tried on the irb, ScriptingContainer created another
instance of Ruby runtime:

See Ruby runtimes of singlethreaded ScriptingContainer and irb · GitHub . Below is hard to read.

-Yoko

Hello,

thanks for response.

puts container.getProvider.class #
=> Java::OrgJrubyEmbedInternal::SingleThreadLocalContextProvider

I really hope it’s possible to use it this way :wink:


Marek J.

m: +420 724 255807
e: [email protected]

social: twitter https://twitter.com/#!/marek_jelen |
githubhttps://github.com/marekjelen |
linkedin http://cz.linkedin.com/in/marekjelen |
bloghttp://www.marekjelen.cz/
projects: brug http://www.brug.cz/ |
wildcloudhttps://github.com/wildcloud
| rupy.eu | noderb https://github.com/noderb