JavaData & JavaJRuby on Rails with Nine Lives: Running a JRuby on Rails Application...

JRuby on Rails with Nine Lives: Running a JRuby on Rails Application on Tomcat

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

If you have not heard the buzz about JRuby and Rails, you must be living under a rock. The Java, Ruby and Rails communities are all excited with possibilies that JRuby and Rails gives to web application developers. Java has been used for more than 10 years in desktop, mobile, web, and enterprise application development. Ruby and Rails are the twin towers of web application agility and coolness and are igniting the web and software application development community with their pragmatic practices. With JRuby, Ruby, and Rails, applications can now run on top of JVM and use the vast Java frameworks and libraries available.

In my previous article, “Running your First Rails Application on JRuby,” I showed you how to get JRuby installed and create and run a basic non-CRUD Rails application within it. This was simple and painless and demonstrated JRuby’s initial support Rails. Since then, JRuby has been enhanced to fully support Rails and is now at version 1.0 status. In this article, you use JRuby to create a simple CRUD-based Rails application for managing a list of guitars (similar to the one in my “Skip the CRUD” article) and run it on the Java-based Tomcat application server. This will demonstrate JRuby’s improved support for Rails applications by using database access via JDBC and the ability to run a Rails application on a Java application server.

Installing JRuby

For this article, I will use a machine running the Ubuntu Linux distribution. I am going to assume you already installed JRuby 1.0 into the /opt/jruby-1.0 directory. I create a symbolic link in /opt/jruby pointing to the JRuby install directory. You will also need to create a $JRUBY_HOME environment variable pointing to the /opt/jruby directory so you can run JRuby commands anywhere. The following screenshot shows JRuby 1.0 installed in /opt:

Figure 1: JRuby installation

Installing Rails in JRuby and Creating Your Rails Application

Installing Rails into JRuby is as simple as running the following command from your $JRUBY_HOME directory:

jruby -S gem install rails -y -–no-ri -–no-rdoc

Figure 2: Installing Rails into JRuby

The –S switch tells JRuby to run the command in JRuby’s bin directory.

You’ll need to install the ActiveRecord-JDBC gem as well. This will allow Rails’ ActiveRecord to use Java’s native JDBC driver to access the database:

jruby -S gem install activerecord-jdbc

Figure 3: Installing ActiveRecord-JDBC into JRuby

Now, create your JRuby on Rails application. I change directories to my local JRuby apps directory /home/dsuspense/jruby/apps and create a railswartest application:

jruby –S rails railswartest

Installing Goldspike

To get your app to deploy to a Java server, you must install a Rails Integration plugin called Goldspike.

Change to the railswartest directory and run the following command to add the plugin to your app:

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

Figure 4: Installing Goldspike into your JRuby on Rails application

Goldspike adds some new rake (Ruby version of Make) tasks that allow you to create a WAR (Web Application ARchive) file of your Rails application. WAR files are the standard way of packaging and deploying Java-based web applications. Run the following command to ensure you have the war rake tasks available now to your app:

jruby –S rake tasks

MySQL JDBC Driver Installation and Configuration

You will use MySQL for your database. You need to download the MySQL 5 JDBC driver and extract it into a directory. There will be a MySQL JDBC driver file mysql-connector-java-5.0.6-bin.jar in the directory. Copy this file into the $JRUBY_HOME/lib directory and rename it to mysql-connector-java-5.0.6.jar:

Figure 5: Installing MySQL JDBC driver into JRuby

You will need to add the MySQL JDBC 5 driver to the list of Java libraries that the Goldspike plugin includes when it builds the WAR file for your application. It will pick this file up from the $JRUBY_HOME/lib directory. Within the railswartest directory, edit the vendor/plugins/goldspike/lib/war_config.rb file and the following to the java_libraries list:

add_java_library(maven_library ('mysql', 'mysql-connector-java',
                                '5.0.6'))

Figure 6: Adding the MySQL 5 JDBC driver to the Goldspike Java libraries list

Configuring the Database

Next, you need to tell your JRuby on Rails app to use ActiveRecord-JDBC and what database you will be using. Within the railswartest directory, edit the config/environment.rb file and add the following just before the line containing Rails::Initializer.run do |config|:

if RUBY_PLATFORM =~ /java/
   require 'rubygems'
   gem 'ActiveRecord-JDBC'
   require 'jdbc_adapter'
end

Figure 7: Configuring JRuby on Rails app to use ActiveRecord-JDBC

This tells Rails that, if it detects that it is running on Java (in other words, JRuby) to load the ActiveRecord-JDBC gem.

Now, configure your JDBC driver. Within the railswartest directory, edit the config/database.yml file and edit the development: and production: configurations to contain the following:

development:
   adapter: jdbc
   driver: com.mysql.jdbc.Driver
   url: jdbc:mysql://localhost:3306/railswartest
   username: <username>
   password: <password>

production:
   adapter: jdbc
   driver: com.mysql.jdbc.Driver
   url: jdbc:mysql://localhost:3306/railswartest
   username: <username>
   password: <password>

Figure 8: Configuring the JRuby on Rails app development and production database connections

I am using a database named railswartest, which I created using the MySQL Query Browser:

Figure 9: Creating MySQL database ‘railswartest’ using MySQL Query Browser

Creating Your Migration

You will use Rails Migrations to manage the creation and deletion of the database table that you are going to use to manage your Guitars from your app. Your table is going to conveniently be named guitars. Run the following from the railswartest direcrory:

jruby script/generate migration add_a_new_table

This will create the db/migrate/001_add_guitar_table.rb file:

Figure 10: Creating our add_guitar_table migration

This file contains a Ruby class named AddGuitarTable that extends Rails’ ActiveRecord::Migration class. Edit this file to contain the following:

class AddGuitarTable < ActiveRecord::Migration
   def self.up
      create_table :guitars do |table|
         table.column :make,  :string, :null => false
         table.column :model, :string, :null => false
         table.column :color, :string, :null => false
      end
   end

   def self.down
      drop_table :guitars
   end
end

Figure 11: AddGuitarTable migration class

Now, run the migration:

jruby -S rake db:migrate

Figure 12: Running your migration to create the guitars table

Running MySQL Query Browser confirms that the guitars table has been created by the migration:

Figure 13: Empty guitars table in MySQL

Now, create your scaffold for your Guitar Ruby class that Rails will use to manage your guitars table. Run the following command:

jruby script/generate scaffold Guitar

Figure 14: Generating the Guitar class scaffold

Testing the Rails Application Using WEBrick

Now that you have everything you need to do CRUD operations on our guitars table, give your app a quick test run using Ruby’s WEBrick server. You run the following command to start WEBrick and point your browser to http://localhost:3000/guitars:

jruby script/server

Figure 15: Running the railswartest app using the WEBrick server

Figure 16: The railswartest app running on the JRuby WEBrick server

Installing Tomcat

I assume you already have Apache Tomcat installed. If not, you can download it and uncompress it to your desired installation directory. I am using Tomcat version 5.5.17; it is installed to the /opt/tomcat directory.

Creating the WAR File and Deploying It to Tomcat

To create a WAR file of your railstestwar app, run the following command:

jruby -S rake war:standalone:create

Figure 17: Creating the railswartest WAR

You also can clean all temporary WAR files by running the command:

jruby -S rake tmp:war:clear

Copy the railswartest.war file to the /opt/tomcat/webapps directory:

Figure 18: Copying the railswartest WAR file to the Tomcat webapps directory

Start up Tomcat; you should be able to point your browser to http://localhost:8080/ and see the Rails welcome page:

Figure 19: Starting up the Tomcat server

Figure 20: The railswartest app Rails welcome page running on the Tomcat server

Now, test the CRUD for the guitars table. Point the browser to http://localhost:8080/guitars; you should see an empty guitar list. You now can do some simple CRUD operations on the guitars table to test your app. What is happening under the covers is really exciting. Your JRuby on Rails app is running on Tomcat and is using JDBC to perform CRUD actions on the guitars table! The following screenshots show some CRUD operations on the guitars table:

Figure 21: Empty guitars table

Figure 22: Creating a new guitar

Figure 23: New guitar entry in guitars table

Figure 24: After adding some more guitars

Figure 25: Editing an exisiting guitar

Figure 26: Edited guitar successfully saved

Figure 27: Deleting an existing guitar

Figure 28: Deletion of existing guitar successful

Conclusion

In this article, you used JRuby to create a simple CRUD-based Rails application to manage a list of guitars (similar to the one in my “Skip the CRUD” article) and you successfully ran it on the Java-based Tomcat application server. This demonstrated JRuby’s improved support for Rails applications by using database access via JDBC and the ability to run Rails applications on a Java application server!

References

About the Author

Dominic Da Silva (http://www.dominicdasilva.com/) is the President of SilvaSoft, Inc., a software consulting company specializing in Java- and Ruby-based web and web services development. He has worked with Java since the year 2000 and is a Linux user from the 1.0 days. He is also Sun Certified for the Java 2 platform. Born on the beautiful Caribbean island of Trinidad and Tobago, he now makes his home in sunny Orlando, Florida.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories