Saturday, April 11, 2009

Darren on Ruby: Migrating Rails to Tomcat / JEE, Part 1: Switching to JRuby & Apache Derby

Prologue

Complete this tutorial as a prelude to this blog. This is less about you learning RoR, but more about giving you a convenient starting point to proceed. If you already have a project in mind that you want to migrate, make a copy and work with that. Note for Windows users: When you get to the point of installing the SQLite3 gem, use version 1.2.3 as at the time of this writing, it is the most recent version compatible with Windows; gem install sqlite3-ruby -v 1.2.3 -y. For your convenience, here are some links you might find useful while completing that tutorial: http://localhost:3000
http://localhost:3000/home/index

Run it on Ruby

So you should now be at a point where you can successfully run your application on Mongrel or WEBrick using MRI. If you haven't done that already, give that a whirl and test it by going here. You should see the 'Hello, Rails!' home page. Now get you hands on the default generated index.html file and put it back in the public folder and hit refresh/F5. You should now see the 'Welcome aboard... You’re riding Ruby on Rails!' welcome page. Click on the 'About your application’s environment' link and you should see a flash message (I think that's what they call it) describing you environment; you should observe two things:
Ruby Version
1.8.6 (i386-mswin32)
Database adapter
sqlite3
Now shut down the web server.

Prepare for JRuby

  1. Setup JRuby
    • Download and install JRuby 1.2.0 from here
    • Set your JRUBY_HOME environment variable and add JRUBY_HOME\bin to your PATH environment variable.
    • Checkout this blog for jgem and jrake
  2. Install Rails (currently 2.3.2)
    • jgem install rails -y
  3. Setup Apache Derby
    • Download and install Derby 10.4.2 from here.
    • Set your DERBY_HOME environment variable and add DERBY_HOME\bin to your PATH environment variable.
    • Copy derby.jar, derbyclient.jar and derbynet.jar in DERBY_HOME\lib to JRUBY_HOME\lib.
    • jgem install activerecord-jdbcderby-adapter-0.9.0 -y (I had an issue with version 0.9.1; something about an uninitialized constant...)
  4. Prepare Development DB
    • Update the RAILS_ROOT\config\database.yml file; comment out the default development database definition (or just prepend an underscore or something) and paste in the following below it:
      development:
        adapter: jdbc
        url: jdbc:derby:db\blog_development;create=true
        driver: org.apache.derby.jdbc.EmbeddedDriver
        username: app
        password: app
      
    • Run the following commands:
      jrake db:sessions:create
      jrake db:migrate
      

Run it on JRuby

Now you should be at a point where you can run your application on Mongrel or WEBrick using JRuby. Start it up, this time running jruby script\server and test it again by going here. Click on the 'About your application’s environment' link; this time you'll observe two significant differences:
Ruby Version
1.8.6 (java)
Database adapter
jdbc
Well that's all folks - You have now migrated to JRuby!. But where are all your blogs?!

What's Next?

Next I will run through a couple of ways of migrating your SQLite3 data to Derby; when it's ready you can catch that blog here. Cheers.

1 comment:

darynholmes said...

This looks interesting. I am looking forward to the next post in the series...