Sinatra on Java
With JRuby and Warbler it’s possible to get Sinatra, or any WebApp based on Rack, running on a myriad of different Java application servers. There are of course gotchas when it comes to using Warbler with the many different app servers, so this is a definitive guide to everything you have to do to get a simple Sinatra app running on the various application servers. Why Sinatra?
There are examples of how to get Rails running on Tomcat and Websphere floating around the web, but I find Rails overkill for small projects. With that in mind, it’s worth looking at how to get Sinatra running on java application servers. Besides the weight of rails, Sinatra is a nice, easy to learn framework.
Start by installing Sinatra and Warbler. You don’t have to be using JRuby to install Warbler, the install will download a gem of the jruby jars.
Install Sinatra and Warbler
Lets start by installing the required gems.
$ sudo gem install sinatra warbler haml
Haml isn’t strictly required, but the template I’m going to use has views generated in haml, so if you’re following the tutorial closely you’ll want to install it.
Create a project folder (and structure)
I usually keep a sinatra project template handy. So I’m going to clone my existing template off github. You can create a more minimal example than the one I’ll download, this will get the job done though.
$ git clone git://github.com/vertis/sinatra-example.git deploy_test
TODO: Details about the project
Check that our page is displayed
$ rackup
Go to http://localhost:9292/ and you should see our default page.
Generate the warble config
$ mkdir config && warble config
Lets look at the config file that was generated:
If you tried packaging and installing this now, it would fail miserably because, the ‘init.rb’ file would not be included. The generated warble.rb only includes the following
config.dirs = %w(app config lib log vendor tmp)
In addition to this the gems that we installed above would not be install. Here is same config with the lines we need (and the other cruft removed).
Package up the War file
$ warble
From here on in we’ll be looking at any gotchas, when deploying to the different Application Servers.
Deploy to Glassfish and test (effort: moderate - working: yes)
You can get Suns open source application server from http://glassfish.org. The current stable version of Glassfish 2.1.1, though Glassfish 3 is in active development. The installer comes packaged as a jar file. You can run the installer with
$ java -Xmx256m -jar glassfish-installer-v2.1.1-b31g-linux.jar
After accepting the license it should put all the files in a folder called ‘glassfish’ in the current directory.
$ cd glassfish You need to run the following commands to finish the setup:
$ chmod -R +x lib/ant/bin $ lib/ant/bin/ant -f setup.xml
Once the software is installed you can start the domain with:
$ bin/asaadmin start-domain domain1
And use either the admin console or the autodeploy directory to deploy the war file.
Glassfish now has a working copy of our application.
Deploy to JBoss and test (effort: n/a - working: no)
JBoss has a community and an enterprise edition. For the purposes of this test we’ll be using the community edition. The current stable version of JBoss AS is 5.1.0 GA. You can get a copy of JBoss from http://www.jboss.org.
Getting started is as simple as unzipping the archive and running:
$ cd jboss-5.1.0.GA
$ bin/run.sh
You can then use the admin console to deploy the application. One gotcha here, the first time I deployed the application using the console I got the following nasty message:
Turns out that after some digging there is an open jruby bug about the issue - 63 http://jira.codehaus.org/browse/JRUBY-3935 (Update: link no longer works, because codehaus is dead)
I also did a bit of digging through the logs and found:
I’ve not managed to find a solution to this problem. I will revisit this at some point in the future. After googling a little it may be possible to just revert to a few older versions that seemed to work.
Deploy to Jetty and test (effort: easy - working: yes)
The current version of Jetty is 7.0.1.v20091125, though the version that comes as part of your Linux distro may not be so up to date. You can either install it using your favorite package manager, or if you’re on Windows get it from the homepage at http://www.mortbay.org
Once you’ve installed Jetty copy the generated war file to the webapps folder, and run:
$ bin/jetty.sh
You should be able to go to http://localhost:8080/deploy_test
Congratulations you now have a working copy of your sinatra app on Jetty.
Deploy to Tomcat and test (effort: easy - working: yes)
The current stable version of Tomcat is 6.0.20. You can either install it using your favorite package manager, or if you’re on Windows get it from the homepage at http://tomcat.apache.org
You shouldn’t have to make any changes to get our web app to work on Tomcat. Once you’ve installed Tomcat copy the generated war file to the webapps folder, and run:
$ bin/startup.sh
You should be able to go to http://localhost:8080/deploy_test
Tomcat really is the bread and butter of Java Application Servers, especially outside the Enterprise.
Deploy to Websphere and test (effort: hard - working: yes)
NB: Websphere 6.1.0.11 was the first application server I ever deployed Sinatra too, it failed miserably. I spent a long time debugging and playing with it to make it work properly. The biggest problem stems from the fact that the default way of using rack as configured by warbler doesn’t work.
Start by logging into the administration console, it should be something like - http://localhost:9043/ibm/console
Click on ‘Servers’ and when it expands select ‘Application Servers’. From here you can setup a new server instance that we can use for our testing. Call the instance something like ‘deploy01’. You can follow the default creation steps all the way through.
Once you have a server instance to test on, you can deploy a new application. The big gotcha as mentioned above is that you can’t use filters, the good news is that it’s quite easy to switch out the rack filters for a rack servlet. Rather than duplicate information that already exists, I’ll link to the place I learned to deploy warbler to websphere, http://clint-hill.com/2008/11/26/jruby-on-rails-and-websphere/.
Websphere is not the easiest application server to setup in general, but once you get it all configured it is fairly robust. Worth the effort if you want an application server you won’t have to restart constantly (as can be the case with Documentum on Tomcat).
Deploy to Weblogic and test (effort: n/a - working: no)
Setting up Oracle Weblogic 10.3.2 was nothing short of awesome. The install process is intuitive and speedy, though the size is quite large, at ~600Mb, compared to smaller cousins such as Tomcat. There is a wizard that walks you through the process of setting up your first domain, what Tomcat would call an instance and Websphere would call a profile, once the software is installed. I chose the default options for everything and had a running Weblogic server in about 20mins (including download).
My previous experience with Weblogic, was the version bundled with Documentum D6SP1. I’ve found both that version and the current fully fledged Oracle version to be a joy to work with.
Once the installation process is finished you can find the administration console at http://localhost:7001/console, you can then login using the username/password you picked during installation.
From the admin console it is a simple matter of clicking on ‘Deployments’ on the left and then when the screen loads clicking ‘install’, browse to the directory with the deploy_test.war file in it and start the install.
You should now be able to access the deployed application at http://localhost:7001/deploy_test…
Clearly thats not going to be the case though. This issue is very similar to the error message received for JBoss.
Conclusion
Not a bad scorecard really. Of all the Java application servers that I tested, only JBoss and Weblogic proved to be a problem. I’ll be retesting these two periodically to see if support has been improved (there are open tickets with JRuby). Until then I hope that this has been useful.