在 Java 中使用 Ruby ActiveRecord 模型
我有一个 Ruby on Rails 应用程序,模型中包含很多业务逻辑。我还有一个 Java 后端进程,需要使用相同的业务逻辑。 如何将 Rails 应用程序打包到可以从 Java 调用的 jar 中(使用 JRuby)?
出于性能原因,我需要直接在 Java 中访问代码。执行 HTTP 请求的开销太大。使用消息队列不起作用,因为访问需要同步。
I have a Ruby on Rails application with much business logic contained in the models. I also have a backend process in Java that needs to use the same business logic. How can I package the Rails app into a jar that I can call from Java (using JRuby)?
I need to access the code directly in Java for performance reasons. Performing an HTTP request has too much overhead. Using a message queue won't work as the access needs to be synchronous.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你看过莺吗?
https://github.com/nicksieger/warbler
这将帮助您将 Rails 应用程序打包成一个 war。这对你有帮助吗?
或者,您可以只查看 warbler 生成的文件:它为每个 .rb 文件创建一个 .class 文件,然后生成一个包含 .class 文件的文件。也许你可以把它们打包进一个罐子里。
另外,有关如何在 Rails 应用程序之外从 jruby 访问 activerecord 的一些信息,请参阅《JRuby Cookbook》一书的第 2.14 节。基本上,它涉及从database.yml读取配置,并使用ActiveRecord.built_connection打开数据库连接,然后您就可以通过要求它们来访问您的模型了。
Have you looked at Warbler?
https://github.com/nicksieger/warbler
This will help you package your rails app into a war. Would that help you?
Alternatively, you could just look at the files warbler generates: it creates a .class file for each .rb file, and then generates a file that includes the .class file. Perhaps you could take them and package them into a jar.
Also, for some info on how to access activerecord from jruby outside the rails app, look at section 2.14 from the book 'JRuby Cookbook'. Basically, it involves reading the config from database.yml, and opening up a DB connection using ActiveRecord.establish_connection, then you are ready to access your models by just requiring them.