Jay Godse
Jay Godse

Reputation: 15503

Java to JRuby to Resque

I have a hybrid web application which runs a Java WAR file and a JRuby WAR file in the same Tomcat.

We have decided to use (JRuby) Resque as our job queue. The call to enqueue jobs looks like this:

Resque.enqueue(FooWorker, 111)

where FooWorker is a worker class defined on and used by the JRuby side (and included in the JRuby WAR file), and it is invoked by the JRuby Resque rake task when it processes a job from the queue.

I would like to give the Java code the ability to enqueue tasks on the Resque queue to be processed by the JRuby FooWorker class.

I took a look at Tommy Cheng's code at https://github.com/tc/call-jruby-from-java-example.

//JavaInterfaceExample.java
interface JavaInterfaceExample{
  int add(int a, int b);
}
#JrubyAdderImpl.rb
require 'java'

class JrubyAdderImpl
  include Java::JavaInterfaceExample

  java_signature 'int add(int, int)'
  def add(a, b)
    a+b
  end
end

I suspect that my code would look like:

//ResqueInterfaceExample.java
interface ResqueInterfaceExample{
  int resque_enqueue_foojob(int a);
}
#JrubyResqueImpl.rb
require 'java'
require 'resque'

class JrubyResqueImpl
  include Java::ResqueInterfaceExample

  java_signature 'int resque_enqueue_foojob(int)'
  def resque_enqueue_foojob(a)
    Resque.enqueue(FooWorker, a) 
  end
end

My FooWorker class sits in the exploded war file directory for the Rails app, and the file is app/workers/foo_worker.rb

What do I need to do to ensure that the JRuby compiler has access to both the FooWorker and Resque JRuby classes to compile the code properly?

Upvotes: 2

Views: 621

Answers (1)

tommy chheng
tommy chheng

Reputation: 9218

I'm not sure about Tomcat, but I know with Jetty(another servlet container), you can compile the jruby code into a jar and place it in the container's lib directory.

Or check out this project https://github.com/gresrun/jesque

"Jesque is an implementation of Resque in Java. It is fully-interoperable with the Ruby and Node.js (Coffee-Resque) implementations."

It lets you enqueue jobs natively from java to resque. I haven't used it but it looks promising.

Upvotes: 6

Related Questions