简体   繁体   English

Java 到 JRuby 到 Resque

[英]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.我有一个混合 Web 应用程序,它在同一个 Tomcat 中运行一个 Java WAR 文件和一个 JRuby WAR 文件。

We have decided to use (JRuby) Resque as our job queue.我们决定使用 (JRuby) Resque 作为我们的作业队列。 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.其中 FooWorker 是在 JRuby 端定义和使用的工作类(并包含在 JRuby WAR 文件中),它在处理队列中的作业时由 JRuby Resque rake 任务调用。

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.我想让 Java 代码能够将 Resque 队列上的任务排入队列,以便由 JRuby FooWorker类处理。

I took a look at Tommy Cheng's code at https://github.com/tc/call-jruby-from-java-example .我在https://github.com/tc/call-jruby-from-java-example 上查看了 Tommy Cheng 的代码。

//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我的FooWorker类位于 Rails 应用程序的分解后的 war 文件目录中,该文件是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?我需要做什么来确保 JRuby 编译器可以访问FooWorker和 Resque JRuby 类以正确编译代码?

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.我不确定 Tomcat,但我知道使用 Jetty(另一个 servlet 容器),您可以将 jruby 代码编译成 jar 并将其放在容器的 lib 目录中。

Or check out this project https://github.com/gresrun/jesque或者查看这个项目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." “Jesque 是用 Java 实现的 Resque。它可以与 Ruby 和 Node.js(Coffee-Resque)实现完全互操作。”

It lets you enqueue jobs natively from java to resque.它允许您将作业从 Java 本地排队到 resque。 I haven't used it but it looks promising.我还没有使用它,但它看起来很有希望。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM