Skip to main content

Plug-in any Ruby/Rack based framework to GlassFish

Posted by vivekp on April 3, 2009 at 10:33 PM PDT

as well as href="">GlassFish
v3 supports href="">Rack.
Rack provides an interface to plugin a Ruby web framework with a web sever.
Similar to Python href="">WSGI.
This means that any ruby based framework that can talk Rack can be
simply deployed on GlassFish.

Here is how you can do it on GlassFish gem:

Insall JRuby

Install JRuby from href="">here.
Setup JRUBY_HOME to the install location and add JRUBY_HOME/bin to your

Install GlassFish gem

$ gem install glassfish

Now, lets write a trivial app and save it in file: myapp.rb

class MyApp
def self.Run
"Hello World"

Write a rack-up script and save it in

require 'rubygems'
gem 'rack'
require 'rack/handler/grizzly'
require 'myapp'

app = proc do |env|
resp = MyApp.Run
200, # Status code
{ # Response headers
'Content-Type' => 'text/html',
'Content-Length' => resp.length,
[resp] # Response body

That is pretty much your sample Ruby
application and the Rack-up script to run on GlassFish. Above require
handler available to Rack.

To run it, simply run glassfish
in side the directory where myapp.rb and are placed.

% glassfish
Starting GlassFish server at: in development environment...
Writing log messages to: /Users/vivek/dev/foo/log/development.log.
Press Ctrl+C to stop.

Now access, http://localhost:3000
in your browser to access your app.

rackapp.png src=""
height="208" width="551">

If you are running GlassFish v3
server and want to deploy this app, you would simply use the same
asadmin command from inside the application directory:

$ asadmin deploy .
GlassFish gem as well as GlassFish v3 server can auto-detect Rails,
Merb and Sinatra applications. You can use the above mentioned
technique to plugin other frameworks. For reference also see Jacob's href="">blog
where he explains how Sintra application is plugged in to GlassFish.

Related Topics >>


I've been tryig to get this working, but it fails right away with this: SEVERE: uninitialized constant Rack::VERSION Any idea why? jruby 1.4.0dev (ruby 1.8.6p287) (2009-07-13 6586) (Java HotSpot(TM) Client VM 1.6.0_07) [i386-java] glassfish (0.9.5), rack (1.0.0)

OK, success. I added this line to the .ru file: require 'jruby/rack/grizzly_helper' (Oh and I still need to define Rack::VERSION as above.)

OK, maybe not so simple. I added this to the top of my test.rb file: module Rack VERSION = '1.0' end Now I get this: SEVERE: uninitialized constant JRuby::Rack from :1 com.sun.grizzly.jruby.rack.RackInitializationException: uninitialized constant JRuby::Rack from :1 at com.sun.grizzly.jruby.rack.DefaultRackApplicationFactory.createApplication(DefaultRackApplicationFactory.ja va:169) at com.sun.grizzly.jruby.rack.DefaultRackApplicationFactory.newApplication( 69) Something is missing?

I also get the uninitialized constant Rack::VERSION error. As far as I can see, this is because the jruby-rack.jar is not being found. Where should it be placed for the glassfish gem to use it?