Shahzad Bhatti Welcome to my ramblings and rants!

September 9, 2008

Setting up JRuby on Rails

Filed under: Ruby — admin @ 11:25 am

In my last blog, I wrote about recent JRuby on Rails project I did at amazon. In this blog, I will show some technical details on setting up JRuby on Rails with Tomcat. I used 2.1.1 of Rails and 1.1.4 of JRuby, here are steps to setup JRuby and Rails:

  • Download Jruby 1.1.4 from http://dist.codehaus.org/jruby/
  • Setup JRUBY_HOME and PATH
     export JRUBY_HOME=installed directory of jruby
     export PATH=$JRUBY_HOME/bin:$PATH
     
  • Install Rails
     gem install rails -y
     
  • Install ActiveRecord JDBC Adapter
     jruby -S gem install activerecord-jdbc-adapter
     
  • Install warbler plugin to create war file, older version of Rails (1.2 or older) used goldspike but the newer version requires warbler
     jruby -S gem install -y  warbler
     
  • Edit config/environment.rb and add
     if RUBY_PLATFORM =~ /java/
       require 'rubygems'
       RAILS_CONNECTION_ADAPTERS = %w(jdbc)
     end
     
  • Download mysql-jdbc driver as I used MySQL. (You may have to download different driver. )
  • Copy mysql-connector-java-5.1.6-bin.jar to lib directory of jruby
  • You can edit config/warble.rb and add other jar files or directories, e.g.
       config.java_libs += FileList["lib/*.jar"]
       config.pathmaps.java_classes << "%{build/classes/,}p"
       config.webxml.jruby.min.runtimes = 2
       config.webxml.jruby.max.runtimes = 24
     
  • Edit config/environment.rb and add define adapter, driver and url as follows:
     development:
       adapter: jdbc
       driver: com.mysql.jdbc.Driver
       url: jdbc:mysql://localhost/rspm_development
       username: rspmdb_user
       password: secret
                                                                                                                                                           
     test:
       adapter: jdbc
       driver: com.mysql.jdbc.Driver
       url: jdbc:mysql://localhost/rspm_test
       username: rspmdb_user
       password: secret
                                                                                                                                                           
     production:
       adapter: jdbc
       driver: com.mysql.jdbc.Driver
       url: jdbc:mysql://localhost/rspm_production
       username: rspmdb_user
       password: secret
     
  • Create a template web.xml.erb in config directory, e.g.
     <!DOCTYPE web-app PUBLIC
       "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
     
       "http://java.sun.com/dtd/web-app_2_3.dtd">
     <web-app>
                                                                                                                                                           
         <% webxml.context_params.each do |k,v| %>
         <context-param>
             <param-name><%= k %></param-name>
             <param-value><%= v %></param-value>
     
         </context-param>
         <% end %>
                                                                                                                                                           
        <servlet>
           <servlet-name>Rails</servlet-name>
           <servlet-class>org.jruby.rack.RackServlet</servlet-class>
     
        </servlet>
                                                                                                                                                           
        <servlet-mapping>
            <servlet-name>Rails</servlet-name>
            <url-pattern>/*</url-pattern>
        </servlet-mapping>
     
                                                                                                                                                           
                                                                                                                                                           
       <filter>
         <filter-name>RackFilter</filter-name>
         <filter-class>org.jruby.rack.RackFilter</filter-class>
       </filter>
     
       <filter-mapping>
         <filter-name>RackFilter</filter-name>
         <url-pattern>/*</url-pattern>
       </filter-mapping>
                                                                                                                                                           
        <listener>
     
           <listener-class><%= webxml.servlet_context_listener || 'org.jruby.rack.RackServletContextListener'%></listener-class>
        </listener>
                                                                                                                                                           
     </web-app>
                                                                                                                                                           
     
  • Now create war file
     jruby -S warble
     
  • Finally copy the war file to webapps directory of standard J2EE web container, in my case Tomcat 6.0
  • Now point the browser to your server, e.g. http://localhost:8080/your-controller and voilla.

On final note, I found warble quite slow in generating war file and tomcat takes a while to deploy the war file, so I use WEBrick for development and Tomcat for staging/production.

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

You must be logged in to post a comment.

Powered by WordPress