JRuby in the Enterprise
About Me
Advisory Engineer at Constant Contact

Spent several years working for Sun/Javasoft in
the distributed computing group

Email: jerry@gulla.com

Twitter: @jerrygulla

Github: github.com/jgulla
What is JRuby?


Just a Ruby interpreter running on the JVM

Ruby 1.8.7 & 1.9.2 compatible.
Text-To-Join

Distributed app

Written in Ruby as a Rails app

Four rails apps, actually

UI, API, Console, SMS Processor.
JRUBY.ORG
Why Ruby?


Ruby is…A dynamic, open source programming
language with a focus on simplicity and
productivity. It has an elegant syntax that is natural
to read and easy to write.
WHY SHOULD YOU SWITCH TO RUBY? IF YOU ARE HAPPY WITH
 PERL OR PYTHON, YOU DON'T HAVE TO. BUT IF YOU DO FEEL
 THERE MUST BE A BETTER LANGUAGE, RUBY MAY BE YOUR
   LANGUAGE OF CHOICE. LEARNING A NEW LANGUAGE IS
  HARMLESS. IT GIVES YOU NEW IDEAS AND INSIGHTS. YOU
 DON'T HAVE TO SWITCH, JUST LEARN AND TRY IT. YOU MAY
   FIND YOURSELF COMFORTABLE ENOUGH WITH RUBY TO
                DECIDE TO SWITCH TO IT.

           -- YUKIHIRO MATSUMOTO (“MATZ”)
 HTTP://LINUXDEVCENTER.COM/PUB/A/LINUX/2001/11/29/
                      RUBY.HTML
Rails 3.x

“Optimized for programmer happiness”.
Why Not Grails?

Grails is great

Not as big of a break with Java

Wanted a bigger shift to bring new ways of doing
things into perspective.
Why JRuby?
High performance

  JVM Highly Tuned

Real threading

  Many “pure Ruby” shops using JRuby just for
  this

Vast array of libraries

  No need to abandon Java libraries.
Why JRuby Continued

Deployment Options

  Can deploy as a WAR file

Monitoring Options

  JVM tools to monitor all available

Speed of Development.
Don’t Freak the Ops
     Guys Out
  HTTP://UPLOAD.WIKIMEDIA.ORG/WIKIPEDIA/EN/F/F4/THE_SCREAM.JPG
http://commons.wikimedia.org/wiki/File:Everest-fromKalarPatar.jpg




          Scalability
Concurrency is a Myth




 HTTP://WWW.IGVITA.COM/2008/11/13/CONCURRENCY-IS-A-MYTH-IN-RUBY/
ILYA GRIGORIK
                HTTP://WWW.IGVITA.COM/ABOUT/




 JRUBY IS, IN FACT, THE ONLY RUBY IMPLEMENTATION
THAT WILL ALLOW YOU TO NATIVELY SCALE YOUR RUBY
 CODE ACROSS MULTIPLE CORES. BY COMPILING RUBY
 TO BYTECODE AND EXECUTING IT ON THE JVM, RUBY
THREADS ARE MAPPED TO OS THREADS WITHOUT A GIL
IN BETWEEN - THAT'S AT LEAST ONE REASON TO LOOK
                     INTO JRUBY.
   HTTP://WWW.IGVITA.COM/2008/11/13/CONCURRENCY-IS-A-MYTH-IN-RUBY/
“Enterprise” JRuby
“Enterprise” JRuby Cont.
Maven

Liquibase

JDBC

JNDI

Containers

You’re favorite commercial, open-source or home-
grown library.
Let’s Begin
          http://commons.wikimedia.org/wiki/File:Begin_road.JPG
http://commons.wikimedia.org/wiki/File:Panama_Canal_Locks_Construct.jpeg




    Set up Ruby
Ruby Version Manager




    HTTPS://RVM.BEGINRESCUEEND.COM/
Installing RVM


Via git repo (preferred method)
bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)

Other methods at https://rvm.beginrescueend.com/rvm/install/
Install JRuby (via rvm)
rvm install jruby-1.6.3

When it’s done, use it:

  rvm use jruby-1.6.3

List of other ruby versions to install:

  rvm list known

List versions of ruby you have installed:

  rvm list
JRuby Without RVM


Assume Java, JRuby in your path

jruby -S gem ...
Install Rails


gem install rails

Can pick a specific version with a
“-v” flag.
Create Your Rails App
Jruby.org hosts a rails template

  Sets things up to run under native ruby or JRuby

  You can do this yourself, but it’s handier to start this
  way

rails new uberconf_demo --template=http://jruby.org

  Works with native ruby or JRuby.
Jruby.org Template
Demo
Packaging Your App




   http://en.wikipedia.org/wiki/File:Distribution_differences.jpg
JRuby/Rails/WAR files
In the Java world, WAR files are standard

Fortunately, we have bundler

  Gem to package your Rails app as a WAR file

Can even make it “Executable” for stand-alone
testing!

http://caldersphere.rubyforge.org/warbler/
Install it


Easy!

gem install warbler
Run It




             http://en.wikipedia.org/wiki/File:Jeanette_Kwakye_cropped.jpg

Easy!

warble war
Other Useful Options
warble compiled    # Feature: precompile all Ruby files

warble config      # Generate a configuration file to customize your archive

warble executable # Feature: make an executable archive

warble gemjar     # Feature: package gem repository inside a war

warble pluginize # Install Warbler tasks in your Rails application

warble version    # Display version of Warbler

warble war       # Create the project war file

warble war:clean # Remove the project war file

warble war:debug # Dump diagnostic information
Maven

In the Java world, Maven is still pretty popular

  For better or for worse

You can use Maven to invoke warbler to package
your app.
Database Migrations




http://en.wikipedia.org/wiki/File:Connochaetes_taurinus_-Wildebeest_crossing_river_-East_Africa.jpg
Database Migrations
Rails uses Rake for ActiveRecord migrations

Nice, but

  Hard to scale to multiple developers

  Hard to deploy to QA/Production systems
  without using ruby toolset

  Paranoid DBAs can’t see the SQL before
  running it.
Enter

liquibase.org

Version control for your database

Other frameworks (Grails) will be adopting it for
migrations

Language/Database agnostic.
Liquibase

Nothing special about using Liquibase with Rails

Use ActiveRecord migrations for initial dev/testing,
then migrate to Liquibase

When ready, just take “snapshot” of DB config,
then roll out new Liquibase changesets.
Handy Liquibase Commands
Generate initial changelog:
java -jar liquibase.jar --classpath=mysql-connector-
java-5.1.14-bin.jar --url=jdbc:mysql://localhost/
api_development --driver=com.mysql.jdbc.Driver --
username=demo --password=password --
changeLogFile=db-changelog.xml generateChangeLog

Drop all tables so you can start over:
dropAll

Run updates against a DB:
liquibase --changeLogFile=myChangeLog update

Just output SQL, don’t run: updateSQL
USING
 LIQUIBASE
WITH MAVEN
Liquibase with Rails Directly


https://github.com/redbeard/liquibase-rails

Supposed to add “drop in” migration tasks for
JRuby/Rails.
Liquibase Changeset
JNDI Database Access
       DATABASE.YML
Servlet Filters



You’ll need a custom web.xml config

Copy WARBLER_HOME/web.xml.erb to
  config/web.xml.erb

  Look for it at ~/.rvm/gems/jruby-1.6.2/gems/
  warbler-1.3.1/web.xml.erb
     HTTP://EN.WIKIPEDIA.ORG/WIKI/FILE:DICHROIC_FILTERS.JPG
web.xml.erb Filter Entry
Java Integration

Nothing special, just the usual JRuby/Java
integration

Looks for JARs in the container classpath, WAR or
in yourapp/lib
Calling Java




HTTPS://GITHUB.COM/JRUBY/JRUBY/WIKI/CALLINGJAVAFROMJRUBY
Tips, Tricks and Lessons




 http://en.wikipedia.org/wiki/File:Coolidge_after_signing_indian_treaty.jpg
Warbler Config


generate a warbler config file

warble config
Pre-Compile Ruby Classes



config.features = %w(compiled)
Set Min/Max Runtimes
Set JNDI Source Name
Set Root Path for App


By default, the root of your app is the WAR name

This may not be what you want.
Set the Root Path
Create file jboss-web.xml in root of app directory




Tell warbler to include it via warble.rb
Bundle Up Files into A
Gem
Can be much faster to build/deploy depending on
how you do it (there are a lot of files!)

Make sure to test it
Make Your War Executable


Command Line:
warble executable war

 In warble.rb:
 config.features = %w(executable)
Rails Console


Rails has a “Console Mode” for testing

Great for trying out ActiveRecord queries, etc.

Just use “rails console” to invoke it.
Gem Versions

Be sure to lock down Gem versions when you’re
happy with them

Don’t want any last minute “upgrades!”

Use bundler

Put Gemfile.lock under source control.
Basic Versioning Rules
After bundling, always check your Gemfile.lock
into version control

After updating your Gemfile, always run
bundle install first.

When running an executable, ALWAYS use
bundle exec [command]

   The only exception is the rails command!
http://yehudakatz.com/2011/05/30/gem-versioning-and-bundler-doing-it-right/
Handle Database Connection Outage




This is the “default” 500 error page.
You can Customize this
but...


You can do better
Create “Status” URL


Want a well known URL that Nagios or a Load
Balancer can check

Should return something useful (and specific to
your test).
Insert Your Own Middleware
Create lib/db_check.rb
Insert Your Own Middleware
Insert it into the stack (application.rb)
You Now Have Generic Status Ability




Extend for other things useful to your application.
HTTP://TORQUEBOX.ORG/
IDEs
Resources
Books
Websites

http://www.jruby.org/

http://jrubyconf.com/

http://vimeo.com/groups/87639
(talks from 2010 JRubyConf)

http://www.engineyard.com/open-source
Twitter Users


http://twitter.com/#!/headius

http://twitter.com/#!/nicksieger

http://twitter.com/#!/jruby
Questions?
Thank You!
Don’t forget to fill out a session evaluation!

JRuby in the enterprise