Miles to go …

March 11, 2009

TOTD #73: JRuby and GlassFish Integration Test #4: JRuby 1.2.0 RC2 + Rails 2.2.x + GlassFish v2 + Warbler

Filed under: rails, totd, web2.0 — arungupta @ 4:00 am

TOTD #70, #71, #72 shows the first three integration tests that I typically run to ensure that the latest JRuby and GlassFish versions work nicely with each other.  #70 showed how to create a trivial Rails application and run it using GlassFish Gem. #71 showed how the same application can be deployed on GlassFish v3 Prelude. #72 showed how to deploy the same application on GlassFish v3.

The next test in the series is to ensure WAR-based deployment of a Rails application continue to work on GlassFish v2. It also shows that JNDI database connection pooling also work as expected. The latest publicly available build is GlassFish v2.1.

Lets begin integration test #4.

  1. Install Warbler gem …

    ~/tools/jruby-1.2.0RC2/samples/rails/runner >../../../bin/jruby -S gem install warbler
    JRuby limited openssl loaded. gem install jruby-openssl for full support.
    http://wiki.jruby.org/wiki/JRuby_Builtin_OpenSSL
    Successfully installed warbler-0.9.12
    1 gem installed
    Installing ri documentation for warbler-0.9.12…
    Installing RDoc documentation for warbler-0.9.12…
  2. Edit “config/database.yml” and change the production database adapater from:
    production:
      adapter: mysql
      encoding: utf8
      database: runner_production
      pool: 5
      username: root
      password:
      socket: /tmp/mysql.sock

    to

    production:
      adapter: jdbcmysql
      encoding: utf8
      database: runner_production
      pool: 5
      username: duke
      password: glassfish
      socket: /tmp/mysql.sock
      jndi: jdbc/runner_production
      driver: com.mysql.jdbc.Driver

    The changes are highlighted in bold.

    Notice “jndi” key/value pair is specified along with “username” and “password”. The JNDI reference is created for the GlassFish domain later. The reference is not resolved when this adapter is used with the JRuby CLI and so it falls back to username/password. However this JNDI reference is correctly resolved during runtime when the application is deployed as a WAR file in GlassFish.

  3. Create and migrate the production database as:
    ~/tools/jruby-1.2.0RC1/samples/rails/runner >../../../bin/jruby -S rake db:create RAILS_ENV=production
    (in /Users/arungupta/tools/jruby-1.2.0RC1/samples/rails/runner)
    ~/tools/jruby-1.2.0RC1/samples/rails/runner >../../../bin/jruby -S rake db:migrate RAILS_ENV=production
    (in /Users/arungupta/tools/jruby-1.2.0RC1/samples/rails/runner)
    ==  CreateRunners: migrating ==================================================
    — create_table(:runners)
       -> 0.1150s
       -> 0 rows
    ==  CreateRunners: migrated (0.1170s) =========================================

    Note, how “RAILS_ENV=production” is specified at the command-line to ensure the production environment.

  4. Copy MySQL Connector/J jar in GLASSFISH_HOME/lib as:
    ~/tools/glassfish/v2.1/glassfish/ >cp ~/tools/mysql-connector-java-5.1.6/mysql-connector-java-5.1.6-bin.jar ./lib

    This is required for connection to the MySQL database.

  5. Fire up GlassFish v2.1 as:
    ~/tools/glassfish/v2.1/glassfish/bin >./asadmin start-domain
    Starting Domain domain1, please wait.
    Default Log location is /Users/arungupta/tools/glassfish/v2.1/glassfish/domains/domain1/logs/server.log.
    Redirecting output to /Users/arungupta/tools/glassfish/v2.1/glassfish/domains/domain1/logs/server.log
    Domain domain1 started.
    Domain [domain1] is running [Sun GlassFish Enterprise Server v2.1 (9.1.1) (build b60e-fcs)] with its configuration and logs at: [/Users/arungupta/tools/glassfish/v2.1/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 runner ].
    Standard JMX Clients (like JConsole) can connect to JMXServiceURL:
    [service:jmx:rmi:///jndi/rmi://Macintosh-187.local:8686/jmxrmi] for domain management purposes.
    Domain listens on at least following ports for connections:
    [8080 8181 4848 3700 3820 3920 8686 ].
    Domain supports application server clusters and other standalone instances.

    The logs are created in “domains/domain1/logs/server.log”. Optionally, you can specify “–verbose” on the command-line to dump the log on the console itself.

  6. Create JDBC connection pool as:
    ~/tools/glassfish/v2.1/glassfish/bin >./asadmin create-jdbc-connection-pool –datasourceclassname com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource –restype javax.sql.DataSource –property “User=duke:Password=glassfish:URL=jdbc\:mysql\://localhost/runner_production” jdbc/runner_pool
    Command create-jdbc-connection-pool executed successfully.

  7. Create JDBC resource as:
    ~/tools/glassfish/v2.1/glassfish/bin >
    ="font-weight: bold;">./asadmin create-jdbc-resource –connectionpoolid jdbc/runner_pool jdbc/runner_production

    Command create-jdbc-resource executed successfully.
  8. Create Warbler config file as:
    ~/tools/jruby-1.2.0RC2/samples/rails/runner >../../../bin/jruby -S warble config
    cp /Users/arungupta/tools/jruby-1.2.0RC2/lib/ruby/gems/1.8/gems/warbler-0.9.12/generators/warble/templates/warble.rb config/warble.rb
  9. Edit “config/warble.rb” to bundle the required gems by adding the following fragment:
     # Include all gems which are used by the web application
      require “#{RAILS_ROOT}/config/environment”
      BUILD_GEMS = %w(warbler rake rcov)
      for gem in Gem.loaded_specs.values
        next if BUILD_GEMS.include?(gem.name)
        config.gems[gem.name] = gem.version.version
      end

    as specified here. And then explicitly specify the runtime gem dependency by adding the following line:

    config.gems += ["activerecord-jdbc-adapter"]

    right after the previous code fragment. The “activerecord-jdbc-adapter” dependency needs to be explicitly included because this is required only at the runtime and so not resolved correctly by the previous code fragment.

  10. And create the WAR file as:
    ~/tools/jruby-1.2.0RC2/samples/rails/runner >../../../bin/jruby -S warble
    mkdir -p tmp/war/WEB-INF/gems/specifications
    cp /Users/arungupta/tools/jruby-1.2.0RC2/lib/ruby/gems/1.8/specifications/rails-2.2.2.gemspec tmp/war/WEB-INF/gems/specifications/rails-2.2.2.gemspec
    mkdir -p tmp/war/WEB-INF/gems/gems
    . . .
    cp public/javascripts/prototype.js tmp/war/javascripts/prototype.js
    cp public/stylesheets/scaffold.css tmp/war/stylesheets/scaffold.css
    mkdir -p tmp/war/WEB-INF
  11. Deploy the WAR file …
    ~/tools/jruby-1.2.0RC2/samples/rails/runner >~/tools/glassfish/v2.1/glassfish/bin/asadmin deploy runner.war
    Command deploy executed successfully.

    After adding few entries the page at “http://localhost:8080/runner/runners” looks like:

So we are able to deploy a trivial Rails application as WAR file on GlassFish v2.1 and also leverage the JDBC connection pooling, that passes Test# 4.

Later blogs will show the remainder of tests. The current set of tests are available using the tags rubyonrails+glassfish+integrationtest.

Please leave suggestions on other TOTD (Tip Of The Day) that you’d like to see. A complete archive of all tips is available here.

Technorati: totd rubyonrails glassfish v2 jruby warbler connectionpooling jdbc jndi integrationtest

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot
Related posts:
  1. TOTD #72: JRuby and GlassFish Integration Test #3: JRuby 1.2.0 RC2 + Rails 2.2.x + GlassFish v3
  2. TOTD #70: JRuby and GlassFish Integration Test# 1: JRuby 1.2.0 RC1 + Rails 2.2.x + GlassFish Gem
  3. TOTD #71: JRuby and GlassFish Integration Test #2: JRuby 1.2.0 RC1 + Rails 2.2.x + GlassFish v3 Prelude
  4. TOTD #74: JRuby and GlassFish Integration Test #5: JRuby 1.2.0 RC2 + Rails 2.x.x + GlassFish + Redmine
  5. Rails and Java EE integration – Warbler instead of Goldspike

5 Comments »

  1. [Trackback] TOTD #70, #71, #72, #73 shows four integration tests that can ensure that the latest JRuby and GlassFish versions work nicely with each other. #70 showed how to create a trivial Rails application and run it using GlassFish Gem. #71…

    Comment by Arun Gupta's Blog — March 16, 2009 @ 11:48 am

  2. [Trackback] TOTD #70, #71, #72, #73 shows four integration tests that can ensure that the latest JRuby and GlassFish versions work nicely with each other. #70 showed how to create a trivial Rails application and run it using GlassFish Gem. #71…

    Comment by Arun Gupta's Blog — March 16, 2009 @ 11:53 am

  3. [Trackback] The GlassFish High Availability allows to setup a cluster of GlassFish instances and achieve highly scalable architecture using in-memory session state replication. This cluster can be very easily created and tested using the "clusterjsp" sample bundl…

    Comment by Arun Gupta's Blog — August 12, 2009 @ 6:10 am

  4. [Trackback] The GlassFish High Availability allows to setup a cluster of GlassFish instances and achieve highly scalable architecture using in-memory session state replication. This cluster can be very easily created and tested using the "clusterjsp" sample bundl…

    Comment by Arun Gupta's Blog — August 12, 2009 @ 9:14 am

  5. [...] TOTD #73: Deploying Rails application as WAR on GlassFish v2.1 [...]

    Pingback by TOTD #110: JRuby on Rails application using Oracle on GlassFish « Miles to go … — October 6, 2009 @ 12:24 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment

The views expressed on this blog are my own and do not necessarily reflect the views of Oracle.
Powered by WordPress