0

我编写了一个小型 Java 类来从 xls 文件中读取嵌入式图像,并且需要在我的 Ruby on Rails 应用程序的后台进程(使用 Sidekiq)中使用它。但看起来线程存在一些问题,因为guarantee(get_thread() == thread) failed: must be the same thread, quickly当 sidekiq 进程开始执行使用 Rjb 的方法时,JVM 会抛出错误。

我就是这样设置的。

我写了一个初始化程序来设置 rjb

require 'rjb'

JARS = Dir.glob("#{Rails.root}/lib/java_libs/*.jar").join(':')
Rjb::load(JARS)
EXCEL_IMAGE_READER = Rjb::import('tools.ImageReader')

然后我在后台进程中使用它

  def get_excel_images
    p 'Starting to get the images'
    images = []
    image_reader = EXCEL_IMAGE_READER.new(@excel_path)
    image_reader.get_file_names.each do |file_name|
      images << Attachment.new_from_bytes(image_reader.get_file(file_name), file_name)
    end
    images
  end

但是一旦方法开始执行,JVM 就会抛出异常。这是 Sidekiq 的日志。

2014-10-30T09:47:02Z 11748 TID-17e7qk INFO: Running in ruby 2.1.1p76 (2014-02-24 revision 45161) [i686-linux]
2014-10-30T09:47:02Z 11748 TID-17e7qk INFO: See LICENSE and the LGPL-3.0 for licensing details.
2014-10-30T09:47:02Z 11748 TID-17e7qk INFO: Starting processing, hit Ctrl-C to stop
2014-10-30T09:47:31Z 11748 TID-19393k Sidekiq::Extensions::DelayedClass JID-08eac9ed686f8d6146cda67f INFO: start
"Starting to get the images"
#
# A fatal error has been detected by the Java Runtime Environment:
#
#  Internal Error (threadLocalStorage.cpp:60), pid=11748, tid=2960567104
#  guarantee(get_thread() == thread) failed: must be the same thread, quickly
#
# JRE version: OpenJDK Runtime Environment (7.0_65-b32) (build 1.7.0_65-b32)
# Java VM: OpenJDK Client VM (24.65-b04 mixed mode, sharing linux-x86 )
# Derivative: IcedTea 2.5.3
# Distribution: Ubuntu 12.04 LTS, package 7u71-2.5.3-0ubuntu0.12.04.1
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# /home/mika/projects/my_project/hs_err_pid11748.log
#
# If you would like to submit a bug report, please include
# instructions on how to reproduce the bug and visit:
#   http://icedtea.classpath.org/bugzilla
#
Aborted (core dumped)

我试图将这些东西从初始化程序移到get_excel_images方法中,但没有任何效果。如果我直接从控制台或通过 rspec 测试运行该代码,则该代码有效。

有谁知道如何解决这个问题?

4

1 回答 1

1

我从 Rjb Github 问题https://github.com/arton/rjb/issues/24中找到了一些信息。看起来降级到 ruby​​ 1.9.3 和 rjb 1.4.9 可能会解决这个问题,但这不是一个好的解决方案,因为 ruby​​ 1.9.3 只会在几个月后收到安全补丁。问题是如何创建线程以及它如何从 ruby​​ 1.9.3 更改为 ruby​​ 2。

在尝试了许多不同的解决方案后,我转向了 JRuby。现在java交互很容易工作。

更新:

在为 JRuby 的缓慢而痛苦了一年之后,我将我所有的 Java 东西移到了一个独立的守护进程中,并通过 TCP Socket 与守护进程通信,使用 JSON 传输数据。现在我又回到了 MRI Ruby,开发过程再次让人感到愉快。

于 2014-11-06T17:22:03.400 回答