JRuby: Deploying a Rails Application on Tomcat

JRuby: Deploying a Rails Application on TomcatThis is a brief tutorial on how to get a simple Ruby on Rails application up and running on Tomcat.

What you will need

  1. subversion client
  2. Java 5 JDK
  3. Tomcat 5.5.x
  4. JRuby 1.0
  5. Ruby on Rails
  6. ActiveRecord-JDBC gem
  7. goldspike Rails plugin (allows you to create a deployable war from an existing Rails app)
  8. MySQL database
  9. mysql-connector JDBC driver


Assumptions and Notes

It’s assumed that you know your way around a Java development environment and have a JDK, Tomcat, MySQL and a subversion client installed already. From there you need to download and install JRuby. Once you have JRuby installed, there are some things to note:

  1. Most importantly, I had issues with the fact that I had an existing install of Ruby on Rails installed which caused me problems when installing gems, etc. with JRuby. If you have an existing install of RoR, just be sure you are working with the gem executable in your <jruby-install-dir>/bin and not your existing gem executable associated with RoR.
  2. People including myself have had classpath issues when trying to call Java libraries. In general, I’ve been able to workaround these issues by including library dependencies (in the form of a jar) in the <jruby-install-dir>/lib directory.


Testing your JRuby Install

Once you’re confident you have JRuby installed you should be able to run the following command and get a similar result:

jruby_version


Creating Your Ruby on Rails Application in JRuby

Now you’re ready to install Ruby on Rails in your JRuby install. Run the following:

gem install rails -y --no-ri --no-rdoc

If you’ve installed Rails before the output should look familiar. Once it’s done you’re ready to create a Rails app by running something like:

rails project-name

Now to mix things up a bit. Your Rails application will need to be reconfigured from it’s default database configuration to use the ActiveRecord-JDBC gem along with a JDBC driver to connect to a database. First, let’s install the ActiveRecord-JDBC gem:

cd project-name
gem install ActiveRecord-JDBC

Now we need to modify the database.yml file located in the config directory of your new Rails application:

database_yml

Here we’ve basically changed the adapter to JDBC. Now we update the config/environment.rb file by adding the section in the red outline:

config_env

Now place the mysql-connector JDBC driver jar in your <jruby-install-dir>/lib and while your at it, drop the jar in your <tomcat-install-dir>/common/lib directory.

Depending how familiar you are with RoR, you may want to add some meat to your application by adding a model or two with something like:

jruby script/generate model Widget

Then edit the resulting migration script (db/migrate/001_create_widgets.rb) to include whatever columns you’d like as a part of the widgets table:

db_migrate

Once you’ve added the columns in the above script, run the following to actually update the database:

rake db:migrate
rake db:migrate RAILS_ENV=production

You now have the model taken care of. The second command above applies the same changes to the production database. To quickly be able to perform CRUD operations on the model you’ve just created, run the following:

jruby script/generate scaffold Widget

Install the ActiveRecord-JDBC gem:

sudo gem install ActiveRecord-JDBC

You should get something like the following as a result:

gem_install_ar_jdbc

In order to facilitate the process of actually putting together a war from your Rails app that can be easily deployed, the goldspike plugin was created. To install it, run the following from your Rails app root directory:

jruby script/plugin install svn://rubyforge.org/var/svn/jruby-extras/trunk/rails-integration/plugins/goldspike

Once the goldspike plugin is installed, you can create a deployable war in one easy command:

rake war:standalone:create

You will see it automagically download some Java dependencies which it will get from one of the following locations:

  1. A local artifact: lib/java/jruby-1.0.jar
  2. An artifact in a local maven repo: ~/.m2/repository/org/jruby/jruby/1.0/jruby-1.0/jar
  3. An artifact in a remote maven repo: http://www.ibiblio.com/maven2/org/jruby/jruby/1.0/jruby-1.0/jar

In most cases it will probably hit the ibiblio.com/maven2 repository for at least one dependency. This is useful to know because if you’re behind a firewall, there are known issues with fetching remote jars. If you run into this problem, I suggest downloading the dependencies manually and placing them in the local lib/java directory.

After the war is created, you can just drop it in your <tomcat-install-dir>/webapps directory and start Tomcat. You should now be able to access your Ruby on Rails app running on Tomcat from your browser:

http://localhost:8080/project-name/widgets/list

That’s it!

In my next post, I will show you how to call Spring-managed beans from your Rails app.

Apache has come up with some really dedicated servers like TomCat. This particular computer software can be used with a number of other web servers as well. TomCat in particular does not need much augmentation and is pretty much advanced as a security software too. Weblog gurus are well aware of this but rookie bloggers are still in the process of discovering benefits of remote backup on different servers. They are the ones who have become insatiable vouchers of dsl internet. Introduction to any new mashup service offering features like voip service or even wireless internet service amazes them.

9 Responses to “JRuby: Deploying a Rails Application on Tomcat”

  1. Random Syntax » Rails on Tomcat - thanks to JRuby! Says:

    […] patiently for a year and a bit and now finally it looks like I can deploy my rails apps on Tomcat. Digital Sanctum has posted a nifty tutorial. An additional deployment platform is always welcome but this means […]

  2. Jakob Says:

    This is exactly what I expected to find out after reading the title o.us poetry. Thanks for informative article

  3. Music-Band Says:

    Hey

    I was surfing the web and i saw this site, pretty cool.
    Currently im running and adult site:Reachton
    k, just want to say hi :)
    Can i link you from my site? im looking for quality content like yours. If no let me know if i can add u in exchange for a montly fee or something.

  4. Robert Says:

    Standard Rails Apps suffer under a single-threaded model (mongrel serializes the data so that you don’t end up with concurrent requests in the non-threadsafe rails land).

    Does deploying with JRuby on Tomcat effectively allow Rails to run in a truly concurrent manner?

  5. GSIY … Ruby-Rails Portal Says:

    […] I want to deploy my RoR application on a apache tomcat server. I found a workthrough at http://www.digitalsanctum.com/2007/07/24/jruby-deploying-a-rails-application-on-tomcat/ but I have problems with the goldspike plugin. When I try to execute "rake […]

  6. Ruby Brasil » Blog Archive » Tutoriais sobre JRuby e Rails Says:

    […] JRuby: Desenvolvendo uma aplicação Rails no TomCat Este é um breve tutorial de como montar uma aplicação simples em Rails para rodar no Tomcat. http://www.digitalsanctum.com/2007/07/24/jruby-deploying-a-rails-application-on-tomcat/  […]

  7. razmaspaz Says:

    Hey,
    I just came across this tutorial. I’ve gotten all the way to running create:standalone:war, and I get an error. I assume it has something to do with the latest version of goldspike having a syntax error in it. Any ideas?

  8. razmaspaz Says:

    nevermind…the catch is that I need to execute jruby -S rake, not just rake

  9. nosewheelie » Blog Archive » Running a Rails app under Tomcat using JRuby Says:

    […] has me looking at running a Rails app under Tomcat using JRuby [1]. A quick google revels many conflicting guides, which seem to stem from different versions of JRuby and tools (e.g. GoldSpike vs. Warbler). […]

Leave a Reply





Fox Holes

Rabbit Holes

Chasms