Glassfish v3 with Merb and Warbler

It’s been a looong time but this I want to share with you.
For a project I’m currently working on we needed something really really fast. Rails with Ruby MRI was out of the question so we went for JRuby and Merb. This particular piece of the project is ideally suited for Merb as we are using it as a backend system (webservice) only.

So you want JRuby because it is blazingly fast (at least compared to MRI)? Fine. But how to deploy the bugger? We quickly decided on Glassfish an open source application server made and maintained by Sun. Moreover this lovely server had a gem which you can use to fire up a Glassfish server. Easy as pie and within no time at all we were developing our Merb app on Glassfish.

I was charged with the task to deploy the app to the server and that’s when the trouble began. First off I installed JRuby 1.1.5 (in /usr/local/jruby) and installed the Glassfish gem (v0.9.0). I had no idea how to monitor and fire up the gem but that was for later. Installed the glassfish and merb-core (!) gem ($ jruby -S gem install glassfish merb-core). Don’t install Merb! This won’t work as it depends on a gem which tries to build native extensions.

Still manually copying over the source of our app I ran the glassfish command only to be greeted with an Activator error. Took me a while to figure out but this appears to be a proper bug. The maintainer is aware of the issue and will fix it in the 0.9.1 release of the gem. No good for me so onward.

If the gem doesn’t work than the true Glassfish server might do the trick. I come from a Java background but have been developing Rails/Ruby now for over a year and I quite forgot how incredibly verbose the Java world is. After reading for an odd couple of hours or so. I went for the installer of the Glassfish v3 prelude. The v3 release does not require you to package up your app in a WAR file which sounded very good. Unfortunately the installer died with some unclear error messages and I just downloaded the zip and unpacked it in /usr/local/glassfish.

More dark clouds ahead! I have no idea how this stuff works and although there are a couple of Rails examples non worked for our Merb app. :S Starting the server with jruby was already a challenge, in the end

java -J-server -Xmx256m -Djruby.home=/usr/local/jruby-1.1.5/ -jar /usr/local/glassfishv3-prelude/glassfish/modules/glassfish.jar

did the trick. But how to get Glassfish to accept my ‘exploded’ (eg non WAR/EAR) Merb app? I still have no idea. Enter Warbler.

This charming little gem turns a Rails, Merb or Rack app into a WAR file ready to be deployed. I’ll leave out all the errors and just to it. First off edit the config.rb file of the warbler gem (found in /usr/local/jruby-1.1.5/lib/ruby/gems/1.8/gems/warbler-0.9.11/lib/warbler/), got to the auto_detect_merb method and change the line:

@gems[”merb”] = Merb::VERSION

to:

@gems[”merb-core”] = Merb::VERSION

This will enable warbler to find the correct gem. Installing the gem ‘merb’ was not an option for JRuby!

Second edit the merb-core gemspec (found in /usr/local/jruby-1.1.5/lib/ruby/gems/1.8/specifications) and find all references to webrat and remove them. Webrat depends on Nokogiri which in turn has native dependencies… etc etc.

Third replace the jruby and jruby-rack version in /usr/local/jruby-1.1.5/lib/ruby/gems/1.8/gems/warbler-0.9.11/lib with the hottest flavours (JRuby-Rack: http://kenai.com/downloads/jruby-rack/, JRuby: http://dist.codehaus.org/jruby/)

We are now ready for WAR.

Config the war file!

$ jruby -S warble config

Edit the config/warble.rb file. Remove the ‘vendor’ and ‘tmp’ directory from the config.dirs directive. LEAVE all references to config.gem alone. This does not work as expected config.gem -= “rails” does not remove the rails dependency. On the contrary. The config.webxml.booter needs to say :merb. Save-close-etc.

Create the war file!

$ jruby -S warble war

Tell the now running Glassfish server to deploy it with

$ /urs/local/glassfish/bin/asadmin deploy your_app.war

The server is running on port localhost:8080/your_app
May this help.

8 Responses to “Glassfish v3 with Merb and Warbler”

  1. Jacob Kessler Says:

    If you want to deploy a non-WAR merb app on v3 prelude, you should be able to use asadmin, just like the other supported containers. Run “asadmin /path/to/your/app” and it should correctly deploy.

  2. admin Says:

    @Jacob I don’t believe that works for Merb apps. I seem to recall that Glassfish in that scenario complaint about missing source files (config/environment.rb). And I saw in the log output some RailsDeployer calls, hardly seems Merb geared.

  3. Kris Says:

    Hi, which version of merb are you running? I’m following your instructions, however; Merb keeps failing upon deployment in GF with:

    org.jruby.rack.RackInitializationException: undefined method `frozen!’ for Merb:Module
    from /Users/Kris/workspace/glassfishv3-prelude/glassfish/domains/domain1/generated/jsp/gtg/loader/jruby/rack/merb.rb:20:in `load_environment’
    from /Users/Kris/workspace/glassfishv3-prelude/glassfish/domains/domain1/generated/jsp/gtg/loader/jruby/rack/merb.rb:80:in `new’
    from :3
    from /Users/Kris/workspace/glassfishv3-prelude/glassfish/domains/domain1/generated/jsp/gtg/loader/rack/builder.rb:22:in `instance_eval’
    from /Users/Kris/workspace/glassfishv3-prelude/glassfish/domains/domain1/generated/jsp/gtg/loader/rack/builder.rb:22:in `initialize’
    from :3

    I’m using GFv3-prelude, and merb-core 1.0.1 gem at the moment. I will try using the latest/greatest jruby-rack and runtime as well. Thanks for the tutorial… I’d love to get this working as it makes deployment to server very easy.

  4. Jacob Kessler Says:

    I must be too used to playing around with trunk builds, then. It works with the version of Glassfish I’m using, so at the very least it will be in the next Glassfish release. Gem 0.9.1 should also be out today, if that helps.

  5. admin Says:

    @Kris You are running into the ‘out dated JRuby-Rack’ problem. If you upgrade jruby-rack to 0.9.3 you problem will go away. I was using Merb 1.0.3 at the time.

  6. Kris Says:

    Man… I can’t figure out what I’m doing wrong here. After the following steps:

    - updated the jruby merb spec to 1.0.3 just now

    - copied the latest jruby-rack.jar (0.9.3) into the warble gems lib folder… also moved the 1.5 jruby jar into here as well since it has 1.4 from the gem. If I “jar tf | grep *.jar” I see the proper libs:

    WEB-INF/lib/jruby-rack-0.9.3.jar
    WEB-INF/lib/jruby.jar

    - create a simple app with “jruby -S merb-gen core demo”, no orm/jdbc to worry about so that i only have bare bones merb app (if i fire this up in jruby -S merb it loads fine)

    - jruby -S warble config

    - edit the warble.rb config file to remove invalid directories

    - jruby -S warble war which creates the jar as expected

    - start up GF: “asadmin start-domain”

    - using the web frontend at localhost:4848 to deploy. When I deploy the jar, I see in glassfish/domains/domain1/log/server.log file the error “javax.servlet.ServletException: org.jruby.rack.RackInitializationException: no such file to load — time”

    The full stack trace is here: http://pastie.org/331932

    I figured I’d post the walk through in case I’m doing something wrong. I’m running on OS X 10.5, not that should matter too much. I would love to get this working.

    I’m trying to do a POC to sell jruby + merb as there is a requirement to use a SQL server DB. It’s a deep rooted personal desire to *not* do this on Windows! I was able to integrate merb to SQL Server nicely with Sequel’s JDBC support, but I need to work out the deployment better beforehand.

    Thanks again for posting this in the first place… it was tremendously helpful and timely!

  7. admin Says:

    @kris I had exactly the same problem. The problem is that GF can’t find your JRuby install. I fixed the problem by starting GF with the -Djruby.home=/path/to/your/jruby
    In my original post I had this wrong (did -DJRUBY_HOME) but since corrected it.
    Let me know if this works!

  8. LOTD #15: Deploying Merb application on GlassFish v3 using Warbler « Miles to go … Says:

    […] GlassFish v3 with Merb and Warbler explains how to use Warbler to deploy a Merb application on GlassFish v3. Here is a quote from the blog: […]

Leave a Reply