Miles to go …

August 13, 2007

First JRuby on Rails App in GlassFish V3

Filed under: web2.0 — arungupta @ 12:02 am

In a previous screencast, I showed how a Rails application can be deployed as WAR file on GlassFish V2. In GlassFish V3, the Grizzly connector by-passes the need to bundle a Rails application as WAR. Instead it directly invokes JRuby interpreter and deploys a Rails application without any modification.

This blog entry describes the exact steps to deploy your first JRuby application in GlassFish V3 Technology Preview builds.

  1. Download, Install and Configure JRuby
    1. If you already have downloaded JRuby1.0, then skip this step. Otherwise download and install JRuby1.0 in a directory, say ‘c:\jruby-bin-1.0‘ (lets say JRUBY_HOME).
    2. Add "JRUBY_HOME\bin" in your environment PATH.
    3. If you have already configured Rails in your JRuby installation, then skip this step. Otherwise install Rails by giving the command:

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

      "-S" switch instructs JRuby to run the command in it’s "bin" directory. The output of the command looks like:

      C:\jruby-bin-1.0>gem install rails -y --no-rdoc
      Bulk updating Gem source index for: http://gems.rubyforge.org
      Successfully installed rails-1.2.3
      Successfully installed activesupport-1.4.2
      Successfully installed activerecord-1.15.3
      Successfully installed actionpack-1.13.3
      Successfully installed actionmailer-1.3.3
      Successfully installed actionwebservice-1.2.3
      Installing ri documentation for activesupport-1.4.2...
      Installing ri documentation for activerecord-1.15.3...
      Installing ri documentation for actionpack-1.13.3...
      While generating documentation for actionpack-1.13.3
      ... MESSAGE: java.lang.ArrayIndexOutOfBoundsException: null
      ... RDOC args: --ri --op C://jruby-bin-1.0/lib/ruby/gems/1.8/doc/actionpack-1.13.3/ri --quiet lib
      (continuing with the rest of the installation)
      Installing ri documentation for actionmailer-1.3.3...
      Installing ri documentation for actionwebservice-1.2.3...
  2. Download, Install and Configure GlassFish V3
    1. Download GlassFish V3 Technology Preview 1, Build 2.
    2. Install by giving the command:
      java -jar glassfish-installer-v3-preview1-b2.jar

      This will create a new directory by the name "glassfish" in your current directory.

    3. Add "GLASSFISH_HOME\bin" in your environment PATH.
    4. Edit "config\asenv.bat" and add "set JRUBY_HOME=C:\jruby-bin-1.0" as the last line. Make sure to change the directory location to match your JRUBY_HOME.
  3. Create a Rails application
    1. Create a standard Rails application by giving the command:
      jruby -S rails hello

      This creates a new directory "hello" in your current directory. The output of the command looks like:

      create
      create app/controllers
      create app/helpers
      create app/models
      create app/views/layouts
      create config/environments
      create components
      create db
      create doc
      create lib
      create lib/tasks
      create log
      create public/images
      create public/javascripts
      create public/stylesheets
      create script/performance
      create script/process
      create test/fixtures
      create test/functional
      create test/integration
      create test/mocks/development
      create test/mocks/test
      create test/unit
      create vendor
      create vendor/plugins
      create tmp/sessions
      create tmp/sockets
      create tmp/cache
      create tmp/pids
      create Rakefile
      create README
      create app/controllers/application.rb
      create app/helpers/application_helper.rb
      create test/test_helper.rb
      create config/database.yml
      create config/routes.rb
      create public/.htaccess
      create config/boot.rb
      create config/environment.rb
      create config/environments/production.rb
      create config/environments/development.rb
      create config/environments/test.rb
      create script/about
      create script/breakpointer
      create script/console
      create script/destroy
      create script/generate
      create script/performance/benchmarker
      create script/performance/profiler
      create script/process/reaper
      create script/process/spawner
      create script/process/inspector
      create script/runner
      create script/server
      create script/plugin
      create public/dispatch.rb
      create public/dispatch.cgi
      create public/dispatch.fcgi
      create public/404.html
      create public/500.html
      create public/index.html
      create public/favicon.ico
      create public/robots.txt
      create public/images/rails.png
      create public/javascripts/prototype.js
      create public/javascripts/effects.js
      create public/javascripts/dragdrop.js
      create public/javascripts/controls.js
      create public/javascripts/application.js
      create doc/README_FOR_APP
      create log/server.log
      create log/production.log
      create log/development.log
      create log/test.log
      
    2. Add a controller to the application by changing to the directory "hello" and giving the command:
      jruby script/generate controller say hello

      The output of the command looks like:

      exists app/controllers/
      exists app/helpers/
      create app/views/say
      exists test/functional/
      create app/controllers/say_controller.rb
      create test/functional/say_controller_test.rb
      create app/helpers/say_helper.rb
      create app/views/say/hello.rhtml
    3. In hello\app\views\say directory, edit "hello.rhtml" such that it looks like:
      <h1>Say#hello</h1>
      <p>Find me in app/views/say/hello.rhtml</p>
      <%= @hello_string %>
    4. In hello\app\controllers directory, edit "say_controller.rb" such that it looks like:
      class SayController < ApplicationController
        def hello
          @hello_string = "Hello from Controller!"
        end
      end
  4. Run the application in GlassFish V3
    1. In GLASSFISH_HOME, start V3 container by giving the command:
      java -jar lib\glassfish-10.0-SNAPSHOT.jar

      The output of the command looks like:

      [#|2007-08-10T15:00:52.551-0700|INFO|GlassFish10.0|javax.enterprise.system.core|_ThreadID=10;_ThreadName=Thread-2;|Listening on port 8080|#]
      [#|2007-08-10T15:00:52.736-0700|INFO|GlassFish10.0|javax.enterprise.system.core|_ThreadID=10;_ThreadName=Thread-2;|Supported containers : phobos,web,jruby,php|#]
      [#|2007-08-10T15:00:52.753-0700|INFO|GlassFish10.0|javax.enterprise.system.core|_ThreadID=10;_ThreadName=Thread-2;|Glassfish v3 started in 802 ms|#]
    2. In the parent directory of "hello", deploy the application by giving the following command:
      asadmin deploy --path hello
    3. The output of the command looks like:

      C:\workarea\samples\gfv3>java -jar C:\testbed\v3-p1-v2\glassfish\bin\\..\lib\admin-cli-10.0-SNAPSHOT.jar
      deploy --path helloSUCCESS : Application hello deployed successfully

      The GlassFish console shows the following entry:

      [#|2007-08-10T15:01:53.833-0700|INFO|GlassFish10.0|GRIZZLY|_ThreadID=11;_ThreadName=httpWorkerThread-8080-0;|New Servicing page from: C:\workarea\samples\gfv3\hello\public|#]
      C:/testbed/v3-p1-v2/glassfish/lib/jruby/lib/ruby/gems/1.8/gems/actionmailer-1.3.3/lib/action_mailer.rb:49 warning: already initialized constant MAX_LINE_LEN
      [#|2007-08-10T15:02:15.740-0700|INFO|GlassFish10.0|javax.enterprise.system.tools.deployment|_ThreadID=11;_ThreadName=httpWorkerThread-8080-0;|hello jruby application loaded in 22083 ms|#]
    4. The application can now be accessed at "http://localhost:8080/hello/say/hello". The GlassFish console shows the following entry:
      /hello/say/hello
      [#|2007-08-10T15:03:22.222-0700|INFO|GlassFish10.0|GRIZZLY|_ThreadID=12;_ThreadName=httpWorkerThread-8080-1;|
      Processing SayController#hello (for 127.0.0.1 at 2007-08-10 15:03:22) [GET]
      |#]
      [#|2007-08-10T15:03:22.225-0700|INFO|GlassFish10.0|GRIZZLY|_ThreadID=12;_ThreadName=httpWorkerThread-8080-1;| Session ID: a78627d02071347f6fb5f0268fa47f18
      |#]
      [#|2007-08-10T15:03:22.227-0700|INFO|GlassFish10.0|GRIZZLY|_ThreadID=12;_ThreadName=httpWorkerThread-8080-1;| Parameters: {"action"=>"hello", "controller"=>"say"}
      |#]
      [#|2007-08-10T15:03:22.253-0700|INFO|GlassFish10.0|GRIZZLY|_ThreadID=12;_ThreadName=httpWorkerThread-8080-1;|Rendering say/hello
      |#]
      [#|2007-08-10T15:03:22.295-0700|INFO|GlassFish10.0|GRIZZLY|_ThreadID=12;_ThreadName=httpWorkerThread-8080-1;|Completed in 0.06500 (15 reqs/sec) | Rendering: 0.0 6300 (96%) | 200 OK [http://localhost/hello/say/hello]
      |#]

      The main point to notice here is that the Rails application request is served directly by the Grizzly connector.

This concludes all the steps required to run a simple JRuby on Rails application on GlassFish. If you want to run the same application using the WEBrick container, then follow the additional steps given below:

  1. In the directory "hello", start WEBrick by giving the command:

    jruby script/server

    The output of the command looks like:

    => Booting WEBrick...=> Rails application started on http://0.0.0.0:3000=> Ctrl-C to shutdown server; call with --help for options[2007-08-10 14:14:26] INFO WEBrick 1.3.1[2007-08-10 14:14:26] INFO ruby 1.8.5 (2007-06-07) [java][2007-08-10 14:14:26] INFO WEBrick::HTTPServer#start: pid=6336176 port=3000
  2. Open "http://localhost:3000/say/hello" in a browser window and it shows the message:

    Hello from Controller!

    The WEBrick console shows the following output:

    127.0.0.1 - - [10/Aug/2007:14:15:25 PDT] "GET /say/hello HTTP/1.1" 200 89- -> /say/hello127.0.0.1 - - [10/Aug/2007:14:15:27 PDT] "GET /favicon.ico HTTP/1.1" 200 0- -> /favicon.ico

In the process, I found Ruby on Rails Cheatsheet very handy for a quick summary of commands.

The NetBeans IDE provides a comprehensive support for Ruby code completion, refactoring, debugging, Rails support, support for RHTML files, code templates, unit test execution, shortcuts, and much more.

Technorati: jruby ruby glassfish grizzly jrubyonglassfish netbeans rubyonrails

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • email
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot
Related posts:
  1. TOTD #24: Getting Started with Rails 2.0.x in JRuby 1.0.3 and JRuby 1.1RC1
  2. JRuby 1.0.2 released – Improved Windows experience and Rails 1.2.5 support
  3. Announcing GlassFish Gem for Rails
  4. TOTD #14: How to generate JRuby-on-Rails Controller on Windows (#9893)
  5. TOTD # 74: JRuby and GlassFish Integration Test #5: JRuby 1.2.0 RC2 + Rails 2.x.x + GlassFish + Redmine

19 Comments »

  1. Good stuff, but why do you start WEBrick at the end? How is it releated to the Glassfish V3 deployment? Unless I’m missing something, I think that that step is unnecessary and should be removed or explained. Otherwise it might confuse new users.

    Comment by Igor — August 13, 2007 @ 9:48 am

  2. Igor, your comment is valid. I updated the entry to show how a standard Rails
    application can be "deployed".

    Comment by Arun Gupta — August 13, 2007 @ 1:26 pm

  3. Three things:

    1. There is no need to go to the trouble of installing another copy of jruby in the Glassfish directory.

    To use an existing jruby installation (e.g., c:\jruby), simply add the following line to "asenv.bat" in directory "glassfish\config\"

    set JRUBY.HOME=C:\jruby

    Then use "asadmin" in the normal manner to deploy to the path of an existing application.

    2. Shouldn’t it be

    jruby -S gem install rails ?

    3. Glassfish currently suffers from a rendering problem, e.g., it mangles ActiveScaffold. This effectively limits its usability to simple demos for the time being. (N.B. jruby itself renders AS perfectly)

    Comment by Raphael Gillett — August 14, 2007 @ 11:40 am

  4. Raphael,

    Thanks for your suggestions. I updated the blog entry to incorporate comments #1 & #2. For #3, can you provide more details ?

    Comment by Arun Gupta — August 14, 2007 @ 6:35 pm

  5. Arun,

    ActiveScaffold may be obtained from

    http://www.activescaffold.com

    It is a widely-used plugin that provides an ajax-based dynamic scaffold with a professional appearance.

    Download the Demo from the above site. First, try it out with Jruby using either Webrick or Mongrel. Then try it with Jruby using Glassfish.

    Webrick and Mongrel both render the Demo perfectly.

    However, Glassfish fails to pick up the CSS instructions and mangles the ajax.

    Comment by Raphael Gillett — August 14, 2007 @ 11:49 pm

  6. [Trackback] In the JRuby Hackday, Nick Sieger described the process models of a Rails application deployed using Traditional C-based Ruby on Mongrel, JRuby on Mongrel, and JRuby on GlassFish. In this blog entry I’m capturing a brain dump from him…

    Comment by Arun Gupta's Blog — August 20, 2007 @ 9:32 am

  7. Are you aware of plug-ins that would allow me to deploy a Django app either as a WAR or via direct invocation from the grizzly connector. If yes, any suggestion on which version of glassfish I should use? Thanks.

    Comment by Sashi — September 4, 2007 @ 4:18 pm

  8. [Trackback] UPDATE: Simplified steps for GlassFish V2 are available here and for V3 here. Follow up from here. In this post I show how a Rails app can talk to database. Here are the steps I followed: Deploy a RoR application…

    Comment by Arun Gupta's Blog — September 13, 2007 @ 8:55 pm

  9. [Trackback] UPDATE: Simplified steps for GlassFish V2 are available here and for V3 here. Earlier in a three-part series (part1, part2, part3) I showed how a JRuby application can be deployed on GlassFish. This screencast consolidates all the entries together and…

    Comment by Arun Gupta's Blog — September 13, 2007 @ 8:57 pm

  10. [Trackback] Jerome has been working on GlassFish gem for Rails. Read the interesting discussion on dev@glassfish. This blog announces a technology preview of this gem and describes the steps to try it out. Download GlassFish Gem from here. If you already…

    Comment by Arun Gupta's Blog — September 14, 2007 @ 6:13 am

  11. Hi Arun,

    thanks for your great article.

    All works good but I’m wondering about GlassFish concurrent requests handling, simply it doesn’t work.

    Test:

    jruby -S rails hello
    cd hello
    jruby script/generate controller engine home test

    edit app/controlelrs/engine_controller.rb

    class EngineController < ApplicationController
    def home
    i = 0
    while i < 15000000
    i += 1
    end
    render :text => "Game over !", :layout => false
    end

    def test
    render :text => "Test", :layout => false
    end
    end

    Now if I navigate to:

    http://localhost:8080/hello/engine/home

    and concurrently to:

    http://localhost:8080/hello/engine/test

    The second request is a blank page, no errors at all, only a blank page: why ?

    Seems that GlassFish could’nt handle concurrent requests.

    Can you try this and help me ?

    Thanks in advance.

    Have a nine day !

    Comment by Gianluca Tessarolo (Tex) — November 21, 2007 @ 12:06 am

  12. Gianluca, This issue is now resolved with an updated version of Gem as described at:

    http://blogs.sun.com/arungupta/entry/glassfish_v3_gem_updated

    Let me know if it does not solve problem.

    Comment by Arun Gupta — January 7, 2008 @ 9:13 pm

  13. Good work but:

    1. How can I configure more than 2 default Rails instances at gem glassfish startup ?

    2. If I have 2 Rails instances and 4 concurrent requests the 3rd, and 4th request return a blank page, not so good… (I think that a better solution could be wait until a free Rails instance wake up…)

    Many thanks for your support…

    Comment by Gianluca Tessarolo (Tex) — January 9, 2008 @ 7:03 am

  14. Gianluca,

    The number of Rails instance is hardcoded to 2 but I’ve already filed a enhancement request for this at:

    https://glassfish.dev.java.net/issues/show_bug.cgi?id=3968

    If you are interested, I request you to Vote for the issue :)

    The 3rd & 4th request return blank page for the same reason as earlier.

    -Arun

    Comment by Arun Gupta — January 9, 2008 @ 7:09 am

  15. Hi Arun,

    I voted right now for the issue.

    For my second complain I want to say that even if I can have more than 2 Rails instances, return a blank page because there are not enough free instances is a wrong thing (I cannot accept that a ‘nth’ user read a blank page just beacause there are not enough free rails instances…), a better solution could be wait for a free rails instance…

    Comment by Gianluca Tessarolo (Tex) — January 9, 2008 @ 7:35 am

  16. The second complain is already lodged as an issue at: https://glassfish.dev.java.net/issues/show_bug.cgi?id=3966

    Comment by Arun Gupta — January 9, 2008 @ 7:46 am

  17. Ok Arun,

    thank you very much.

    Keep up the good work ! (I hope this is a correct english expression for "Buon lavoro !")…

    Comment by Gianluca Tessarolo (Tex) — January 9, 2008 @ 8:55 am

  18. For my second complain I want to say that even if I can have more than 2 Rails instances, return a blank page because there are not enough free instances is a wrong thing (I cannot accept that a ‘nth’ user read a blank page just beacause there are not enough free rails instances…), a better solution could be wait for a free rails instance…

    Comment by LAPTOP BATTERY — November 26, 2008 @ 9:59 pm

  19. Yep, that’s the approach taken by the recent release of gem. Read more at:

    http://wiki.glassfish.java.net/Wiki.jsp?page=JRuby

    Comment by Arun Gupta — November 28, 2008 @ 7:23 am

RSS feed for comments on this post. TrackBack URL

Leave a comment

The views expressed on this blog are my own and do not necessarily reflect the views of Oracle.
Powered by WordPress