Oracle GlassFish Server 3.0.1 Scripting Framework Guide

Chapter 1 Using JRuby on Rails With Oracle GlassFishTM Server

This chapter explains how to get started using JRuby on Rails with Oracle GlassFish Server 3.0.1.

Introduction to JRuby and Rails on Oracle GlassFish Server

This section provides an overview of JRuby and Rails in the context of Oracle GlassFish Server.

What Is Ruby on Rails ?

Ruby is an interpreted, dynamically-typed, object-oriented programming language. It has a simple, natural syntax that enables developers to create applications quickly and easily. It also includes the easy-to-use RubyGems packaging utility for customizing a Ruby installation with additional plug-ins.

Rails is a web application framework that leverages the simplicity of Ruby and eliminates much of the repetition and configuration required in other programming environments. With Rails, you can create database-backed web applications, complete with models and tables, by running a few one-line commands.

To learn more about Ruby on Rails, see Ruby on Rails.

What Is JRuby ?

JRuby is a JavaTM implementation of the Ruby interpreter. While retaining many of the popular characteristics of Ruby, such as dynamic-typing, JRuby is integrated with the Java platform. With JRuby on Rails, you get the simplicity and productivity offered by Ruby and Rails and the power of the Java platform offered by JRuby, thereby giving you many benefits as a Rails developer, including the following:

To learn more about JRuby, see the JRuby Home page.

JRuby on Rails, GlassFish Server, and GlassFish v3 Gem

Developing and deploying Rails applications on Oracle GlassFish Server offers several advantages over using a typical web server for running Rails applications:

Rails applications can be deployed on GlassFish Server in either of two ways:


Note –

A Gem is a Ruby package that contains a library or an application. Rails itself is a Gem that is installed on JRuby. See GlassFish v3 Gem for more information.


Installing JRuby and Rails

    Installing JRuby and Rails on GlassFish Server comprises three general steps:

  1. Download and install GlassFish Server.

  2. Download and install JRuby.

  3. Install Rails on top of your JRuby installation.

The instructions in this guide assume that GlassFish Server software is already installed and running. If GlassFish Server is not already installed, refer to the Oracle GlassFish Server 3.0.1 Installation Guide for installation instructions.

JRuby and Rails can be installed on GlassFish Server in either of two ways:

The remainder of this section explains how to install JRuby and Rails using these two different installation methods.

ProcedureTo Install JRuby and Rails Using Update Tool

This procedure explains how to install JRuby and JRuby Gems (including Rails) directly on top of an existing GlassFish Server installation using the GUI-based Update Tool.

Before You Begin

GlassFish Server must be installed and running before proceeding with these instructions.

In these instructions, the root GlassFish Server installation directory is referred to as as-install.

  1. In a command shell for your operating system, change to the as-install/bin directory and run the updatetool command.


    Tip –

    If you are running Update Tool from behind a firewall that implements a proxy server, you may need to set the HTTP_PROXY environment variable before running the updatetool command. For example, in a Solaris/Linux Bash shell:


    export HTTP_PROXY=http://proxy-server:port
    

    If this is the first time you have launched updatetool, the full Update Tool product will not yet be installed, and you are prompted to allow installation to proceed.

    1. Type y when prompted to install Update Tool.

      The installer downloads and installs the full Update Tool product and then exits.

    2. Type the updatetool command again to launch Update Tool.

    The Update Tool main window is displayed, with Available Updates highlighted.

  2. Select the JRuby on GlassFish and JRuby Gems packages from the list of Available Add-Ons, and then click Install to install the packages.

    The contents of the JRuby on GlassFish and JRuby Gems packages are as follows:

    • JRuby on GlassFish – Contains JRuby 1.3.1

    • JRuby Gems – Contains Rails 2.3.2, Warbler, jdbc-mysql, and activerecord-jdbcmysql-adapter packages.

  3. Set the JRUBY_HOME environment variable to the location of your JRuby installation.

    By default, when installed using Update Tool, JRuby is installed in a subdirectory below as-install/glassfish. The commands to set JRUBY_HOME will typically be as follows:

    • Solaris/Linux:


      export JRUBY_HOME=as-install/glassfish/jruby
      
    • Windows:


      set JRUBY_HOME=C:\as-install\glassfish\jruby
      
  4. Add the JRUBY_HOME/bin directory to your system path.

    For example:

    • Solaris/Linux:


      export PATH=$PATH:$JRUBY_HOME/bin
      
    • Windows:


      set PATH=%JRUBY_HOME%\bin;%PATH%
      
  5. Proceed with the JRuby container configuration instructions in Configuring JRuby for GlassFish Server.

ProcedureTo Install JRuby in Standalone Mode

This procedure explains how to download and install JRuby and then configure it to work with GlassFish Server. If you prefer to use the GlassFish Server Update Tool to install JRuby directly on GlassFish Server, use the procedure in To Install JRuby and Rails Using Update Tool instead.

  1. Download the desired JRuby package from the JRuby Downloads page and save it in the location of your choice.

    Packages are available in a variety of formats, including tar.gz, ZIP, Windows executable, and JAR.

  2. Unpack the JRuby package in the directory of your choice.

    For the purposes of this procedure, the directory in which you unpack the JRuby package is referred to as jruby-install.

  3. Set the JRUBY_HOME environment variable to the location of your JRuby installation.

    For example:


    export JRUBY_HOME=jruby-install
    
  4. Add JRUBY_HOME/bin directory to your system path.

    For example:


    export PATH=$PATH:$JRUBY_HOME/bin
    
  5. Perform the following additional steps to configure the JRuby installation to work with GlassFish Server.

    1. Change to the GlassFish Server bin directory and start the GlassFish Server domain.

      For example:


      cd as-install/glassfish/bin
      ./asadmin start-domain domain-name
      
    2. Specify the JRuby home directory:


      ./asadmin configure-jruby-container --jruby-home=jruby-install
      
  6. Install the required Rails Gems for standalone JRuby operation.


    jruby -S gem install rails
    

    The -S parameter tells JRuby to look for the install rails script anywhere in the JRUBY_HOME path.

Configuring JRuby for GlassFish Server

After performing the basic installation, you can further configure several aspects of JRuby to work with GlassFish Server:

Configuring the JRuby Container

The GlassFish Server asadmin configure-jruby-container subcommand provides several options for configuring a JRuby container. Changes made to a JRuby container using the configure-jruby-container subcommand are written to the GlassFish Server domain's domain.xml file, which makes the changes persistent.

The general syntax for configuring the JRuby container is:


asadmin configure-jruby-container --property=value

For example, the following command sets the jruby-home property:


asadmin configure-jruby-container --jruby-home=jruby-install

Table 1–1 lists the options provided by the asdmin configure-jruby-container subcommand.

Table 1–1 asdmin configure-jruby-container Options

Option 

Description 

--help | -?

Displays the help text for the subcommand. 

--monitoring

If set to true, enables monitoring for the JRuby container. The default is false.

--jruby-home

The directory in which JRuby itself (not the GlassFish Server JRuby container) is installed. 

The specified directory must exist or an error will occur. Note, however, that the configure-jruby-container subcommand does not check whether JRuby is installed in the directory.

The default directory is as-install/jruby, which is the directory in which Update Tool installs JRuby. Therefore, if you obtained JRuby from Update Tool, this option is not required.

--jruby-runtime

The initial number of JRuby runtime instances in the pool. 

This number must be greater than 0, greater than or equal to --jruby-runtime-min, and less than or equal to --jruby-runtime-max. The default is 1.

--jruby-runtime-min

The minimum number of JRuby runtime instances in the pool. 

This number must be greater than 0, and less than or equal to --jruby-runtime and --jruby-runtime-max. The pool will always be at least this large, but can also be larger. The default is 1.

--jruby-runtime-max

The maximum number of JRuby runtime instances in the pool. 

This number must be greater than 0, and greater than or equal to --jruby-runtime and --ruby-runtime-min. The default is 1. Setting this value too high can cause OutOfMemory errors, either in the heap or PermGen.

--show

If set to true, displays the current settings of the Enterprise Server JRuby container. The default is true.

For additional information about the asadmin JRuby configuration options, see the configure-jruby-container(1) man page.

Configuring JRuby Deployment Options

You can change deployment-specific options for the JRuby application using the following command syntax:


asadmin deploy --property property=value[:property=value]

For example, the following command deploys an application using a JRuby instance that is different from the one configured for the GlassFish Server instance:


asadmin deploy --property jruby-home=latest-jruby-install application

See the Appendix A, The asadmin Deployment Subcommands, in Oracle GlassFish Server 3.0.1 Application Deployment Guide for detailed instructions on using the extensive list of asadmin deploy options to deploy applications, including JRuby, on GlassFish Server.

Configuring the JRuby Runtime Pool

GlassFish Server 3.0.1 provides a JRuby runtime pool to enable the servicing of multiple concurrent requests. It should be noted, however, that Rails is not currently thread-safe, and while JRuby is able to take advantage of Java's native threading, Rails cannot benefit from it. Each JRuby runtime runs a single instance of Rails, and requests are handed off to whichever instance happens to be available at the time of the request.

See Table 1–1 for descriptions of the JRuby runtime pool options.

The following are some important points to remember when configuring the JRuby runtime pool:

Configuring the JRuby Container Through GlassFish Server Administration Console

You can use the GlassFish Server Administration Console to configure a subset of JRuby container options:

These options are the same as those described in Table 1–1.


Note –

When configured through the Administration Console, these settings apply to the currently running GlassFish Server instance only. When the instance is shut down, the changes will be lost. To make your changes persistent from instance to instance, use the asadmin configure-jruby-container command instead.


For complete information about using the GlassFish Server Administration Console, see Administration Console in Oracle GlassFish Server 3.0.1 Administration Guide.

ProcedureTo Configure JRuby Container from the GlassFish ServerAdministration Console

The following procedure explains how to access a JRuby Container from the GlassFish Server Administration Console.

  1. Open the GlassFish Server Administration Console from a Web browser.

    For example:


    http://localhost:4848
  2. Log in to the Administration Console, if configured for secure login.

    Depending on your GlassFish Server configuration, your Administration Console may not require login credentials.

  3. Select Common Tasks->Configuration->JRuby Container from the tree in the panel on the left side of the Administration Console.

  4. Change JRuby Container properties, as desired, in the Details panel.

    See Configuring the JRuby Container for explanations of JRuby container properties. For detailed descriptions of the JRuby configuration options, see the configure-jruby-container(1) man page.

Creating a Simple Rails Application

After installing and configuring JRuby on GlassFish Server, you are ready to start coding. This section explains how to create a simple Rails application, named hello, that displays the following message:


Welcome to JRuby on Rails on the Oracle GlassFish Server!

The process of creating and deploying the hello application can be broken down into four subtasks:

After completing the procedures in this section, continue to Deploying and Running a Rails Application for information about deploying applications to GlassFish Server.

ProcedureTo Create the hello Application

This procedure explains how to create a simple Rails application, named hello, and deploy it on GlassFish Server 3.0.1.

Before You Begin

JRuby and Rails must be installed as described in Installing JRuby and Rails before proceeding with the instructions in this section.

  1. Select or create a directory in which you want to create the sample application.

    In this example, a directory named /apps/jruby-apps is used.

  2. Change to the directory you are using for your applications, and then create a Rails application named hello.


    cd /apps/jruby-apps
    jruby -S rails hello
    

    This command creates the hello subdirectory, which contains a set of automatically generated files and subdirectories.

    The directories containing the files that you will likely use most are:

    • app: Application code (controllers, helpers, models, views, layouts)

    • config: Configuration files (environments, initializers, locales), including database, boot, and route files

    • public: Files and resources that need to be accessed directly rather than accessed through the Rails call stack; includes images, style sheets, and HTML files

  3. Proceed with the instructions in To Create the Controller and the View.

ProcedureTo Create the Controller and the View

The next step in creating and deploying the hello application is to create an application controller and a default application view.

  1. Change to the directory in which you created the hello application in To Create the hello Application.

    In this example:


    cd /apps/jruby-apps/hello
    
  2. Create a controller and default view for your application:


    jruby script/generate controller home index
    

    You should see a controller file called home_controller.rb in the hello/app/controllers directory, and a view file called index.html.erb in the hello/app/views directory.

  3. Proceed with the instructions in To Pass Data From the Controller to the View.

ProcedureTo Pass Data From the Controller to the View

After creating a controller and a view for the hello application, the next step is to pass data from the controller to the view. This procedure explains how to set an instance variable in the controller and then access its value from the view.

  1. Open the hello/app/controllers/home_controller.rb file in a text editor.

  2. Add an instance variable named @hello_message to the action named index and then save the file.

    The file should now contain the following:


    class HomeController < ApplicationController
    def index
    @hello_message = "Welcome to JRuby on Rails on Oracle GlassFish Server"
    end
    end

    In Rails, actions map to views. In this case, when you access the index.html.erb file, the index action executes, making the @hello_message variable available to index.html.erb.

  3. Open the hello/app/views/home/index.html.erb file in a text editor.

  4. Add the following output block at the end of the file and then save the file:


    <p><%= @hello_message %></p>

    The file should now contain the following:


    <h1>Home#index</h1>
    <p>Find me in app/views/home/index.html.erb</p>
    <p><%= @hello_message %></p>

    This JRuby code, embedded into the view, inserts the value of @hello_message into the page. When you deploy run the hello application, “Welcome to JRuby on Rails on the GlassFish Server" is displayed in your browser.

  5. At this point, you can either:

ProcedureTo Use Rails Without a Database

Although Rails is intended for creating database-backed web applications, this example is simple enough that it does not require one. In this case, you can edit the enviroment.rb configuration file to indicate that your application does not use a database.

  1. Open hello/config/environment.rb file in a text editor.

  2. Look for the following commented property in the file:


    #config.frameworks -= [ :active_record, :active_resource, :action_mailer ]
  3. Uncomment the property by removing the pound (#) character from the beginning of the line.

    The line should now read as follows:


    config.frameworks -= [ :active_record, :active_resource, :action_mailer ]
  4. Exit and save the file.

  5. Proceed to Deploying and Running a Rails Application for instructions on deploying the application to GlassFish Server.

Deploying and Running a Rails Application

There are two general ways to deploy a Rails application on GlassFish Server:

The reminder of this section explains how to deploy a Rails application to GlassFish Server as a directory. For more information about running Rails applications using GlassFish v3 Gem, see To Run a Rails Application on GlassFish v3 Gem.


Note –

These procedures provide only the most basic deployment instructions. For comprehensive information about deploying applications to GlassFish Server, refer to the Oracle GlassFish Server 3.0.1 Application Deployment Guide.


ProcedureTo Deploy a Rails Application as a Directory Using the asadmin Command

The following procedure explains how to use the asadmin command to deploy the sample hello application as a directory within a GlassFish Server domain, and how to access the application from your web browser. You can also use these same instructions to deploy any Rails application to GlassFish Server.

Before You Begin

These instructions build upon the example hello application described in Creating a Simple Rails Application.

  1. Start the GlassFish Server domain to which you want to deploy the application:


    asadmin start-domain domain-name
    
  2. Change to the directory where you created the sample application.

    For example:


    cd /apps/jruby-apps/hello
    
  3. Deploy the hello application with asadmin command:


    as-install/bin/asadmin deploy hello
    
    • If you want to use a different JRuby instance than the update installation or the instance you configured with the configure-jruby-container command, you can use the following deploy-time option:


      asadmin deploy --property jruby-home=jruby-install
      
  4. Run the hello application using the following URL in your browser:


    http://localhost:8080/hello/

ProcedureTo Run a Rails Application From the GlassFish Server autodeploy Directory

As with the instructions in To Deploy a Rails Application as a Directory Using the asadmin Command, this procedure builds upon the example hello application described in Creating a Simple Rails Application.

  1. Copy the entire hello application directory to the as-install/glassfish/domains/domain-dir/autodeploy directory.

    For example:


    cp -r /apps/jruby-apps/hello glassfishv3/glassfish/domains/domain1/autodeploy
    
  2. Run the hello application using the following URL in your browser:


    http://localhost:8080/hello/

Accessing a Database From a Rails Application

One of the main strengths of Rails is that it makes it easy to create applications that access databases. This section explains how to create a simple application that accesses a book database using MySQLTM.

ProcedureTo Set Up the MySQL Database Server

Before You Begin

JRuby, Rails, and the required Gems should already be installed, as described in Installing JRuby and Rails.

  1. Download and install MySQL database server:

    MySQL Server is available from the MySQL Downloads page. MySQL installation instructions are available from the MySQL Documentation page.

  2. Configure the server according to the MySQL documentation, including entering a root password.

  3. Start the MySQL server.

  4. Install the JRuby activerecord-jdbcmysql-adapter gem, if necessary.

    For example:


    gem install activerecord-jdbcmysql-adapter
    
  5. Modify the database.rake script for your Rails installation so it uses jdbcmysql rather than mysql.

    For example, if using the version of JRuby and Rails installed through GlassFish Server Update Center on Solaris or Linux, the database.rake file is located in:


    $JRUBY_HOME/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/tasks

    Change all instances of mysql with jdbcmysql, and then save the file.

ProcedureTo Create a Database-Backed Rails Application

  1. Create or select a directory for creating a database-backed Rails application, and change to that directory.

    In this example, a directory named /apps/jruby-apps is used.

  2. Create and configure a application template to use the MySQL database:


    jruby -S rails books -d mysql
    

    This creates a books directory.

  3. Change to the books/config directory and open the config/database.yml file in a text editor.

    1. Replace all instances of adapter: mysql with adapter: jdbcmysql.

    2. Enter your MySQL root password under the development heading in the database.yml file.

  4. Save the file and change back to the books directory, if you are not already there.

  5. Create the database by running the following command:


    jruby -S rake db:create
    

    The rake command invokes the Rake tool. The Rake tool builds applications by running Rake files, which are written in Ruby and provide instructions for building applications.

  6. (Optional) If desired, verify that a database named books_development was successfully created.

    For example:


    mysql -u root -p -e 'show databases'
    
  7. Still in the books directory, create the scaffold and the book model for the application:


    jruby script/generate scaffold book title:string \
    author:string isbn:string description:text
    

    When you run the script/generate command, you specify the name of the model, the names of the columns, and the types for the data contained in the columns.

    A scaffold is the set of code that Rails generates to handle database operations for a model object, which is Book in this case. The scaffold consists of a controller and some views that allow users to perform the basic operations on a database, such as viewing the data, adding new records, and editing records. Rails also creates the model object when generating the scaffold.

  8. Create the database tables:


    jruby -S rake db:migrate
    

    When Rails is finished creating the tables, you should see output similar to the following:


    ==  CreateBooks: migrating ====================================================
    -- create_table(:books)
       -> 0.1470s
       -> 0 rows
    ==  CreateBooks: migrated (0.1470s) ===========================================

    If you need to reset the database later, you can run the following command:


    jruby —S rake db:reset
    
  9. (Optional) If desired, verify that two tables, named books and schema_migrations, were successfully created in the books_development database.

    For example:


    mysql -u root -p -e 'show tables from books_development'
    

Accessing Java Libraries From a Rails Application

The primary advantage of developing with JRuby is that you have access to Java libraries from a Rails application. For example, say you want to create an image database and a web application that allows processing of the images. You can use Rails to set up the database-backed web application and use the powerful Java 2DTM API for processing the images on the server-side.

This section shows you how to get started using Java libraries in a Rails application while stepping you through building a simple Rails application that does basic image processing with the Java 2D API.

This application demonstrates the following concepts involved in using Java libraries in a Rails application:

For simplicity's sake, this application does not use a database. You will need a JPEG file to run this application.

ProcedureTo Create the Rails Application That Accesses Java Libraries

  1. Change to the directory you want to create an application.

    /apps/jruby-apps/

  2. Create an application by running this command:


    jruby -S rails imageprocess
  3. Open the imageprocess/config/environment.rb file in a text editor, and modify the file as described in To Use Rails Without a Database.

  4. Change to the imageprocess directory you just created.

  5. Create a controller and default view for the application by running this command:


    jruby script/generate controller home index
    
  6. Change to the imageprocess/app/views/home directory.

  7. Create a second view by copying the default view to seeimage.html.erb:


    cp index.html.erb seeimage.html.erb
    

ProcedureTo Create the Views That Display the Images Generated by Java2D Code

In this task, you will perform the following actions:

  1. Find a JPEG image that you can use with this application.

  2. Add the image to imageprocess/public/image file.

  3. Change to imageprocess/app/views/home directory.

  4. Open the index.html.erb file in a text editor.

  5. Replace the contents of this file with the following HTML markup:


    <html>
    	<body>
    		<img src="../../images/kids.jpg"/><p>
    		<%= link_to "Perform a ColorConvertOp on this image", :action => "seeimage" %>
    	</body>
    </html>

    This page loads an image from imageprocess/public/images and provides a link that references the seeimage action. The seeimage action maps to the seeimage view, which shows the processed image.

  6. Replace kids.jpg in the index.html.erb with the name of your image that you saved earlier in this procedure.

  7. Save the index.html.erb file.

  8. Open the seeimage.html.erb file in a text editor.

  9. Replace the contents of this file with the following HTML markup:


    <html>
    	<body>
    		<img src="/home/processimage"/><p>
    		<%= link_to "Back", :action => "index" %>
    	</body>
    </html>

    The img tag on this page accesses the processimage action in HomeController. The processimage action is where you will put the Java2D code to process the image that you loaded into index.html.erb.

ProcedureTo Add Java2D Code to a Rails Controller

With this task, you will add the code to process your JPEG image.

  1. Add the following line to HomeController, right after the class declaration:


    include Java

    This line is necessary for you to access any Java libraries from your controller.

  2. Create a constant for the BufferedImage class so that you can refer to it by the shorter name:


    BI = java.awt.image.BufferedImage
  3. Add an empty action, called seeimage, at the end of the controller:


    def seeimage
    end

    This action is mapped to the seeimage.html.erb view.

  4. Give controller access to your image file using java.io.File, making sure to use the name of your image in the path to the image file. Place the following line inside the seeimage action:


    filename = "#{RAILS_ROOT}/public/images/kids.jpg"
    imagefile = java.io.File.new(filename)

    Notice that you do not need to declare the types of the variables, filename or imagefile. JRuby can tell that filename is a String and imagefile is a java.io.File instance because that is what you assigned them to be.

  5. Read the file into a BufferedImage object and create a Graphics2D object from it so that you can perform the image processing on it. Add these lines directly after the previous two lines:


    bi = javax.imageio.ImageIO.read(imagefile)
    w = bi.getWidth()
    h = bi.getHeight()
    bi2 = BI.new(w, h, BI::TYPE_INT_RGB)
    big = bi2.getGraphics()
    big.drawImage(bi, 0, 0, nil)
    bi = bi2
    biFiltered = bi

    Refer to The Java Tutorial for more information on the Java 2D API.

    The important points are :

    • You can call Java methods in pretty much the same way in JRuby as you do in Java code.

    • You do not have to initialize any variables.

    • You can just create a variable and assign anything to it. You do not need to give it a type.

  6. Add the following code to convert the image to grayscale:


    colorSpace = java.awt.color.ColorSpace.getInstance(
    java.awt.color.ColorSpace::CS_GRAY)
    op = java.awt.image.ColorConvertOp.new(colorSpace, nil)
    dest = op.filter(biFiltered, nil)
    big.drawImage(dest, 0, 0, nil);
  7. Stream the file to the browser:


    os = java.io.ByteArrayOutputStream.new
    javax.imageio.ImageIO.write(biFiltered, "jpeg", os)
    string = String.from_java_bytes(os.toByteArray)
    send_data string, :type => "image/jpeg", :disposition => "inline", 
    	:filename => "newkids.jpg"

    Sometimes you need to convert arrays from Ruby to Java code or from Java code to Ruby. In such cases, you need to use the from_java_bytes routine to convert the bytes in the output stream to a Ruby string so that you can use it with send_data to stream the image to the browser. JRuby provides some other routines for converting types, such as to_java to convert from a Ruby Array to a Java String. See Conversion of Types.

ProcedureTo Run a Rails Application That Uses Java 2D Code

  1. Deploy the application on the GlassFish v3 Gem:


    asadmin deploy imageprocess
    
  2. Run the application by entering the following URL into your browser:


    http://localhost:3000/home/index

    You should now see an image and a link that says, “Perform a ColorConvertOp on this image.

  3. Click the link.

    You should now see a grayscale version of the image from the previous page.

Monitor Rails Applications on GlassFish Server 3.0.1

The GlassFish Server 3.0.1 offers monitoring services for various objects.

Monitoring is the process of reviewing the statistics of a system to improve performance or solve problems. The monitoring service can track and display operational statistics, such as the number of requests per second, the average response time, and the throughput. For more information on monitoring, see Chapter 8, Administering the Monitoring Service, in Oracle GlassFish Server 3.0.1 Administration Guide.

Monitoring for JRuby Container

The JRuby container installed on the GlassFish Server can be configured to be monitored. By default the monitoring services are disabled.

Monitoring can be configured for the container using the following asadmin CLI command:


configure-jruby-container --monitoring=true

The monitoring service can be enabled or disabled. Enable monitoring for GlassFish Server JRuby container with the following runtime command:


enable-monitoring --modules jruby-container=HIGH

The other possible values are OFF or LOW. Disable the monitoring for JRuby container with the following command:


disable-monitoring --modules jruby-container=OFF

Viewing JRuby Container Statistics

JRuby statistics are available through both the asadmin CLI and the Administration Console. To learn more about different types of JRuby statistics available from GlassFish Server, see JRuby Statistics in Oracle GlassFish Server 3.0.1 Administration Guide.

GlassFish v3 Gem

Another way to work with JRuby is to install the GlassFish v3 Gem on a JRuby standalone instance. The GlassFish v3 Gem is just a lightweight version of the Oracle GlassFish Server 3.0.1 that runs as part of JRuby instance.

When you install the GlassFish v3 Gem on JRuby, you have a Oracle instance embedded in the JRuby virtual machine. This gives you a more complete development environment because you have everything you need for JRuby on Rails applications running inside the JRuby virtual machine in addition to everything you need from the GlassFish Server to create web applications.

ProcedureTo Install the GlassFish v3 Gem

To install the GlassFish v3 Gem on your JRuby standalone instance, use the following procedure:

  1. Download and install JRuby and Rails according to the instructions in To Install JRuby in Standalone Mode.

  2. Run the Gem installer to install the GlassFish v3 Gem:


    jruby -S gem install glassfish
    
  3. Start your server with the following command:


    jruby -S glassfish
    

ProcedureTo Run a Rails Application on GlassFish v3 Gem

You can also run your JRuby on Rails application on the embedded GlassFish v3 Gem. Create a simple Rails application as described in Creating a Simple Rails Application. Instead of deploying it to the GlassFish Server, use the GlassFish v3 Gem to run that application as described in the following task.

  1. Change to the directory where a sample application has been created. For example:


    cd /jruby-apps
    
  2. Deploy the previously created hello application:


    jruby -S glassfish helloV3
    
  3. Run the application using the following URL in your web browser:


    http://localhost:3000/home/index

    You should now see the following message in your browser window:


    Welcome to JRuby on Rails on the Oracle GlassFish Server!

    Note that the GlassFish v3 Gem runs on port 3000 and not the default port 8080.

ProcedureTo Deploy and Run the Database-Backed Web Application

Creating database-backed web applications was described in To Create a Database-Backed Rails Application. The following task describes running the created books application on the GlassFish v3 Gem. You can alternatively deploy it to your regular GlassFish Server using directory-based deployment, as described in To Deploy a Rails Application as a Directory Using the asadmin Command.

  1. Change to /books directory.

  2. Deploy the application to the GlassFish v3 Gem by running the following command:


    jruby -S glassfish books
  3. Run the application in your web browser using the following URL:


    http://localhost:3000/books

    The opening page says “Listing books” and has an empty table, meaning that there are no book records in the database yet. To add book records to the table, do the next step.

  4. Add records to the table by clicking the New book link on the index.html page.

  5. Enter the data for book on the new.html page and click Create.

Using GlassFish v3 Gem CLI

The GlassFish v3 Gem offers several commands to configure the GlassFish Server server. You can change the configuration options either by using GlassFish Server command line or by modifying the glassfish.yml file. You view these options by using the following command:


glassfish -h

The options include runtimes, runtimes-min, runtimes-max, and also a configuration option to create a configuration file for GlassFish Server.

You can create a glassfish.yml file which is a default GlassFish Server configuration file generated by the following command:


jruby -S gfrake config

Introduction to Warbler

In Deploying and Running a Rails Application, direct deployment of a rails application to GlassFish Server has been described. Warbler provides an easier way to deploy a rails application to a Java application server.

What Is Warbler ?

Warbler is a Gem that makes WAR file out of a Rails, Merb, or Rack-based application. Warbler provides a minimal, flexible, Ruby-like way to bundle application files for deployment to a Java application server.

Warbler provides a set of out-of-the box defaults to allow most Rails applications to assemble and work without external Gem dependencies.

Warbler bundles JRuby and the JRuby-Rack Servlet adapter for dispatching requests to the application inside the Java application server, and assembles all jar files in the WARBLER_HOME/lib/ directory into the application.

To learn more about Warbler, see Warbler.

Creating and Deploying a Simple Rails Application with Warbler

The procedure for creating a simple Rails application for Warbler, is similar to the procedure described in Creating a Simple Rails Application.

ProcedureTo Create a Rails Application

  1. Create a new directory for the Warbler application. For example:


    mkdir rails-warbler
    
  2. Change to rails-warbler directory and create a sample application called hello:


    jruby -S rails hello
  3. Edit the enviroment.rb file to indicate that your application does not use a database:

    Open rails-warbler/hello/config/environment.rb in a text editor.

  4. Remove the pound character (#) in front of line 21 to uncomment it so that it reads as follows and save:


    config.frameworks -= [ :active_record, :active_resource, :action_mailer ]
  5. Exit and save the file.

  6. Use Warbler to create a war file in rails-warbler/hello application directory:


    jruby -S warble

    This creates a hello.war file in the directory.

ProcedureTo Deploy the WAR File

  1. Change to the rails-warbler/hello application directory.

  2. Deploy the application WAR file to the GlassFish Server by running the asadmin command:


    asadmin deploy hello.war
    
  3. Run the hello application by entering the following URL in your browser:


    http://host-name:port/hello

For Further Information

For more information on Ruby-on-Rails, JRuby, JRuby on GlassFish Server and Warbler, see the following resources: