What is the best way to access a DRb object (e.g. Ruby Queue) from Scala (and Java)?

I created many small scripts using Ruby with a very simple Queue class, and shared the queue between Ruby and JRuby processes using DRb. It would be nice to have access to them from Scala (and possibly Java) using JRuby.

I put together something Scala and a 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 roughly corresponds to the java.util.Queue interface, but I did not declare the interface because it does not implement the element and peek methods, because the Ruby class does not offer them.)

- . JRuby Scala Strings - Java. Scala Int Long Scala (List, Set, RichString, Array, Symbol) - .

: , RMI/DRb JSR-223 API. , , , JSON, , toJson. Ruby ( queuepatch Queue), JSON.

- DRb Java/ Scala? ? ( , JQM-MQ?)

+3
1

, DRB ruby ​​ Queuing.

-, .

, Twitter, .

, JMS- Java .

DRB-, , , , , ! http://www.java-tips.org/java-se-tips/java.lang/array-based-queue-implementation-in-java.html

-, , , .

+2

Source: https://habr.com/ru/post/1749746/


All Articles