2012-02-14 22 views
1

我有一个混合的Web应用程序,它在同一个Tomcat中运行Java WAR文件和JRuby WAR文件。Java到JRuby到Resque

我们决定使用(JRuby)Resque作为我们的工作队列。入队工作的调用看起来是这样的:

Resque.enqueue(FooWorker, 111) 

其中FooWorker是定义和使用JRuby的侧(与包含在JRuby的WAR文件)工人类,它是由JRuby的Resque rake任务调用当它从队列中处理作业时。

我想给Java代码赋予Resque队列任务的能力,以便由JRuby FooWorker类处理。

我看了一下Tommy Cheng的代码https://github.com/tc/call-jruby-from-java-example。 Java文件

//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 

我怀疑我的代码是这样: Java文件

//ResqueInterfaceExample.java 
interface ResqueInterfaceExample{ 
    int resque_enqueue_foojob(int a); 
} 

ruby​​文件

#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 

我的FooWorker是一个类,它位于rails应用程序的分解的war文件目录中,并且该文件是app/workers/foo_worker.rb

我需要做些什么来确保jruby编译器可以同时访问FooWorker和Resque JRuby类能够正确地编译代码?

回答

5

我不确定Tomcat,但我知道Jetty(另一个servlet容器),你可以将jruby代码编译成一个jar文件,并将它放在容器的lib目录中。

或检查出该项目https://github.com/gresrun/jesque

“Jesque是Resque在Java中的实现。这是完全的互操作性与Ruby和Node.js的(咖啡Resque)的实现。”

它可以让你从java本地排队工作到resque。我没有用过它,但看起来很有希望。

+1

优秀!感谢您指出jesque – 2013-06-14 23:26:39