JRuby on Rails: Deploying to Oracle Containers for Java EE (OC4J)This is a featured page

This is a basic howto on deploying a JRuby on Rails application to Oracle Containers for Java EE (OC4J) Standalone.

Contents

  • Prerequisites
  • Installation
  • Create a Rails 2.0 Application
  • Packaging Rails app into WAR
  • Deploy on OC4J Standalone
  • Troubleshooting
  • References

Prerequisites


Note: We used Rails 2.x in this guide, although there will be little changes in Rails 1.x

Installation

  1. First download and install JDK 5, JRuby 1.1 and make sure you set $JAVA_HOME/bin and $JRUBY_HOME/bin in your $PATH variable.

  2. Install Rails 2.0:

    sudo jruby -S gem install rails [-y]

  3. Install activerecord-jdbc-adapter:

    sudo jruby -S gem install activerecord-jdbc-adapter

  4. Download appropriate Oracle JDBC Driver jar file (ojdbc14.jar for DB 10g and ojdbc5.jar for DB 11g) and copy it into $JRUBY_HOME/lib.

Note: JRuby leverages JDBC to access various JDBC compliant database systems and Ruby-OCI8 can't work with JRuby because "Many Gems will work fine in JRuby however some Gems build native C libraries as part of their install process. These Gems will not work in JRuby unless the Gem has also provided a Java equivalent to the native library." as stated in the JRuby Wiki. The latest ActiveRecord JDBC Adapter (v0.8.0) only works with JRuby 1.1RC3 or future version.


Create a Rails 2.0 Application


Performing the following steps to create a simple Rails 2.0 application.

  1. Create the app:

    jruby -S rails bookstore
    cd bookstore

  2. Modify config/database.yml as any of the three forms below:

    development:
    adapter: jdbc
    driver: oracle.jdbc.driver.OracleDriver
    url:jdbc:oracle:thin:@<hostname>:<port>:<sid>
    username: railsuser
    password: railspasswd

    production:
    development
    development:
    adapter: oracle
    url:jdbc:oracle:thin:@<hostname>:<port>:<sid> username: railsuser
    password: railspasswd

    production:
    development
    development:
    adapter: oracle
    host: <hostname>
    port: 1521 (by default)
    database: <sid>
    username: railsuser
    password: railspasswd

    production:
    development
    We can use both the "jdbc" adapter or the "oracle" adapter as when using C Ruby, and set the production configuration the same as development configuration because when deployed Rails uses production configuration.

    In Rails 1.2, the jdbc adapter gem is not required by default by ActiveRecord. We need to add "jdbc" into %JRUBY_HOME%\lib\ruby\gems\1.8\gems\activerecord-x.y.z\lib\active_record.rb as below:

    unless defined?(RAILS_CONNECTION_ADAPTERS)
    RAILS_CONNECTION_ADAPTERS = %w( mysql postgresql sqlite firebird sqlserver db2 oracle sybase openbase frontbase jdbc)
    end

    Thus all Rails application can share this configuration and users don't need to config each newly created Rails application to use JDBC adapter.

  3. Generate scaffold:

    ruby script/generate scaffold book name:string description:text price:float

    The scaffold API in Rails 2.0 is different from Rails 1.2. We need to specify field name and data type in the command line. This command will generate the model, view, controller and db migration files automatically.

  4. Perform database migration:

    jruby -S rake db:migrate

  5. Launch the default web server:

    jruby script/server

  6. Navigate to http://localhost:3000/books to access the application.


Packaging Rails app into WAR

Warbler is a gem to make a single WAR file out of a Rails project and provides a minimal, flexible, Ruby-like way to bundle up all of your application files for deployment to a Java application server. It's developed by Nick Sieger, one of the core developer of JRuby. Warbler bundles JRuby and the Goldspike servlet for dispatching requests to your application inside the java application server, and assembles all jar files in $WARBLER_HOME/lib/*.jar into your application. No external dependencies are needed.

  1. Install Warbler :

    jruby -S gem install warbler

  2. Generate the Warbler configuration file:

    jruby -S warbler config

    and change the 13th line of config/warble.rb file to

    config.gems = ["activerecord-jdbc-adapter", "rails" ]

    because Warbler doesn't automatically package the activerecord JDBC driver.

  3. Package the app:

    jruby -S warbler [war]

    A WAR file(bookstore.war) is generated under the top directory of the Rails app.


Deploy on OC4J Standalone


OC4J stands for "Oracle Containers for J2EE". OC4J standalone is the Oracle AS Containers for J2EE distribution which can be executed outside of the standard OracleAS environment. It is very easy to install(simply unzip it). Typically OC4J standalone would be used for development purposes and for small scale web solutions. OC4J 10g Standalone ships with Oracle JDBC driver for Java 1.4, so deploying JRuby on Rails application on it is made much easier:

  1. Use the Application Server Control Console to deploy (access http://localhost:8888/em), or

  2. Use the command line :

    java -jar $OC4J_ROOT/j2ee/home/admin_client.jar deployer:oc4j:localhost:23791 oc4jadmin -deploy -file /path/to/bookstore.war -deploymentName bookstore -bindAllWebApps

  3. Navigate to http://localhost:8888/bookstore


Troubleshooting

  1. While navigating to http://localhost:8888/bookstore/books after deploying the application on OC4J, the browser displays the following error message:

    No :secret given to the #protect_from_forgery call.
    Set that or use a session store capable of generating its own keys (Cookie Session Store)

    Solution: Uncomment the secret in line 9 in app/controllers/application.rb then re-deploy the app. See link.

  2. While navigating to http://localhost:3000/bookstore/books, the browser displays the following error message:

    driver encountered an error: java.sql.SQLException: Io exception: SO Exception was generated.

    Solution: This is due to malformed database connection string in database.yml which should look like this:

    url: jdbc:oracle:thin:@hostname:1521:XE

    The port 1521 cannot be omitted with ojdbc14.jar but it works with ojdbc5.jar.


References







No user avatar
jesse.hu
Latest page update: made by jesse.hu , May 8 2008, 3:20 AM EDT (about this update About This Update jesse.hu fix a format error - jesse.hu

3 words added
2 words deleted

view changes

- complete history)
More Info: links to this page
Started By Thread Subject Replies Last Post
simoha78 Error by deploying rails Apps on tomcat 1 Sep 22 2009, 1:47 PM EDT by saturnflyer
Thread started: Aug 26 2009, 6:16 AM EDT  Watch
I have a Rails application (MyApp) and want deploy it on Tomcat.
- The configuration files (environment.rb, database.rb, warble.rb) are correctly configured.

- *** LOCAL GEMS ***
actionmailer (2.3.3)
actionpack (2.3.3)
activerecord (2.3.3)
ActiveRecord-JDBC (0.5)
activerecord-jdbc-adapter (0.9.1)
activerecord-jdbcmysql-adapter (0.9.1)
activeresource (2.3.3)
activesupport (2.3.3)
ambethia-smtp-tls (1.1.2)
builder (2.1.2)
camping (1.5.180)
chronic (0.2.3)
fastercsv (1.5.0)
hoe (2.3.3)
jdbc-mysql (5.0.4)
jruby-openssl (0.5.1)
markaby (0.5)
metaid (1.0)
mislav-will_paginate (2.3.11)
packet (0.1.15)
rack (1.0.0)
rails (2.3.3)
rake (0.8.7)
rspec (1.2.6)
rubyforge (1.0.4)
rubygems-update (1.3.5)
rubyist-aasm (2.1.1)
sources (0.0.1)
test-spec (0.10.0)
warbler (0.9.13)
will_paginate (2.2.2)
I use Warbler for creating the WAR file, then I copy this fWAR-file into webapps, start tomcat and call: http://localhost:8080/myApps. Then I get in logs from Tomcat, the following error:
org.jruby.rack.RackInitializationException: no such file to load -- packet
from file:/C:/Programme/Apache Software Foundation/apache-tomcat-6.0.20/webapps/Jruby_App/WEB-INF/lib/jruby-complete-1.3.0RC1.jar!/META-INF/jruby.home/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
I extended my Application with the following plug-ins: attachment_fu, auto_complete, backgroundrb, restful_authentication
I think they cause this problem. I think they should be installed on the Tomcat server too, but I don't know, how I do it.

Can you please tell me wat I am missing?
Thanks im Advance
Do you find this valuable?    
Keyword tags: None
Show Last Reply
PaulHoehne Error during jruby-rack initialization 1 Jul 25 2009, 9:53 PM EDT by PaulHoehne
Thread started: Jul 24 2009, 6:40 PM EDT  Watch
I followed the instructions (except postgres instead of Oracle and JRuby-1.3.1). On both the OC4J and Weblogic containers I get an exception thrown during jruby-rack initialization. As this is an empty application, only updated to include a valid database connection for production and configured for warbler, I find it a little odd. I'm able to deploy the same war file on glassfish 2.1 with no problem. I'm trying to deploy on either 10.1.3.4 (OC4J) or the current WebLogic server.
Do you find this valuable?    
Keyword tags: None
Show Last Reply
Showing 2 of 2 threads for this page

Related Content

  (what's this?Related ContentThanks to keyword tags, links to related pages and threads are added to the bottom of your pages. Up to 15 links are shown, determined by matching tags and by how recently the content was updated; keeping the most current at the top. Share your feedback on Wetpaint Central.)