JRuby on Glassfish - Getting Started Guide

  1. Introduction to JRuby on GlassFish

1. What is Ruby?
2. What is Rails?
3. What is JRuby?
4. Advantages of JRuby over other Ruby implementations
5. JRuby on Glassfish and the JRuby GEM

  1. Getting Started with JRuby on GlassFish V3

1. Setting up JRuby
1. Download and install JRUby
2. Specify JRUBY_HOME
2. Installing Rails
3. Setting up the GF V3 GEM

  1. Running a prepared RoR application on GF.
  2. Creating a Rails appplication

1. Generating a Controller
2. Create scaffolding
3. Bundle Rails as a WAR file

  1. Running a Rails app on GF

1. Host app on V3 GEM
2. Deploy app on GF

  1. Accessing a Java EE component from a Rails app using jRuby

Introduction to JRuby on GlassFish

Before going into what JRuby is and why you should use it with GlassFish, let's briefly go over what are Ruby and Rails.

What is Ruby?

Ruby is an open-source, interpreted programming language that has a simple syntax and allows for greater productivity than many other languages do.

What is Rails?

Many programmers use Ruby in the context of Rails, which is a web application framework based on Ruby. Rails leverages the Ruby language to allow developers to create database-backed web applications quickly and easily by eliminating much of the repetition and configuration required in other programming environments. An application written in Ruby and created using the Rails framework is called a Ruby-on-Rails application.

JRuby on GlassFish

JRuby is a 100 % pure Java implementation of Ruby. By using JRuby, you get the simplicity and productivity offered by Ruby and the access to Java libraries offered by the Java platform.

Developing and Deploying your JRuby application on GlassFish has many advantages over using another web server, including:

  • Provides a simple, integrated development and deployment environment
  • Enables you to deploy multiple Rails applicatons to one GlassFish instance
  • Allows your Rails application to handle multiple requests in an efficienct manner

For more details on these and other advantages of using GlassFish for your JRuby on Rails applications, see Advantages of JRuby-on-Rails With the GlassFish Application Server from the article Rails Powered by the GlassFish Application Server

The JRuby GEM

In Ruby on Rails terminology, a GEM is a Ruby package that contains a library or an application. In fact, Rails itself is a GEM that you install on top of Ruby.

One way to work with JRuby on GlassFish V3 TP2 is to install the GlassFish V3 GEM on top of your JRuby installation. The GlassFish V3 GEM is just a GlassFish server and a Grizzly connector for JRuby. When you install it on top of JRuby, you have GlassFish embedded in the JRuby virtual machine.

Getting Started Using JRuby on GlassFish

You have three options for developing and deploying JRuby on GlassFish:

Deploy an existing Ruby on Rails application using the asadmin command.
Bundle a Ruby on Rails application as a WAR file and deploy the WAR file to GlassFish

GlassFish v3

Prerequisite

Installation

Download and install JRuby.

It is recommended to use the latest version. Please refer to the following guides to get more information :

Download and install the latest GlassFish v3 gem.

After you have installed JRuby and Rails on JRuby, install the GlassFish V3 gem by using the command

gem install glassfish

Create an application

Create a template application

jruby -S rails hello

Create a controller and view as:

cd hello
jruby script/generate controller say hello

Edit controller

vi app/controllers/say_controller.rb

and add the following variable in hello helper method

@hello_string = "v3 Gem is getting polished!"

Edit view as:

vi app/views/say/hello.rhtml

and add the following string at the bottom:

<%= @hello_string %>

Run application

Run the hello application created earlier

To run the application created earlier :

cd ..
jruby -S glassfish_rails hello

To run an existing application

If you would like to use GlassFish v3 gem with an existing application e.g. mephisto that is present on our machine at /export/home/samples/mephisto follow these steps :
And launch the GlassFish v3 gem as :

cd /export/home/samples
jruby -S glassfish_rails mephisto

By running the above command, you have now launched the GlassFish v3 server embedded in the same VM space as the JRuby process. Also the application hello is now ready to accept requests which would be served by the GlassFish V3 server.

Effective from v0.1.1 there are additional parameters that could be specified on the command prompt.
To get a list of all the options supported by the gem, use the command :

jruby -S glassfish_rails -h

Cannot resolve external resource into attachment.

For e.g to be able to satisfy 2 concurrent requests you would need to start 2 Rails instances within the GlassFish V3 server and this can be achived by issuing the command :

jruby -S glassfish_rails mephisto -n 2

The console output of the above command
Cannot resolve external resource into attachment.

The application is deployed at *_http://localhost:3000/*_ and ready to accept requests

Sample output of the application as it appears in the browser :

Cannot resolve external resource into attachment.