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)
Other Posts That Might Interest You