Skip to main content

Getting Started with Edge Rails (2.2) using JRuby and GlassFish

Posted by arungupta on November 4, 2008 at 11:48 AM PST



This blog shows how to get started with Edge Rails (2.2, href="http://weblog.rubyonrails.org/2008/10/24/rails-2-2-rc1-i18n-thread-safety-docs-etag-last-modified">almost
there)
using JRuby. The blog uses href="http://dist.codehaus.org/jruby/jruby-bin-1.1.5.zip">JRuby
1.1.5 and href="http://download.java.net/glassfish/v3-prelude/promoted/glassfish-v3-prelude-b28c.zip">GlassFish
v3 Prelude b28c to deploy a simple Rails app.

  1. Download and unzip href="http://dist.codehaus.org/jruby/jruby-bin-1.1.5.zip">JRuby
    1.1.5.
  2. JRuby 1.1.5 comes with "rake 0.8.3". However if you are
    using JRuby 1.1.4, then you need to update "rake" version to 0.8.3 as:


    style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
    cellpadding="2" cellspacing="2">
    ~/tools/jruby-1.1.4 > style="font-weight: bold;">bin/jruby -S gem update rake

    JRuby limited openssl loaded. gem install jruby-openssl for full
    support.

    http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL

    Updating installed gems

    Updating rake

    Successfully installed rake-0.8.3

    Gems updated: rake
  3. Install Edge Rails as:


    style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
    cellpadding="2" cellspacing="2">
    ~/tools/jruby-1.1.5 > style="font-weight: bold;">bin/jruby -S gem install rails -s
    http://gems.rubyonrails.org -v 2.2.0

    JRuby limited openssl loaded. gem install jruby-openssl for full
    support.

    http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL

    Successfully installed activesupport-2.2.0

    Successfully installed activerecord-2.2.0

    Successfully installed actionpack-2.2.0

    Successfully installed actionmailer-2.2.0

    Successfully installed activeresource-2.2.0

    Successfully installed rails-2.2.0

    6 gems installed

    Installing ri documentation for activesupport-2.2.0...

    Installing ri documentation for activerecord-2.2.0...

    Installing ri documentation for actionpack-2.2.0...

    Installing ri documentation for actionmailer-2.2.0...

    Installing ri documentation for activeresource-2.2.0...

    Installing RDoc documentation for activesupport-2.2.0...

    Installing RDoc documentation for activerecord-2.2.0...

    Installing RDoc documentation for actionpack-2.2.0...

    Installing RDoc documentation for actionmailer-2.2.0...

    Installing RDoc documentation for activeresource-2.2.0...
  4. Create a new app as:


    style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
    cellpadding="2" cellspacing="2">
    ~/samples/jruby/edge > style="font-weight: bold;">~/tools/jruby-1.1.5/bin/jruby -S
    rails helloworld -d mysql

    JRuby limited openssl loaded. gem install jruby-openssl for full
    support.

    http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL

          create  

          create 
    app/controllers

          create 
    app/helpers

    . . .

          create 
    log/server.log

          create 
    log/production.log

          create 
    log/development.log

          create 
    log/test.log
  5. Edge Rails do not ship with any(?, atleast MySQL) pure-Ruby
    database drivers and throws the following error if a database related
    operation is performed:


    style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
    cellpadding="2" cellspacing="2">
    ~/samples/jruby/helloworld
    >~/tools/jruby-1.1.5/bin/jruby -S rake db:create

    (in /Users/arungupta/samples/jruby/helloworld)

    !!! The bundled mysql.rb driver has been removed from Rails 2.2. Please
    install the mysql gem and try again: gem install mysql.

    rake aborted!

    no such file to load -- mysql



    (See full trace by running task with --trace)



    I find it weird that a database-backed
    framework
    does not ship database drivers. Anyway, lets
    install MySQL JDBC ActiveRecord adapter as:


    style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
    cellpadding="2" cellspacing="2">
    ~/tools/jruby-1.1.5 > style="font-weight: bold;">bin/jruby -S gem install
    activerecord-jdbcmysql-adapter

    JRuby limited openssl loaded. gem install jruby-openssl for full
    support.

    http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL

    Successfully installed activerecord-jdbc-adapter-0.8.2

    Successfully installed jdbc-mysql-5.0.4

    Successfully installed activerecord-jdbcmysql-adapter-0.8.2

    3 gems installed

    Installing ri documentation for activerecord-jdbc-adapter-0.8.2...

    Installing ri documentation for jdbc-mysql-5.0.4...

    Installing ri documentation for activerecord-jdbcmysql-adapter-0.8.2...

    Installing RDoc documentation for activerecord-jdbc-adapter-0.8.2...

    Installing RDoc documentation for jdbc-mysql-5.0.4...

    Installing RDoc documentation for
    activerecord-jdbcmysql-adapter-0.8.2...



    More details on configuring this adapter href="http://wiki.jruby.org/wiki/Running_Rails_with_ActiveRecord-JDBC">here.

  6. Create a simple scaffold:


    style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
    cellpadding="2" cellspacing="2">
    ~/samples/jruby/edge/helloworld > style="font-weight: bold;">~/tools/jruby-1.1.5/bin/jruby
    script/generate scaffold runner distance:float time:integer

    JRuby limited openssl loaded. gem install jruby-openssl for full
    support.

    http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL

          exists 
    app/models/

          exists 
    app/controllers/

          exists 
    app/helpers/

          create 
    app/views/runners

          exists 
    app/views/layouts/

          exists 
    test/functional/

          exists 
    test/unit/

          exists 
    public/stylesheets/

          create 
    app/views/runners/index.html.erb

          create 
    app/views/runners/show.html.erb

          create 
    app/views/runners/new.html.erb

          create 
    app/views/runners/edit.html.erb

          create 
    app/views/layouts/runners.html.erb

          create 
    public/stylesheets/scaffold.css

          create 
    app/controllers/runners_controller.rb

          create 
    test/functional/runners_controller_test.rb

          create 
    app/helpers/runners_helper.rb

          
    route  map.resources :runners

      dependency  model

         
    exists    app/models/

         
    exists    test/unit/

         
    exists    test/fixtures/

         
    create    app/models/runner.rb

         
    create    test/unit/runner_test.rb

         
    create    test/fixtures/runners.yml

         
    create    db/migrate

         
    create   
    db/migrate/20081103190813_create_runners.rb

    ~/samples/jruby/edge/helloworld > style="font-weight: bold;">~/tools/jruby-1.1.5/bin/jruby -S
    rake db:create

    (in /Users/arungupta/samples/jruby/edge/helloworld)

    ~/samples/jruby/edge/helloworld > style="font-weight: bold;">~/tools/edge/jruby-1.1.5/bin/jruby
    -S rake db:migrate

    (in /Users/arungupta/samples/jruby/edge/helloworld)

    ==  CreateRunners: migrating
    ==================================================

    -- create_table(:runners)

       -> 0.0068s

       -> 0 rows

    ==  CreateRunners: migrated (0.0077s)
    =========================================



    Some words here.

  7. Change "config/database.yml" to use the JDBC adapter.
    Change:


    style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
    cellpadding="2" cellspacing="2">
    development:

      adapter: mysql

      encoding: utf8

      database: helloworld_development

      pool: 5

      username: root

      password:

      socket: /tmp/mysql.sock



    to


    style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
    cellpadding="2" cellspacing="2">
    development:

      adapter: jdbcmysql

      encoding: utf8

      database: helloworld_development

      pool: 5

      username: root

      password:

      socket: /tmp/mysql.sock



    The change is highlighted in bold letters.

  8. Download href="http://download.java.net/glassfish/v3-prelude/promoted/glassfish-v3-prelude-b28c.zip">GlassFish
    v3 Prelude href="http://download.java.net/glassfish/v3-prelude/promoted/glassfish-v3-prelude-b28c.zip">
    28c, unzip and start it as:


    style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
    cellpadding="2" cellspacing="2">
    ~/tools/glassfish/v3/28c/glassfishv3-prelude/glassfish
    >java -Xmx512m
    -DJRUBY_HOME=/Users/arungupta/tools/jruby-1.1.5/ -jar
    modules/glassfish.jar
     
  9. Finally deploy the application as:


    style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
    cellpadding="2" cellspacing="2">
    ~/samples/jruby/edge > style="font-weight: bold;">~/tools/glassfish/v3/28c/glassfishv3-prelude/glassfish/bin/asadmin
    deploy helloworld



    Command deploy executed successfully.

And now the scaffold is accessible at
"http://localhost:8080/helloworld/runners".



This application does not show any of the href="http://guides.rubyonrails.org/2_2_release_notes.html">cool
features (thread-safe, i18n, pooled datbase connections, ...)
coming up in Rails 2.2. But at least it clearly explains how to
get started if you want to develop and run a Rails 2.2 application
using
JRuby.



Subsequent blogs will provide more information about performance
advantages, multi threading, connection pooling and other benefits
offered by GlassFish for Rails applications.



Technorati: href="http://technorati.com/tag/rubyonrails">rubyonrails
glassfish
v3 href="http://technorati.com/tag/jruby">jruby

Related Topics >>

Comments

Sure, including JRUBY_HOME/bin in the PATH would make it easy. I explicitly use complete path to show the exact interpreter that is being used. Yes, adding JRUBY_HOME to domain.xml is the right way but this is a quick way to startup without any need to change your existing configuration. A new version of GlassFish gem was released this morning and we are pretty close to making it final. Can you provide more information about which links are not working ?

Hi Arun, great example, but for testing better is configure PATH on JRUBY_HOME/bin ... and add jvm-option with JRUBY_HOME to domain config. and gem install jruby-openssl for full support like You see in yours executions of JRuby. I have two question, why this application is not showed in glassfish application (in admin panel), and how prepare in this way ... war file. And second question is ... what about glassfish gem ... were is ? old link dont work... this project is currently dead ?