RedBridge (JRuby Embed) will not die!

Help! Consider the following naughty script:

require ‘thread’
t = Thread.new do
while (true)
puts “Still alive”
sleep 10
end
end
t.join

Obviously it will not die! Now I will run it in RedBridge (via the
variable ‘scriptlet’), consider the following irb output:

irb(main):001:0> require ‘java’
=> true

irb(main):002:0> scriptlet = “require ‘thread’;t = Thread.new do while
(true);puts “Still alive”;sleep 10 end end; t.join”
=> “require ‘thread’;t = Thread.new do while (true);puts “Still
alive”;sleep 10 end end; t.join”

irb(main):003:0> java_import ‘org.jruby.embed.ScriptingContainer’
=> Java::OrgJrubyEmbed::ScriptingContainer

irb(main):004:0> container = ScriptingContainer.new
=> #Java::OrgJrubyEmbed::ScriptingContainer:0x6cc2a4

irb(main):005:0> require ‘thread’
=> true

irb(main):006:0> Thread.new do container.runScriptlet(scriptlet) end
=> #<Thread:0x17d7c7f run>
irb(main):007:0> Still alive

irb(main):008:0* Still alive
Still alive
Still alive

irb(main):009:0* container.getProvider.getRuntime.tearDown(true)
=> nil

irb(main):010:0> Still alive
Still alive
Still alive
Still alive
Still alive
Still alive

Obviously I want a mechanism to make my container just call it quits and
‘container.getProvider.getRuntime.tearDown(true)’ is not getting it
done.

How do I do it?

Thanks,

Cris

This line seems to work nicely in my app:

container.getProvider.getRuntime.getThreadService.getMainThread.kill

In my irb example above it kill the whole irb, but in my app it just
kill the member of the thread pool that I want to go away!

I think you can do it this way:

When starting the new thread:

  1. make sure your script returns the new thread instance
  2. when calling runScriptlet, save the return value in a variable.

When you want to stop the thread:

  1. Set a variable in the container to hold onto the thread you just
    saved
  2. container.runScriptlet again with something like “saved_thread.exit”.
  • Keith

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs