David Phillips
David Phillips

Reputation: 10208

Use Ruby ActiveRecord models in Java

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.

Upvotes: 1

Views: 235

Answers (1)

rmk
rmk

Reputation: 4455

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.

Upvotes: 1

Related Questions