2012-02-14 43 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