简体   繁体   English

从Java调用现有JRuby对象的方法

[英]Calling methods of existing JRuby objects from Java

I'm trying to provide a JRuby callback to my Java class, so I can get notified when an event happens in the Java part of my program ( a OrientDB Java Hook ). 我试图为我的Java类提供一个JRuby回调,以便在程序的Java部分( OrientDB Java Hook )中发生事件时得到通知。

The JRuby wiki offers examples demonstrating how to load JRuby files and run them in Java, but in my case, the Ruby codes are already in the same JVM, and I just need to send signals to existing Ruby objects. JRuby Wiki提供了一些示例,这些示例演示了如何加载JRuby文件并在Java中运行它们,但是在我的情况下,Ruby代码已经在同一JVM中,我只需要向现有的Ruby对象发送信号。

Assume I have the ruby class loaded in memory: 假设我在内存中加载了ruby类:

class A
  def self.b
    puts 'ruby called'
  end
end

How should I call Ab from my Java method? 我应该如何从Java方法调用Ab

Turns out I can just use JRuby in this case. 事实证明,在这种情况下,我只能使用JRuby。

For the OrientDB embed example, instead of a Java class, I can pass a ruby class 对于OrientDB嵌入示例,我可以传递ruby类,而不是Java类。

class H < com.orientechnologies.orient.core.hook.ORecordHookAbstract
  def onRecordAfterCreate(r)  
    puts r
  end  
end  

to the API and let Java code call Ruby. API,然后让Java代码调用Ruby。

Relevant gist 相关要点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM