Miles to go …

April 14, 2008

Rails and Java EE integration – Native Rails on GlassFish v3

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

The last part of this tri-series blog (Part 1, Part 2) will show how a Rails application can be deployed on GlassFish – without the need of Goldspike, Warbler or any other gem or plugin. Yes, that’s a native Rails app deployment on GlassFish v3.

GlassFish v3 is next version of GlassFish v2 and the focus is modularization, enablement of non-Java EE containers and modularity – download b09.

Rails powered by GlassFish provides all the details on why GlassFish provides an industry-grade and functionally-rich Application Server.

Now detailed steps:

  1. Using JRuby 1.1 (installed with Rails), create a Rails app “railsee3″ as:

    ~/testbed/jruby-1.1/samples/rails >../../bin/jruby -S rails railsee3
          create 
          create  app/controllers
          create  app/helpers
          create  app/models
          . . .
          create  log/production.log
          create  log/development.log
          create  log/test.log
  2. Add Servlet descriptors
    1. Create a new directory “WEB-INF”, and a new file “web.xml” in that directory using the following contents:

      <!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>
              <servlet>
                      <servlet-name>HelloServlet</servlet-name>
                      <servlet-class>server.HelloServlet</servlet-class>
              </servlet>
              <servlet-mapping>
                      <servlet-name>HelloServlet</servlet-name>
                      <url-pattern>/hello</url-pattern>
              </servlet-mapping>
      </web-app>
    2. Create a new file “sun-web.xml” in “WEB-INF” using the following contents:
      <?xml version=”1.0″ encoding=”UTF-8″?>
      <!DOCTYPE sun-web-app PUBLIC “-//Sun Microsystems, Inc.//DTD Application Server 9.0 Servlet 2.5//EN” “http://www.sun.com/software
      /appserver/dtds/sun-web-app_2_5-0.dtd”>
      <sun-web-app error-url=”">
        <context-root>/servlet</context-root>
        <class-loader delegate=”true”/>
      </sun-web-app>
    3. Create a new directory “WEB-INF/lib”.
  3. Create and Copy Servlet
    1. Create a Java library with Servlet code as explained in bullet #5 here.
    2. Copy “HelloServlet.jar” from “dist” directory of NetBeans project to “WEB-INF/lib” directory.
  4. Configure JRuby-on-Rails in GlassFish – Edit “config/asenv.conf” in GlassFish directory and specify JRUBY_HOME as the last line:
    JRUBY_HOME=”/Users/arungupta/testbed/jruby-1.1″

  5. Deploy the Rails application as:

  6. ~/testbed/jruby-1.1/samples/rails >~/testbed/glassfish/v3/p2b9/glassfish/bin/asadmin deploy –force=true railsee3
    railsee3 deployed successfully
    Command deploy executed successfully.
  7. The bundled Servlet is now accessible at “http://localhost:8080/servlet/hello”. The default browser output looks like:

    And passing a parameter to the URL as “http://localhost:8080/railsee3/hello?name=Arun” shows the output as:

With this, your Java EE Servlet is now bundled with your Rails application deployed on GlassFish v3.

Now, lets add Controller and View to Rails application and invoke this servlet from there to show complete integration with Rails.

  1. Create a new Controller and View as

    ~/testbed/jruby-1.1/samples/rails/railsee3 >../../../bin/jruby script/generate controller home index
    JRuby limited openssl loaded. gem install jruby-openssl for full support.
    http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL
          exists  app/controllers/
          exists  app/helpers/
          create  app/views/home
          exists  test/functional/
          create  app/controllers/home_controller.rb
          create  test/functional/home_controller_test.rb
          create  app/helpers/home_helper.rb
          create  app/views/home/index.html.erb
  2. Change the generated controller in “app/controllers/home_controller.rb” to:
    class HomeController < ApplicationController

    include Java

      def index
            url = java.net.URL.new(“http://localhost:8080/servlet/hello”);
            conn = url.open_connection;
       &
    nbsp;    reader = java.io.BufferedReader.new(java.io.InputStreamReader.new(conn.get_input_stream));
            @servlet_output = “”;
            input_line = reader.read_line;
            while input_line != nil
                    @servlet_output << input_line;
                    input_line = reader.read_line;
            end
            reader.close;
      end
    end

  3. Change the generated view in “app/views/home/index.rhtml.erb” to:
    <h1>Home#index</h1>
    <p>Find me in app/views/home/index.html.erb</p>

    <%= @servlet_output %>

  4. Re-deploy the Rails application as shown in bullet # 5 above and “http://localhost:8080/railsee3/home/index” shows the output as shown:

So this blog explained how a Rails application can be deployed on GlassFish v3 without the need of any gems like Warbler or plugin like Goldspike – total native deployment!

In summary, the tri-part blog showed the deployment models for a Rails application on GlassFish. Each model showed how a Java EE 5 Servlet can be co-bundled with Rails application and invoked from Rails view:

Technorati: rubyonrails netbeans glassfish v3 javaee5 servlets jruby ruby warbler

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • email
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot
Related posts:
  1. Rails and Java EE integration – Warbler instead of Goldspike
  2. Rails and Java EE integration – Servlet co-bundled and invoked from Rails
  3. JRuby-on-Rails deployed on GlassFish – Success Story
  4. WAR-based Packaging and Deployment of Rails on GlassFish – Goldspike, RailServlet, Warbler, Rack, …
  5. TOTD # 71: JRuby and GlassFish Integration Test #2: JRuby 1.2.0 RC1 + Rails 2.2.x + GlassFish v3 Prelude

15 Comments »

  1. If I do not have the need to interface my rails app with any additional servlets (HelloServlet etc.) what configuration do I need to do in order to deploy the rails app to glassfish v3?

    Comment by Brandon — April 14, 2008 @ 8:07 am

  2. [Trackback] The last part of this tri-series blog (Part 1, Part 2) will show how a Rails application can be deployed on GlassFish – without the need of Goldspike, Warbler or any other gem or plugin. Yes, that’s a native…

    Comment by Arun Gupta's Blog — April 14, 2008 @ 8:20 am

  3. Brandon,

    If you need to deploy only a Rails application, with no interface with any Java EE application, then bullets # 1, 4 & 5 will give you that.

    Comment by Arun Gupta — April 14, 2008 @ 10:22 am

  4. Can you tell anything about performance between native ruby and jruby?

    We’ve tried to run ruby on a T2000 but the single thread performance is horribly bad for our application (although the T2000 is a great box overall).

    IMHO the problem lies in the single-threaded character of mongrel and ruby being an interpreted language.

    Comment by Mika — April 14, 2008 @ 12:59 pm

  5. Mika, Charlie Nutter ran some live performance benchmarks between JRuby1.1 trunk (now released) and Ruby 1.8.6. The results are reported at:

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

    The gist is "JRuby 1.0 was 2x slower than Ruby 1.8.6, JRuby 1.1 Beta1 2x faster, JRuby trunk 2-5x faster and often faster than 1.9". And the JRuby performance has only gone up since Beta1.

    Comment by Arun Gupta — April 14, 2008 @ 1:02 pm

  6. Arun,

    Thank you for this exciting post.

    For those of us who prefer to code without an IDE, could you provide a link to the relevant jar?

    Could you also sketch out the minimum contents of the jar needed to get things running?

    Comment by Raphael — April 17, 2008 @ 1:24 am

  7. Raphael,

    The jar contains minimal content:

    ~/NetBeansProjects/HelloServlet/dist >jar tf HelloServlet.jar
    META-INF/
    META-INF/MANIFEST.MF
    server/
    server/HelloServlet.class

    and is now available at:

    http://blogs.sun.com/arungupta/resource/ror/HelloServlet.jar

    Comment by Arun Gupta — April 17, 2008 @ 10:29 am

  8. Arun,

    asenv.conf seems not to pick up JRUBY_HOME.

    On Win XP deploy gives:

    FAILURE : Exception while deploying the app : java.lang.RuntimeException: JRuby installation not found at C:\glassfish\modules\jruby
    Command deploy failed.

    Comment by Raphael — April 19, 2008 @ 1:26 pm

  9. OK

    In order for Glassfish to pick up JRuby, add to "asenv.bat" (not to "asenv.conf") the following (no quotes)
    set JRUBY.HOME=C:\jruby

    Note the dot in JRUBY.HOME

    However, success is short-lived as a cascade of new errors indicates that Glassfish has problems in a Windows environment

    C:\jruby>#!/usr/bin/env jruby
    ‘#!’ is not recognized as an internal or external command,
    operable program or batch file.

    Comment by Raphael — April 19, 2008 @ 2:42 pm

  10. Raphael,

    I tried this on Mac environment and it works there. Will try on Windows when I get to work tomorrow.

    -Arun

    Comment by Arun Gupta — April 20, 2008 @ 9:06 am

  11. Raphael,

    I tried GlassFish v3 10a (http://download.java.net/glassfish/v3-tp2/promoted/glassfish-v3-preview2-b10a.zip) with JRuby 1.1 and it worked. The only change is to set JRUBY_HOME (note "_") in asenv.bat as:

    set JRUBY_HOME=C:\testbed\jruby-bin-1.1\jruby-1.1

    and I could deploy the basic Rails app (without any Servlet).

    -Arun

    Comment by Arun Gupta — April 29, 2008 @ 9:32 am

  12. [Trackback] Redmine is a flexible project management web application written using Ruby on Rails framework. The feature list is pretty comprehensive from the usual suspects like multiple projects, role-based control, forums/wikis/SCM for each project to enterpris…

    Comment by Arun Gupta's Blog — May 23, 2008 @ 6:10 am

  13. [Trackback] JRuby 1.1.2 is now released – download here! The highlights are: Startup time drastically reduced YAML symbol parsing &gt;100x faster Performance, threading, and stack depth improvements for method calls Fixed several nested backref problems Fixed bad…

    Comment by Arun Gupta's Blog — May 29, 2008 @ 7:16 am

  14. how do i create ,deploy and run webservice on glassfish using Eclipse ?

    Comment by priyanka — October 23, 2008 @ 6:05 am

  15. Priyanka,

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

    describes how to create a Web service using Eclipse and deploy on GlassFish.

    Comment by Arun Gupta — October 23, 2008 @ 6:09 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