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.

September 6, 2008

Rails save the day

Filed under: Ruby — admin @ 12:52 pm

In our daily scrum meeting at work last week, we identified one of application that had been crashing often. The application was pretty simple web application written in Java using Spring-MVC, Hibernate, Spring and Apache-commons libraries. However, the developer who released it embedded that application in another much bigger web application in order to save some deployment issues, which can be real pain at amazon. So, I volunteered to address the issue and rewrite the application in JRuby on Rails (2.1). It took me a day to rewrite all application and migrate all data from the old application. Along the way, I also added a couple of some additional features. The original application took a couple of weeks to develop and was consisted of over 6500 lines of code. The ruby version consisted a little less than 800 lines of code. I have been using Ruby on my own since 2004 and started learning Rails in 2005. Though, I started using Rails a bit more on some side projects after I attended Rails conference of 2006. I still use Java for most of the work, and was delighted to show speed of development with Rails at work. Since, the deployment issues are still pain in Rails so I chose JRuby that allows Rails apps to deploy on Tomcat, which we are using for most of the projects. Hopefuly, that will fix the stability issues and save me from pagers as I will be oncall next week.

Powered by WordPress