Monday April 14, 2008
Rails and Java EE integration - Native Rails on GlassFish v3
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:
|
~/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 |
| <!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> |
| <?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> |
| JRUBY_HOME="/Users/arungupta/testbed/jruby-1.1" |
| ~/testbed/jruby-1.1/samples/rails
>~/testbed/glassfish/v3/p2b9/glassfish/bin/asadmin
deploy --force=true railsee3 railsee3 deployed successfully Command deploy executed successfully. |


| ~/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 |
| class
HomeController < ApplicationController include Java def index url = java.net.URL.new("http://localhost:8080/servlet/hello"); conn = url.open_connection; 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 |
| <h1>Home#index</h1> <p>Find me in app/views/home/index.html.erb</p> <%= @servlet_output %> |

Posted by Arun Gupta in web2.0 | Comments[13]
|
|
|
|
Today's Page Hits: 6273
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?
Posted by Brandon on April 14, 2008 at 08:07 AM PDT #
Posted by Arun Gupta's Blog on April 14, 2008 at 08:20 AM PDT #
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.
Posted by Arun Gupta on April 14, 2008 at 10:22 AM PDT #
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.
Posted by Mika on April 14, 2008 at 12:59 PM PDT #
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.
Posted by Arun Gupta on April 14, 2008 at 01:02 PM PDT #
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?
Posted by Raphael on April 17, 2008 at 01:24 AM PDT #
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
Posted by Arun Gupta on April 17, 2008 at 10:29 AM PDT #
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.
Posted by Raphael on April 19, 2008 at 01:26 PM PDT #
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.
Posted by Raphael on April 19, 2008 at 02:42 PM PDT #
Raphael,
I tried this on Mac environment and it works there. Will try on Windows when I get to work tomorrow.
-Arun
Posted by Arun Gupta on April 20, 2008 at 09:06 AM PDT #
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
Posted by Arun Gupta on April 29, 2008 at 09:32 AM PDT #
Posted by Arun Gupta's Blog on May 23, 2008 at 06:10 AM PDT #
Posted by Arun Gupta's Blog on May 29, 2008 at 07:16 AM PDT #