0

I would like to know if it's possible to share an object between two JRuby instances if they run inside the same JVM?

I already tried to create a singleton java class that got referenced from both JRubies, but it's seems, that each JRuby creates it's own environment and it's own version of that java class.

Does someone know a way to solve this?

4

1 回答 1

0

好的-实际上是两个问题。

  1. 要共享的变量必须以 $ 符号开头(例如 $databases)。
  2. 脚本容器的 LocalContextScope 必须设置为正确的值。

如果 LocalContextScope 设置为“singleton”和“concurrent”,则可能会出现以下情况。

require 'java'

include Java
import 'org.jruby.embed.ScriptingContainer'
import 'org.jruby.embed.LocalContextScope'

$a = "Test"

container = ScriptingContainer.new LocalContextScope::CONCURRENT
container.run_scriptlet "puts $a"
于 2012-07-24T11:41:24.103 回答