Rails 2.1 was released earlier this month. GlassFish provides a complete development/deployment environment for Rails applications. Some of the main reasons for using GlassFish (instead of WEBrick or Mongrel) are:
- Identical Development and Deployment environment
- Multiple Applications can be deployed on one Container
- Multiple requests can be handled by a single application (with no extra configuration)
- Out-of-the-box Clustering, Load Balancing and High Availability
- Database connection pooling
- Co-hosting Rails and Java EE applications
More details are available in Rails powered by the GlassFish Application Server. This blog provides detailed instructions to get started with Rails 2.1 on GlassFish.
- Install Rails 2.1 in a JRuby 1.1.2 installation:
~/testbed/rails21/jruby-1.1.2 >bin/jruby -S gem install rails –no-ri –no-rdoc
JRuby limited openssl loaded. gem install jruby-openssl for full support.
http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL
Updating metadata for 253 gems from http://gems.rubyforge.org/
…………………………………………………………………….
…………………………………………………………………….
…………………………………………………………………….
…………….
complete
Bulk updating Gem source index for: http://gems.rubyforge.org/
Successfully installed activesupport-2.1.0
Successfully installed activerecord-2.1.0
Successfully installed actionpack-2.1.0
Successfully installed actionmailer-2.1.0
Successfully installed activeresource-2.1.0
Successfully installed rails-2.1.0
6 gems installed - Create a new Rails app as:
~/testbed/rails21/jruby-1.1.2/samples/rails >../../bin/jruby -S rails -d mysql runner
create
create app/controllers
create app/helpers
create app/models
create app/views/layouts
create config/environments
create config/initializers
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/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 config/initializers/inflections.rb
create config/initializers/mime_types.rb
create config/initializers/new_rails_defaults.rb
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/console
create script/dbconsole
create script/destroy
create script/generate
create script/performance/benchmarker
create script/performance/profiler
create script/performance/request
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/422.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 - Start GlassFish as:
~/testbed/glassfish/v3/tp2/glassfishv3-tp2/glassfish >java -DJRUBY_HOME=/Users/arungupta/testbed/rails21/jruby-1.1.2 -jar modules/glassfish-10.0-tp-2-SNAPSHOT.jar
Jun 17, 2008 6:14:21 PM com.sun.enterprise.glassfish.bootstrap.ASMain main
INFO: Launching GlassFish on HK2 platform
Jun 17, 2008 6:14:22 PM com.sun.enterprise.v3.services.impl.GrizzlyProxy start
INFO: Listening on port 8080
Jun 17, 2008 6:14:22 PM com.sun.enterprise.v3.services.impl.GrizzlyProxy start
INFO: Listening on port 8181
Jun 17, 2008 6:14:22 PM com.sun.enterprise.v3.services.impl.GrizzlyProxy start
INFO: Listening on port 4848
Jun 17, 2008 6:14:22 PM com.sun.enterprise.v3.admin.adapter.AdminConsoleAdapter setContextRoot
INFO: Admin Console Adapter: context root: /admin
Jun 17, 2008 6:14:22 PM com.sun.enterprise.v3.server.AppServerStartup run
INFO: Glassfish v3 started in 974 ms - Deploy the newly created application as:
~/testbed/rails21/jruby-1.1.2/samples/rails >~/testbed/glassfish/v3/tp2/glassfishv3-tp2/glassfish/bin/asadmin deploy runner
Command deploy executed successfully.and the GlassFish console shows:
Jun 17, 2008 6:14:41 PM com.sun.enterprise.rails.RailsDeployer registerAdapter
INFO: Loading application runner at /runner
Jun 17, 2008 6:14:41 PM
INFO: Starting Rails instances
Jun 17, 2008 6:14:49 PM
SEVERE: JRuby limited openssl loaded. gem install jruby-openssl for full support.
http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL
Jun 17, 2008 6:14:51 PM com.sun.grizzly.jruby.RubyObjectPool$1 run
INFO: Rails instance instantiation took : 9066ms
Jun 17, 2008 6:14:51 PM com.sun.enterprise.v3.deployment.DeployCommand execute
INFO: Deployment of runner done is 9168 msThe default page at “http://localhost:8080/runner” shows
- Generate a Scaffold as described in TOTD #28
~/testbed/rails21/jruby-1.1.2/samples/rails/runner >../../../bin/jruby -S rake db:create
(in /Users/arungupta/testbed/rails21/jruby-1.1.2/samples/rails/runner)
~/testbed/rails21/jruby-1.1.2/samples/rails/runner >../../../bin/jruby script/generate scaffold distance miles:float run_at:datetime
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/distances
exists app/views/layouts/
exists test/functional/
exists test/unit/
exists public/stylesheets/
create app/views/distances/index.html.erb
create app/views/distances/show.html.erb
create app/views/distances/new.html.erb
create app/views/distances/edit.html.erb
create app/views/layouts/distances.html.erb
create public/stylesheets/scaffold.css
create app/controllers/distances_controller.rb
create test/functional/distances_controller_test.rb
create app/helpers/distances_helper.rb
route map.resources :distances
dependency model
exists app/models/
exists test/unit/
exists test/fixtures/
create app/models/distance.rb
create test/unit/distance_test.rb
create test/fixtures/distances.yml
create db/migrate
create db/migrate/20080618012326_create_distances.rb
~/testbed/rails21/jruby-1.1.2/samples/rails/runner >../../../bin/jruby -S rake db:migrate
(in /Users/arungupta/testbed/rails21/jruby-1.1.2/samples/rails/runner)
== 20080618012326 CreateDistances: migrating ==================================
— create_table(:distances)
-> 0.0080s
== 20080618012326 CreateDistances: migrated (0.0090s) ========================= - Edit line 44 of “config/environment.rb” to change the default timezone from “UTC” to “Pacific Time (US & Canada)”. The updated line looks like:
config.time_zone = ‘Pacific Time (US & Canada)’ - Creating a new scaffold entry shows the following screen:

Notice how default timezone is selected using Rails 2.1 Time Zone Support. After creating couple of entries (as described in TOTD #28), the page at “http://localhost:8080/runner/distances” looks like:

Notice how “-0700″ is shown which is the offset for US Pacific Time during daylight savings.
The same application can also be easily deployed on GlassFish v3 Gem. Let’s see how:
- Install GlassFish v3 Gem as:
~/testbed/rails21/jruby-1.1.2 >bin/jruby -S gem install glassfish
JRuby limited openssl loaded. gem install jruby-openssl for full support.
http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL
Updating metadata for 13 gems from http://gems.rubyforge.org/
………….
complete
Successfully installed glassfish-0.2.0-universal-java
1 gem installed - Deploy the previously created Rails application as:
~/testbed/rails21/jruby-1.1.2/samples/rails >../../bin/jruby -S glassfish_rails runner
Jun 17, 2008 6:39:37 PM com.sun.enterprise.glassfish.bootstrap.ASMain main
INFO: Launching GlassFish on HK2 platform
Jun 17, 2008 6:39:37 PM com.sun.enterprise.glassfish.bootstrap.ASMainHK2 findDerbyClient
INFO: Cannot find javadb client jar file, jdbc driver not available
Jun 17, 2008 6:39:38 PM com.sun.enterprise.v3.services.impl.GrizzlyProxy start
INFO: Listening on port 3000
Jun 17, 2008 6:39:38 PM com.sun.enterprise.v3.services.impl.GrizzlyEmbeddedHttpConfigurator configureSSL
WARNING: pewebcontainer.all_ssl_protocols_disabled
Jun 17, 2008 6:39:38 PM com.sun.enterprise.v3.services.impl.GrizzlyEmbeddedHttpConfigurator configureSSL
WARNING: pewebcontainer.all_ssl_ciphers_disabled
Jun 17, 2008 6:39:38 PM com.sun.enterprise.v3.services.impl.GrizzlyProxy start
INFO: Listening on port 3131
Jun 17, 2008 6:39:38 PM com.sun.enterprise.v3.services.impl.GrizzlyProxy start
INFO: Listening on port 3838
Jun 17, 2008 6:39:39 PM com.sun.enterprise.v3.admin.adapter.AdminConsoleAdapter setContextRoot
INFO: Admin Console Adapter: context root: /admin
Jun 17, 2008 6:39:39 PM com.sun.enterprise.rails.RailsDeployer registerAdapter
INFO: Loading application runner at /
Jun 17, 2008 6:39:39 PM
INFO: Starting Rails instances
Jun 17, 2008 6:39:44 PM
SEVERE: JRuby limited openssl loaded. gem install jruby-openssl for full support.
http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL
Jun 17, 2008 6:39:45 PM com.sun.grizzly.jruby.RubyObjectPool$1 run
INFO: Rails instance instantiation took : 6710ms
Jun 17, 2008 6:39:45 PM com.sun.enterprise.v3.server.AppServerStartup run
INFO: Glassfish v3 started in 8053 msThe page at “http://localhost:3000/runner” looks like:
Let us know on GlassFish Webtier Forum or Webtier Alias if you tried any of your Rails application on GlassFish. Please file bugs at GlassFish Issue Tracker (“V3″ as “Found in Version:” and “jruby” as “Subcomponent:”) if your app is not working.
Related posts:
I was wondering how you would recommend deployment configuration to be made, for say a glassfish/rails application.
Like, how do you handle install-specific configuration (smtp server, symlinks to dirs with resources, etc).
I guess glassfish has excellent ways to monitor it, but being a rails developer, I am not that knowledgeable about this.
Any pointers are immensely appreciated.
Comment by albert ramstedt — June 22, 2008 @ 3:42 pm
[Trackback] My primary development machine is Mac OS Leopard on Intel Core 2 Duo (read osxtips). But the beauty of virtualization is that I can run multiple Operating Systems on a single powerful machine. And what better virtualization product than…
Comment by Arun Gupta's Blog — June 24, 2008 @ 6:22 am
[Trackback] Vivek released 0.3.0 version of GlassFish Gem for Rails (a total of 11 issues fixed and resolved – RubyForge & GlassFish Issue Tracker) earlier. But that caused a regression on #4228. And so matching the agility expected by Rails developers, h…
Comment by Arun Gupta's Blog — June 25, 2008 @ 6:42 am
Albert, The beauty of Rails on GlassFish is that your application can be deployed as is. If it does not, that’s a bug! If you provide more details then I can help you better.
Comment by Arun Gupta — June 25, 2008 @ 5:21 pm
Well, I know it can be deployed. Only what I dont know is what java people use instead of capistrano. Say an application install needs specific configuration like mail server config, symlinks to networked drived mounts.. etc.
What I would want is to run a script post-war unpack that copies files and creates the symlinks. Maybe you could write capistrano deployment recipes for it though. But maybe there is a convention for deploying java-apps?
Comment by albert — June 26, 2008 @ 1:12 am
Albert,
I don’t think there is an equivalent. Part of this stems from the fact that in the Java world, it’s more likely the case that mainstream Java developers just throw the war file over the wall to the operations team, who handle all the server setup.
Capistrano comes from this new world where everybody is part of the ops team. I think the best answer to date is to continue to use cap. In the meanwhile we will make sure there are a solid set of deployment recipes for deploying war files and doing the couple Java-y things that are different than a regular Ruby deployment.
Comment by Arun Gupta — June 30, 2008 @ 10:58 am