What's the best way of accessing a DRb object (e.g. Ruby Queue) from Scala (and Java)?

Posted by Tom Morris on Stack Overflow See other posts from Stack Overflow or by Tom Morris
Published on 2010-06-12T15:48:55Z Indexed on 2010/06/12 15:52 UTC
Read the original article Hit count: 230

I have built a variety of little scripts using Ruby's very simple Queue class, and share the Queue between Ruby and JRuby processes using DRb. It would be nice to be able to access these from Scala (and maybe Java) using JRuby.

I've put together something Scala and the JSR-223 interface to access jruby-complete.jar.

import javax.script._

class DRbQueue(host: String, port: Int) {
  private var engine = DRbQueue.factory.getEngineByName("jruby")
  private var invoker = engine.asInstanceOf[Invocable]
  engine.eval("require \"drb\" ")
  private var queue = engine.eval("DRbObject.new(nil, \"druby://" + host + ":" + port.toString + "\")")

  def isEmpty(): Boolean = invoker.invokeMethod(this.queue, "empty?").asInstanceOf[Boolean]
  def size(): Long = invoker.invokeMethod(this.queue, "length").asInstanceOf[Long]
  def threadsWaiting: Long = invoker.invokeMethod(this.queue, "num_waiting").asInstanceOf[Long]
  def offer(obj: Any) = invoker.invokeMethod(this.queue, "push", obj.asInstanceOf[java.lang.Object])
  def poll(): Any = invoker.invokeMethod(this.queue, "pop")
  def clear(): Unit = { invoker.invokeMethod(this.queue, "clear") }
}
object DRbQueue {
  var factory = new ScriptEngineManager()
}

(It conforms roughly to java.util.Queue interface, but I haven't declared the interface because it doesn't implement the element and peek methods because the Ruby class doesn't offer them.)

The problem with this is the type conversion. JRuby is fine with Scala's Strings - because they are Java strings. But if I give it a Scala Int or Long, or one of the other Scala types (List, Set, RichString, Array, Symbol) or some other custom type.

This seems unnecessarily hacky: surely there has got to be a better way of doing RMI/DRb interop without having to use JSR-223 API. I could either make it so that the offer method serializes the object to, say, a JSON string and takes a structural type of only objects that have a toJson method. I could then write a Ruby wrapper class (or just monkeypatch Queue) to would parse the JSON.

Is there any point in carrying on with trying to access DRb from Java/Scala? Might it just be easier to install a real message queue? (If so, any suggestions for a lightweight JVM-based MQ?)

© Stack Overflow or respective owner

Related posts about java

Related posts about ruby