This chapter explains how to get started using JRuby on Rails with Oracle GlassFish Server 3.0.1.
This section provides an overview of JRuby and Rails in the context of Oracle GlassFish Server.
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.
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:
Access the rich set of Java libraries from Rails applications
Use the powerful and secure support of Java Unicode strings with Rails applications
JRuby on Rails applications can spin off and pool multiple threads because JRuby uses Java threads, which map to native Ruby threads
To learn more about JRuby, see the JRuby Home page.
Developing and deploying Rails applications on Oracle GlassFish Server offers several advantages over using a typical web server for running Rails applications:
Provides a simple, integrated deployment environment; develop and deploy from a single toolkit
Enables deployment of multiple Rails applications to a single GlassFish Server instance
Makes it possible to configure a single Rails application to handle multiple requests
Rails applications can be deployed on GlassFish Server in either of two ways:
Deploy the application to a directory in the GlassFish Server domain.
Run the application using GlassFish v3 Gem.
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 on GlassFish Server comprises three general steps:
Download and install GlassFish Server.
Download and install JRuby.
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:
Using the GlassFish Server Update Tool
GlassFish Server Update Tool is a GUI-based application for selecting and installing GlassFish Server add-ons and updates. It is the easiest means for installing JRuby and Rails for use GlassFish Server.
Update Tool also includes a command-line (CLI) Image Packaging System (IPS) utility, called pkg, that provides the same core functionality as its GUI-based counterpart. The pkg tool is started with the as-install/bin/pkg command. Using the pkg utility to install JRuby and Rails is beyond the scope of this document. See the Update Center wiki for complete information about Update Tool and the pkg command.
In Standalone Mode
You can download and install JRuby and Rails separately from GlassFish Server, in standalone mode. When installed in standalone mode, integrating JRuby and Rails with GlassFish Server involves a few additional configuration steps.
The remainder of this section explains how to install JRuby and Rails using these two different installation methods.
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.
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.
In a command shell for your operating system, change to the as-install/bin directory and run the updatetool command.
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.
Type y when prompted to install Update Tool.
The installer downloads and installs the full Update Tool product and then exits.
Type the updatetool command again to launch Update Tool.
The Update Tool main window is displayed, with Available Updates highlighted.
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.
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:
Add the JRUBY_HOME/bin directory to your system path.
For example:
Proceed with the JRuby container configuration instructions in Configuring JRuby for GlassFish Server.
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.
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.
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.
Set the JRUBY_HOME environment variable to the location of your JRuby installation.
For example:
export JRUBY_HOME=jruby-install |
Add JRUBY_HOME/bin directory to your system path.
For example:
export PATH=$PATH:$JRUBY_HOME/bin |
Perform the following additional steps to configure the JRuby installation to work with GlassFish Server.
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.
After performing the basic installation, you can further configure several aspects of JRuby to work with GlassFish Server:
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.
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.
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:
To allow consistent and fast runtime access for the requesting applications, the dynamic runtime pool maintains itself with the minimum number of runtimes possible. The pool may take an initial runtime value, but that value is not used after pool creation.
The JRuby runtime pool values can be set either at the container level or at deploy time:
To set the runtime pool values at the container level, use the asadmin configure-jruby-container subcommand, as described in Configuring the JRuby Container.
To set the runtime pool values at deploy time, use the asadmin deploy subcommand, as described in Configuring JRuby Deployment Options.
Runtime pool properties can be set simultaneously at both the container level and at deploy time. If both settings are used, the deploy runtime settings take precedence over the container runtime settings.
You can use the GlassFish Server Administration Console to configure a subset of JRuby container options:
JRuby Home
Initial Pool Size
Minimum Pool Size
Maximum Pool Size
These options are the same as those described in Table 1–1.
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.
The following procedure explains how to access a JRuby Container from the GlassFish Server Administration Console.
Open the GlassFish Server Administration Console from a Web browser.
For example:
http://localhost:4848 |
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.
Select Common Tasks->Configuration->JRuby Container from the tree in the panel on the left side of the Administration Console.
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.
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.
This procedure explains how to create a simple Rails application, named hello, and deploy it on GlassFish Server 3.0.1.
JRuby and Rails must be installed as described in Installing JRuby and Rails before proceeding with the instructions in this section.
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.
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
Proceed with the instructions in To 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.
The controller handles requests, dispatches them to other parts of the application as necessary, and determines which view to render.
The view is the file that generates the output to the browser. In Rails, views are typically written with ErB, a templating mechanism.
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 |
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.
Proceed with the instructions in To 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.
Open the hello/app/controllers/home_controller.rb file in a text editor.
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.
Open the hello/app/views/home/index.html.erb file in a text editor.
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.
At this point, you can either:
Proceed with the instructions in To Use Rails Without a Database, which are relevant to the hello sample, but will not be relevant for most applications used in a production environment.
Proceed directly to Deploying and Running a Rails Application for instructions on deploying the application to GlassFish Server.
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.
Open hello/config/environment.rb file in a text editor.
Look for the following commented property in the file:
#config.frameworks -= [ :active_record, :active_resource, :action_mailer ] |
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 ] |
Exit and save the file.
Proceed to Deploying and Running a Rails Application for instructions on deploying the application to GlassFish Server.
There are two general ways to deploy a Rails application on GlassFish Server:
Deploy it as a directory to the GlassFish Server domain, using either the asadmin deploy command or the GlassFish Server Administration Console.
Run it using GlassFish v3 Gem.
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.
To Deploy a Rails Application as a Directory Using the asadmin Command
To Run a Rails Application From the GlassFish Server autodeploy Directory
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.
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.
These instructions build upon the example hello application described in Creating a Simple Rails Application.
Start the GlassFish Server domain to which you want to deploy the application:
asadmin start-domain domain-name |
Change to the directory where you created the sample application.
For example:
cd /apps/jruby-apps/hello |
Deploy the hello application with asadmin command:
as-install/bin/asadmin deploy hello |
Run the hello application using the following URL in your browser:
http://localhost:8080/hello/ |
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.
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 |
Run the hello application using the following URL in your browser:
http://localhost:8080/hello/ |
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.
JRuby, Rails, and the required Gems should already be installed, as described in Installing JRuby and Rails.
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.
Configure the server according to the MySQL documentation, including entering a root password.
Start the MySQL server.
Install the JRuby activerecord-jdbcmysql-adapter gem, if necessary.
For example:
gem install activerecord-jdbcmysql-adapter |
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.
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.
Create and configure a application template to use the MySQL database:
jruby -S rails books -d mysql |
This creates a books directory.
Change to the books/config directory and open the config/database.yml file in a text editor.
Save the file and change back to the books directory, if you are not already there.
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.
(Optional) If desired, verify that a database named books_development was successfully created.
For example:
mysql -u root -p -e 'show databases' |
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.
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 |
(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' |
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:
Giving your controller access to Java libraries
Creating constants to refer to Java classes
Performing file input and output using the java.io and javax.imageio packages
Assigning Java objects to Ruby objects
Calling Java methods and using variables
Converting arrays from Java language arrays to Ruby arrays
Streaming files to the client
For simplicity's sake, this application does not use a database. You will need a JPEG file to run this application.
Change to the directory you want to create an application.
/apps/jruby-apps/
Create an application by running this command:
jruby -S rails imageprocess |
Open the imageprocess/config/environment.rb file in a text editor, and modify the file as described in To Use Rails Without a Database.
Change to the imageprocess directory you just created.
Create a controller and default view for the application by running this command:
jruby script/generate controller home index |
Change to the imageprocess/app/views/home directory.
Create a second view by copying the default view to seeimage.html.erb:
cp index.html.erb seeimage.html.erb |
In this task, you will perform the following actions:
Load an image on which you want to perform image processing with Java2D.
Make the initial view show the original image and provide a link that the user clicks to perform the ColorConvertOp image processing operation.
Make the other view display the processed image.
Find a JPEG image that you can use with this application.
Add the image to imageprocess/public/image file.
Change to imageprocess/app/views/home directory.
Open the index.html.erb file in a text editor.
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.
Replace kids.jpg in the index.html.erb with the name of your image that you saved earlier in this procedure.
Save the index.html.erb file.
Open the seeimage.html.erb file in a text editor.
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.
With this task, you will add the code to process your JPEG image.
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.
Create a constant for the BufferedImage class so that you can refer to it by the shorter name:
BI = java.awt.image.BufferedImage |
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.
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.
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.
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); |
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.
Deploy the application on the GlassFish v3 Gem:
asadmin deploy imageprocess |
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.”
Click the link.
You should now see a grayscale version of the image from the previous page.
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.
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 |
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.
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.
To install the GlassFish v3 Gem on your JRuby standalone instance, use the following procedure:
Download and install JRuby and Rails according to the instructions in To Install JRuby in Standalone Mode.
Run the Gem installer to install the GlassFish v3 Gem:
jruby -S gem install glassfish |
Start your server with the following command:
jruby -S glassfish |
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.
Change to the directory where a sample application has been created. For example:
cd /jruby-apps |
Deploy the previously created hello application:
jruby -S glassfish helloV3 |
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.
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.
Change to /books directory.
Deploy the application to the GlassFish v3 Gem by running the following command:
jruby -S glassfish books |
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.
Add records to the table by clicking the New book link on the index.html page.
Enter the data for book on the new.html page and click Create.
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 |
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.
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.
The procedure for creating a simple Rails application for Warbler, is similar to the procedure described in Creating a Simple Rails Application.
Create a new directory for the Warbler application. For example:
mkdir rails-warbler |
Change to rails-warbler directory and create a sample application called hello:
jruby -S rails hello |
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.
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 ] |
Exit and save the file.
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.
Change to the rails-warbler/hello application directory.
Deploy the application WAR file to the GlassFish Server by running the asadmin command:
asadmin deploy hello.war |
Run the hello application by entering the following URL in your browser:
http://host-name:port/hello |
For more information on Ruby-on-Rails, JRuby, JRuby on GlassFish Server and Warbler, see the following resources: