Navigation

ADS specializes in using Ruby on Rails to build advanced, scalable, database-backed web sites for organizations of all sizes. Find out more at our website.

Atlantic Dominion Solutions

Once we have JRuby installed, have created our first Rails app and have it running under JRuby, the next step is deploy our app to a Java application server. From my discussions with JRuby users and the JRuby team, once a Rails app is packaged as a .war file, you can deploy it to any Java application server. We will use Glassfish in this tutorial as it has a small footprint and zero configuration for our development environment. This article assumes that you followed the previous post and have JRuby installed and a Rails app created. Our next tutorial will demonstrate the true power of JRuby - utilizing Java libraries in Ruby applications using Ruby syntax.

Before we get started, I want to point out a few issues I ran into after downloading and installing Java 6 Release 1 Developers Preview on my Mac. After I downoaded the latest from the Apple Developer Network and ran the installer, I was left wondering where the files were. After a quick search of my hard drive I found quite a few versions of Java installed in

/System/Library/Frameworks/JavaVM.framework/Versions

Once in that directory, the versions were listed by number, and there was version 6, labeled "1.6". In the Versions directory I also found a few symlinks:

/Current
/CurrentJDK

These directories were pointing to the A and 1.5 directories respectively. Ah said I, no wonder I am not running on the latest Java version even after running the "upgrade". What I then did was recreate the Current and CurrentJDK symlinks, pointing both to the 1.6 folder, which itself is a symlink to the 1.6.0 folder. Confused? So was I. Hopefully that will clear up some of the confusion. The reason for this exercise was that the JRuby team suggested that I run JRuby apps under the Java 6 VM in order to get the best performance. In my quest to do so, I found many articles online from people upset about Apple’s lack of a full Java 6 release for the Mac. I’m not to worried though as I am deploying my apps to Linux and Unix servers with the full Java 6 package. Let’s move on and get to the JRuby goodness.

Requirements

Glassfish Version 2 Build 41
• Goldspike Rails Plugin
• JDK Version 5 or 6

Install and configure Glassfish

The first thing we need to do is download Glassfish and get it running. Let’s do it!

• Download Glassfish Version 2 Build 41
• Change to the directory where you want to install it and run

java -Xmx256m -jar glassfish-installer-v2-b41d.jar
cd glassfish
chmod -R +x lib/ant/bin
lib/ant/bin/ant -f setup.xml

It is good to note that when we run Glassfish there will be a few ports available to interact with it:

[exec] Using port 4848 for Admin.
[exec] Using port 8080 for HTTP Instance.
[exec] Using port 7676 for JMS.
[exec] Using port 3700 for IIOP.
[exec] Using port 8181 for HTTP_SSL.
[exec] Using default port 3820 for IIOP_SSL.
[exec] Using default port 3920 for IIOP_MUTUALAUTH.
[exec] Using default port 8686 for JMX_ADMIN.
[exec] Domain being created with profile:developer, as specified by variable AS_ADMIN_PROFILE in configuration file.
[exec] Security Store used should be: JKS
[exec] Domain domain1 created.

We now have Glassfish configured with a default domain, ready to be run. To test, run

bin/asadmin list-domains

This command should return a message letting us know that domain1 is not running. Let’s fire up Glassfish using the following command:

bin/asadmin start-domain

We see a lot of information pass by indicating that the server is up and running.

Starting Domain domain1, please wait.
Log redirected to /Users/rdempsey/Documents/jruby/glassfish/domains/domain1/logs/server.log.
Redirecting output to /Users/rdempsey/Documents/jruby/glassfish/domains/domain1/logs/server.log
Domain domain1 is ready to receive client requests. Additional services are being started in background.
Domain [domain1] is running [Sun Java System Application Server 9.1 (build b41d-beta2)] with its configuration and logs at: [/Users/rdempsey/Documents/jruby/glassfish/domains].
Admin Console is available at [http://localhost:4848].
Use the same port [4848] for "asadmin" commands.
User web applications are available at these URLs:
[http://localhost:8080 https://localhost:8181 ].
Following web-contexts are available:
[/web1  /__wstx-services ].
Standard JMX Clients (like JConsole) can connect to JMXServiceURL:
[service:jmx:rmi:///jndi/rmi://Musashi.local:8686/jmxrmi] for domain management purposes.
Domain listens on at least following ports for connections:
[8080 8181 4848 3700 3820 3920 8686 ].
Domain does not support application server clusters and other standalone instances.

A quick visit to http://localhost:4848 and using the default admin/adminadmin to log in shows us that Glassfish is up and running.

Prepare our Rails app for deployment to Glassfish

There are two ways that I have seen to deploy a Rails app to Glassfish. The first is to use the asadmin command to deploy. This worked well for me aside from the following: I ran the undeploy command and no matter what, every time I ran Glassfish, the initial app I deployed always started up. The second deployment option, and the one we will use here, is to create a .war file. WAR files can be deployed to any Java application server, and are super easy to deploy to Glassfish V2 using the web-based admin console.

Let’s continue to use our Rails application testapp that we created in the first JRuby tutorial.

First, we need to ensure that, for testing purposes, the production database is pointing to the development database and has the same settings in database.yml:

production:
  adapter: jdbc
  driver: com.mysql.jdbc.Driver
  url: jdbc:mysql://localhost/testapp_development
  username: root
  password:

Next we will install the Goldspike plugin:

script/plugin install svn://rubyforge.org/var/svn/jruby-extras/trunk/rails-integration/plugins/goldspike

The Goldspike plugin adds a few rake tasks to our Rails application and gives us the ability to create .war files. Because we are going to be deploying to a Java app server, we need to add the MySQL library to the war_config.rb file in the vendor/plugins/goldspike/lib directory. Add the following line to the current list of java libraries being included:

add_java_library(maven_library ('mysql', 'mysql-connector-java', '5.0.5'))

Let’s check out the rake tasks we get with the Goldspike plugin:

rake tmp:war:clear             # Clears all files used in the creation of a war
rake war:shared:create         # Create a war for use on a Java server where JRuby is available
rake war:standalone:create     # Create a self-contained Java war
rake war:standalone:run        # Run the webapp in Jetty

To create our .war file, run the following command:

rake war:standalone:create

Make sure that you see the following when you run the rake command. We need to be sure that the MySQL library was included lest we have problems.

Assembling web application
  Adding Java library mysql-connector-java-5.0.5
  Adding web application
Creating web archive

We should now have a testapp.war file in the root of our testapp application. Now, let’s make make the deployment magic happen:

• Log in to the Glassfish admin console, click the “Deploy Web Application (.war)” link
• Make sure that you the type is “Web application (.war),” browse to the testapp directory and choose the testapp.war file.
• Let’s keep it simple and keep all of the default settings and click "OK".
• In the console, select testapp, and click “Enable”

We now have our application ready to launch. Launching is a simple matter of clicking the "Launch" link beside our application. Doing so fires up another window with the following URL: http://localhost:8080/testapp/.

Is it really working?

Can it really be that simple? You betcha. Let’s visit http://localhost:8080/testapp/widgets and find out. You can do all of the CRUD goodness.

What’s Next?

In our next tutorial we are going to show the true power and utilization of JRuby - utilizing Java libraries in Ruby applications using Ruby syntax.

Additional resources

JRuby Home Page
Glassfish Community Home Page
Glassfish Wiki - Show Me Tell Me (tutorials)

Share this post

Other posts you may enjoy

You can leave a response, or trackback from your own site.

Print This Post Print This Post

29 Responses to “Deploy Your First JRuby on Rails App to Glassfish”

On June 10th, 2007 at 4:22 pm AkitaOnRails said:

That’s another really nice tutorial. I hope more people jot down specifics for more deployment procedures under other app servers as JBoss, Websphere and what have you. On the other hand, before Rails I’ve developing heavily under 1.4.2 only, so I missed 1.5 and 1.6 altogether. I was assuming that since 1.6 is now OpenJava, someone would have it ported to work reliably on Mac OS X. Anyone knows something about it?

On June 10th, 2007 at 7:01 pm anonymous said:

any idea if rails integration plugin can be used to develop servlets or something similar to a mongrel handler?

On June 10th, 2007 at 7:05 pm Robert Dempsey said:

Not that I know of. I would check out the JRuby site for that one. If you find out let us know.

On June 10th, 2007 at 8:17 pm nap said:

excellent tutorial guys, many thanks.

On June 10th, 2007 at 8:53 pm nap said:

couple comments tho..

1) current version of the mysql connector is 5.0.6, not 5.0.4 (but obviously it’s just whatever you have)
2) goldspike makes some assumptions about the name of the jar files you tell it to add. the default name of the mysql connector when you download the binary version ends in -bin (after the version). you should rename it to mysql-connector-java-5.0.6.jar and make sure it’s in your JRUBY_HOME/lib directory (make sure to add that to your .bash_profile or equivalent startup script)

On June 10th, 2007 at 9:03 pm Robert Dempsey said:

Nap,

Thanks for catching that. Ironically, I have the 5.0.6 in my jruby/lib directory. I have updated the tutorial. Thanks for the goldspike info as well.

On June 10th, 2007 at 11:35 pm AkitaOnRails said:

Hello again. I spent the whole day trying this tutorial with jruby 1.0 and several things went very wrong. I still don’t know the whole reasons, perhaps the fact that I installed my jruby at /opt/local/jruby. After a lot of experimentation, I had to patch Goldmine to support jruby 1.0 (the trunk still used 0.9.9), I had to add all the other .jar dependencies from the jruby/bin (as backport). Then I had to patch it to generate a WEB-INF/web.xml with explicit jruby.home and gem.home set.

My database.yml was using variables as well (production was pointing to development for DRY sake) but the Java YAML parser complained and I had to copy the entire development setting to production as well. Only then my app ran in Glassfish.

Even then it didn’t ran 100%: my testbed app was a slightly customized Redmine and file uploading still didn’t work. I still didn’t figured out what was the reason for that.

On the other hand I also had to patch ActiveRecord-JDBC because it recognizes :boolean as tinyint(1) and bit(1) but it complained about ‘bit’, so I added that to the adapter as well (through opening its class in environment.rb). Only then the unit and funcional tests passed. But the integration testes failed miserably return HTTP 500. Probably something in the CGI support that I still didn’t figure out.

Anyone had the same trouble? I want to share some experiences on that. If anyone can read portuguese, take a look at my detailed report at (http://www.akitaonrails.com/articles/2007/06/10/meu-primeiro-teste-com-jruby-1-0). All the patches I applied to my working copy are there as well.

On June 11th, 2007 at 8:42 am Robert Dempsey said:

Akita,

I am sorry to hear that you ran into so many issues. I thank you for posting them here, and hope that your experience will help others in your shoes. You do bring up a good point as well - the proper application of JRuby.

I have been talking a lot with friends (other developers both Java and Ruby/Rails) about when to use JRuby. I will keep the opinions for another post, however, the consensus was “if it ain’t broke don’t fix it.” In other words, if your Rails app works well running on Mongrel+Apache or whatever your setup is, leave it there. Do you need to plug into Java libraries? If the answer is yes, then go for it, but keep testing as you go.

Thank you again for the post of your experience.

By the way, I had to download the latest Goldspike plugin once I started using JRuby 1.0. In addition, I had to include the path to jruby/lib in my $PATH.

On June 11th, 2007 at 12:26 pm nap said:

You should also note that goldspike’s war_config.rb should be updated to specify the jruby-complete-1.0 jar instead of the 0.99 one. Until it’s available at the appropriate remote sources, you’ll probably want to download the jar from codehaus and place it in your $JRUBY_HOME/lib directory so goldspike can find it.

On June 11th, 2007 at 1:02 pm nap said:

Btw, I took a few additional notes (mostly about providing openssl support) and recorded them on my blog here. Thanks again for the great tut.

On June 11th, 2007 at 1:19 pm Robert Dempsey said:

Thanks for the additional info nap! I will definitely check out your additional notes.

On June 11th, 2007 at 9:44 pm anonymous said:

Has anyone done any benchmarking to compare performance of rails deployed to an appserver vs. a mongrel_jcluster?

On June 11th, 2007 at 9:55 pm Robert Dempsey said:

I haven’t seen any yet. I am definitely looking for it though. If you find any please post it. Thanks.

On June 13th, 2007 at 9:26 am dsuspense said:

For those interested in installing JRuby on Windows and getting a simple JRuby app up and running, please check out the following link:

http://www.developer.com/lang/article.php/3669041

On June 13th, 2007 at 9:51 am Robert Dempsey said:

Thanks for the links to the other tutorials Dominic. They look great.

On June 14th, 2007 at 1:17 pm Duane said:

Does anybody know if I can use JRuby to go in the other direction, namely calling Ruby from existing Java code? We run Java app servers here and I thought it would be neat to be able to code up some of my business logic / rule engine sorts of things in Ruby and just link them in, rather than having to go through the trouble of making a web service out of them.

On June 15th, 2007 at 8:59 am Robert Dempsey said:

@Duane,

Check out the JRuby mailing lists. I have seen people discussing it on the “user” list.

http://xircles.codehaus.org/projects/jruby/lists

On July 23rd, 2007 at 10:24 am khelll said:

i got this when i try to widgets controller:
javax.servlet.ServletException: Could not load Rails. See the logs for more details.
org.jruby.webapp.RailsFactory.makeObject(RailsFactory.java:131)
org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:840)
org.jruby.webapp.AbstractRailsServlet.serviceRequest(AbstractRailsServlet.java:144)
org.jruby.webapp.AbstractRailsServlet.service(AbstractRailsServlet.java:131)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.jruby.webapp.FileServlet.doGet(FileServlet.java:102)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

On July 23rd, 2007 at 10:41 am Robert Dempsey said:

@khelll - what does your environment.rb file look like? You want to make sure that you have the following in there:

if RUBY_PLATFORM =~ /java/
require ‘rubygems’
RAILS_CONNECTION_ADAPTERS = %w(jdbc)
end

Put that under the “require File.join” line. If that doesn’t work let me know. Thanks.

On July 24th, 2007 at 10:59 am florin said:

That?¢‚Ǩ‚Ñ¢s a really nice tutorial! It helped me while I did my own JRuby integration with JPublish ( http://weblog.flop.ca/2007/07/17/1184705235053.html) and I can confirm that being able to run Ruby in Java (JPublish Actions in my case) is very exciting. Thank you for the nice articles.

On July 24th, 2007 at 11:03 am Robert Dempsey said:

Florin,

Thank you for the great feedback. We are working on turning this tutorial into a screencast and should have it released soon.

On August 2nd, 2007 at 6:42 pm Arun Gupta said:

I published a series of tutorials couple of months ago at:
http://blogs.sun.com/arungupta/entry/ruby_on_rails_hello_world, http://blogs.sun.com/arungupta/entry/database_enabled_hello_world_ror and http://blogs.sun.com/arungupta/entry/database_enabled_ror_war_on

I’m working on a screencast version of these tutorials using NetBeans 6 M10.

Thanks,
-Arun

On August 3rd, 2007 at 10:59 am Robert Dempsey said:

Arun,

Thanks for posting your tutorials. They have definitely helped me out. I look forward to the screencasts for NetBeans as well.

On August 8th, 2007 at 9:36 pm Arun Gupta said:

Robert, Glad you liked the entries. The screencast is now available at: http://blogs.sun.com/arungupta/entry/screencast_web6_first_jruby_app

Enjoy!

On August 8th, 2007 at 9:47 pm Robert Dempsey said:

Thanks Arun. I look forward to watching it. Keep us up on all the latest.

On November 8th, 2007 at 1:51 pm Follow-up on my presentation on JRuby « Programblings said:

[...] JRuby and Rails running. Deploying to Glassfish. Theirs posts are pretty detailed, but since things are moving so fast in the JRuby world, some of [...]

On November 23rd, 2007 at 4:37 am Gianluca Tessarolo said:

I try to work with GlassFish V3 gem / Jruby 1.0.2 and Rails 1.2.5: all works well but I don’t understand why GlassFish doesn’t handle concurrent requests.

If one controller action is a bit slow (eg. 10 seconds long) the other actions don’t render anything (no response from GlassFish, only a blank page).

To test it simply put “sleep 10″ before render into an action controller code…

Does someone have tested this simple case ?

Thanks in advance !

On January 18th, 2008 at 7:40 pm Imran Lakhani said:

Thanks Pal,

This tutorial was really very worth and deserve to be bookmarked :)
It was very very helpful

On March 1st, 2008 at 8:23 pm Programblings » Blog Archive » Follow-up on my presentation on JRuby said:

[...] JRuby and Rails running. Deploying to Glassfish. Theirs posts are pretty detailed, but since things are moving so fast in the JRuby world, some of [...]

Leave a Reply