简体   繁体   中英

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 ).

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.

Assume I have the ruby class loaded in memory:

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

How should I call Ab from my Java method?

Turns out I can just use JRuby in this case.

For the OrientDB embed example, instead of a Java class, I can pass a ruby class

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

to the API and let Java code call Ruby.

Relevant gist

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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