我有一个混合web应用程序,它在同一Tomcat中运行一个Java WAR文件和一个JRuby WAR文件。
我们决定使用(JRuby)Resque作为我们的作业队列。将作业排入队列的调用如下所示:
Resque.enqueue(FooWorker, 111)
其中FooWorker是一个在JRuby端定义并由其使用的工作类(包含在JRubyWAR文件中),当JRuby Resque rake任务处理队列中的作业时,它会被调用。
我想让Java代码能够将要由JRubyFooWorker
类处理的任务排入Resque队列。
我看了一下程的密码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
我怀疑我的代码看起来像:
//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
我的FooWorker
类位于Rails应用程序的分解war文件目录中,该文件是app/workers/foo_worker.rb
我需要做些什么来确保JRuby编译器能够访问FooWorker
和Resque JRuby类来正确编译代码?
我不确定Tomcat,但我知道使用Jetty(另一个servlet容器),您可以将jruby代码编译到一个jar中,并将其放置在容器的lib目录中。
或者查看此项目https://github.com/gresrun/jesque
"Jesque是Java中Resque的一个实现。它与Ruby和Node.js(Coffee Resque)实现完全可互操作。"
它允许您将作业从java本地排队到resque。我没有用过,但看起来很有希望。