Introduction to JRuby
        Anthony Juckel
      ajuckel@gmail.com
           @ajuckel
What is Ruby?
• Dynamic/duck-typed language
• Strongly object oriented
• Open class definitions
• Closures, blocks, and generators, oh my!
What is JRuby?
What is JRuby?
• JRuby == Ruby
What is JRuby?
• JRuby == Ruby
• JRuby != Ruby
What is JRuby?
• JRuby == Ruby
• JRuby != Ruby
• JRuby == Ruby++
How to install it?
• Download from jruby.org
• Install via rvm (but not on this wifi)
What can I do with it?
• Run Ruby code (1.8 or 1.9)
• Access Java libraries from Ruby
• Embed Ruby within a Java application
Lab 1: Install it!
• $ jruby -e ‘puts “Hello, World!”’
• $ jruby hello.rb
• $ jruby --1.9 hello.rb
• $ jirb
• $ jruby --1.9 -S irb
• $ JRUBY_OPTS=”--1.9” jirb
RubyGems
• Gems are units of software reuse in the
  Ruby world
• Most Gems will work the same in JRuby as
  in MRI
• Gems with native code _may_ work, but
  seek alternatives
RubyGems
• Gems can install library files
 • $ gem install json
 • require ‘json’
• Gems can install scripts
 • $ gem install rspec
 • $ jruby --1.9 -S rspec
RubyGems
• Some gems are only of interest on JRuby
 • jruby-openssl
 • activerecord-jdbc-adapter
RubyGems
• Others have JRuby-specific versions
 • nokogiri
   • Uses libxml on MRI
   • Uses Java XML libraries on JRuby
Lab 2: Gems
• $ gem search -r [gem]   • jruby-openssl
• $ gem install [gem]     • rspec
• $ gem list              • json
• $ gem rdoc              • bundler
• $ gem server            • $ cd lab02
                          • $ rspec
Launch Time
• Yes, it can be a problem
• MRI: Quick to start, speed remains constant
• JVM: Slow to start, faster as hotspots are
  optimized
Launch Time
• What to do about it?
 • Tweak startup options to make JVM
    startup as fast as possible
Launch Time
• JVM Options (prefixed with -J with jruby)
 • -client (-J-client)
 • -XX:+TieredCompilation
 • -Xshare:dump / -Xshare:on
Launch Time
• JRuby Options
 • -X-C (turn off compilation)
 • --ng-server / --ng
    • --ng will get faster after several
       executions while the JVM optimizes
Launch Time
           ITERATIONS=1000 rspec

1000
                                          1.9.3
                                          JRuby
100                                       JRuby -X-C
                                          JRuby NailGun
 10


   1


 0.1
    1000           100000      10000000
Lab 3: Try it out
• -J-client
• -J-XX:+TieredCompilation
• -J-Xshare:on
• -X-C (turn off compilation)
• --ng-server / --ng
Launch Time (cont.)
• Can be improved, but for small tasks
  cannot (yet?) match MRI
• What options are left?
 • Guard
 • Spork
Launch Time (cont.)
• Iterate using MRI
  • Quickly verify small changes
• Integrate using JRuby
  • Less frequent
  • May have longer test suites, perhaps more
    work, can afford to pay the start up cost
ActiveRecord
• An Object-Relational Mapping (ORM)
  library
• Developed as part of Rails
• Maps database data to Ruby classes
ActiveRecord
• activerecord: Core functionality
• activerecord-*-adapter: Database-specific
  adapter layer
ActiveRecord
• MRI’s activerecord-*-adapters use native
  database libraries.
• JRuby can use JDBC drivers
• Must have JDBC adapter on classpath
• activerecord-jdbc*-adapters
Lab 4: ActiveRecord
• $ gem install activerecord-jdbc-adapter
•   $ gem install activerecord-jdbcpostgresql-adapter

•   $ gem install activerecord-jdbcsqlite3-adapter

•   $ gem install activerecord-derby-adapter
Converting Rails App
• Rails itself (aside from ActiveRecord) is
  very compatible with JRuby
• Gems used in any given project may or may
  not be.
• jruby-lint can help identify potential issues.
Converting Rails App
• What are some known issues?
 • Some ObjectSpace methods disabled by
    default
 • Native code use
 • Kernel#fork / backtick operator / system
    usage
Lab 5: Convert an App
• $ cd graphy # Rails app targeting MRI 1.9.3
• If you’ve got MRI installed, look at current
  app
  • bundle install
  • rake db:create db:migrate db:seed
  • rails s
Lab 5: Convert an App
• $ jrlint
• Update Gemfile
 • :platforms => :jruby
   • activerecord-jdbc*-adapter
 • :platforms => :ruby
  • pg, sqlite3
Lab 5: Convert an App
• After performing the updates, test the app
  in JRuby
• Also test the app in MRI, if available
 • Having dependencies properly scoped
    should allow you to execute the app in
    either environment
Lab 5: Convert an App
• Have an app along with you? Try
  converting it now!
• Install/Run jrlint
• Update Gemfile (if necessary)
• bundle install
• Run your tests/specs or rails s.
• Ask questions!
Web App Deployment
• A Rails app is no good without a solid
  deployment environment
• MRI deployment strategies leverage MRI
  strengths
• Need JVM deployment options to leverage
  JVM strengths
Web App Deployment
• MRI: HTTP server -> Ruby process(es)
 • Mongrel / Unicorn
 • Passenger
• JRuby: HTTP server -> JRuby threads
 • Rails app: config.threadsafe!
 • Be mindful of the threadsafety of your
    dependencies
Web App Deployment
• Deploying in Java Application Server
• Most self-contained option: WARbler!
 • JRuby web app -> deployable WAR
• Other options
 • Trinidad / Glassfish Gem
 • Torquebox
Lab 6: Deploy it
• We’ve ported an app to JRuby. Now
  deploy it!
 • $ gem install warbler
 • $ warble config
 • Browse/edit config/warble.rb
 • $ warble war # builds WAR
 • $ cp app.war [tomcat-home]/webapps
Questions?

• https://github.com/ajuckel/intro-to-jruby
• @ajuckel
• ajuckel@gmail.com

Introduction to JRuby

  • 1.
    Introduction to JRuby Anthony Juckel ajuckel@gmail.com @ajuckel
  • 2.
    What is Ruby? •Dynamic/duck-typed language • Strongly object oriented • Open class definitions • Closures, blocks, and generators, oh my!
  • 3.
  • 4.
    What is JRuby? •JRuby == Ruby
  • 5.
    What is JRuby? •JRuby == Ruby • JRuby != Ruby
  • 6.
    What is JRuby? •JRuby == Ruby • JRuby != Ruby • JRuby == Ruby++
  • 7.
    How to installit? • Download from jruby.org • Install via rvm (but not on this wifi)
  • 8.
    What can Ido with it? • Run Ruby code (1.8 or 1.9) • Access Java libraries from Ruby • Embed Ruby within a Java application
  • 9.
    Lab 1: Installit! • $ jruby -e ‘puts “Hello, World!”’ • $ jruby hello.rb • $ jruby --1.9 hello.rb • $ jirb • $ jruby --1.9 -S irb • $ JRUBY_OPTS=”--1.9” jirb
  • 10.
    RubyGems • Gems areunits of software reuse in the Ruby world • Most Gems will work the same in JRuby as in MRI • Gems with native code _may_ work, but seek alternatives
  • 11.
    RubyGems • Gems caninstall library files • $ gem install json • require ‘json’ • Gems can install scripts • $ gem install rspec • $ jruby --1.9 -S rspec
  • 12.
    RubyGems • Some gemsare only of interest on JRuby • jruby-openssl • activerecord-jdbc-adapter
  • 13.
    RubyGems • Others haveJRuby-specific versions • nokogiri • Uses libxml on MRI • Uses Java XML libraries on JRuby
  • 14.
    Lab 2: Gems •$ gem search -r [gem] • jruby-openssl • $ gem install [gem] • rspec • $ gem list • json • $ gem rdoc • bundler • $ gem server • $ cd lab02 • $ rspec
  • 15.
    Launch Time • Yes,it can be a problem • MRI: Quick to start, speed remains constant • JVM: Slow to start, faster as hotspots are optimized
  • 16.
    Launch Time • Whatto do about it? • Tweak startup options to make JVM startup as fast as possible
  • 17.
    Launch Time • JVMOptions (prefixed with -J with jruby) • -client (-J-client) • -XX:+TieredCompilation • -Xshare:dump / -Xshare:on
  • 18.
    Launch Time • JRubyOptions • -X-C (turn off compilation) • --ng-server / --ng • --ng will get faster after several executions while the JVM optimizes
  • 19.
    Launch Time ITERATIONS=1000 rspec 1000 1.9.3 JRuby 100 JRuby -X-C JRuby NailGun 10 1 0.1 1000 100000 10000000
  • 20.
    Lab 3: Tryit out • -J-client • -J-XX:+TieredCompilation • -J-Xshare:on • -X-C (turn off compilation) • --ng-server / --ng
  • 21.
    Launch Time (cont.) •Can be improved, but for small tasks cannot (yet?) match MRI • What options are left? • Guard • Spork
  • 22.
    Launch Time (cont.) •Iterate using MRI • Quickly verify small changes • Integrate using JRuby • Less frequent • May have longer test suites, perhaps more work, can afford to pay the start up cost
  • 23.
    ActiveRecord • An Object-RelationalMapping (ORM) library • Developed as part of Rails • Maps database data to Ruby classes
  • 24.
    ActiveRecord • activerecord: Corefunctionality • activerecord-*-adapter: Database-specific adapter layer
  • 25.
    ActiveRecord • MRI’s activerecord-*-adaptersuse native database libraries. • JRuby can use JDBC drivers • Must have JDBC adapter on classpath • activerecord-jdbc*-adapters
  • 26.
    Lab 4: ActiveRecord •$ gem install activerecord-jdbc-adapter • $ gem install activerecord-jdbcpostgresql-adapter • $ gem install activerecord-jdbcsqlite3-adapter • $ gem install activerecord-derby-adapter
  • 27.
    Converting Rails App •Rails itself (aside from ActiveRecord) is very compatible with JRuby • Gems used in any given project may or may not be. • jruby-lint can help identify potential issues.
  • 28.
    Converting Rails App •What are some known issues? • Some ObjectSpace methods disabled by default • Native code use • Kernel#fork / backtick operator / system usage
  • 29.
    Lab 5: Convertan App • $ cd graphy # Rails app targeting MRI 1.9.3 • If you’ve got MRI installed, look at current app • bundle install • rake db:create db:migrate db:seed • rails s
  • 30.
    Lab 5: Convertan App • $ jrlint • Update Gemfile • :platforms => :jruby • activerecord-jdbc*-adapter • :platforms => :ruby • pg, sqlite3
  • 31.
    Lab 5: Convertan App • After performing the updates, test the app in JRuby • Also test the app in MRI, if available • Having dependencies properly scoped should allow you to execute the app in either environment
  • 32.
    Lab 5: Convertan App • Have an app along with you? Try converting it now! • Install/Run jrlint • Update Gemfile (if necessary) • bundle install • Run your tests/specs or rails s. • Ask questions!
  • 33.
    Web App Deployment •A Rails app is no good without a solid deployment environment • MRI deployment strategies leverage MRI strengths • Need JVM deployment options to leverage JVM strengths
  • 34.
    Web App Deployment •MRI: HTTP server -> Ruby process(es) • Mongrel / Unicorn • Passenger • JRuby: HTTP server -> JRuby threads • Rails app: config.threadsafe! • Be mindful of the threadsafety of your dependencies
  • 35.
    Web App Deployment •Deploying in Java Application Server • Most self-contained option: WARbler! • JRuby web app -> deployable WAR • Other options • Trinidad / Glassfish Gem • Torquebox
  • 36.
    Lab 6: Deployit • We’ve ported an app to JRuby. Now deploy it! • $ gem install warbler • $ warble config • Browse/edit config/warble.rb • $ warble war # builds WAR • $ cp app.war [tomcat-home]/webapps
  • 37.

Editor's Notes

  • #2 Target audience(s!): Ruby developers new to the JVM; Java developers new to the Ruby Language\nRuby folks, the material in the first few labs may be quite familiar, but we’ll likely be moving quickly.\nJava folks, we’re going to gloss over language features\n
  • #3 You can’t really talk about what JRuby is without first defining Ruby\nThis is not a primer on Ruby as a language. More a whirlwind tour of the JRuby ecosystem.\n
  • #4 JRuby is ruby syntax and semantics through and through. Deviations are bugs.\nJVM as a platform has different sweetspots than the MRI VM as a platform\nFor a given set of tasks (CPU-bound performance, high concurrency needs, esoteric platforms) JRuby is a better\n
  • #5 JRuby is ruby syntax and semantics through and through. Deviations are bugs.\nJVM as a platform has different sweetspots than the MRI VM as a platform\nFor a given set of tasks (CPU-bound performance, high concurrency needs, esoteric platforms) JRuby is a better\n
  • #6 JRuby is ruby syntax and semantics through and through. Deviations are bugs.\nJVM as a platform has different sweetspots than the MRI VM as a platform\nFor a given set of tasks (CPU-bound performance, high concurrency needs, esoteric platforms) JRuby is a better\n
  • #7 \n
  • #8 \n
  • #9 \n
  • #10 \n
  • #11 \n
  • #12 \n
  • #13 \n
  • #14 View the source, run the tests, perhaps play with the code.\n\nOnce everyone’s comfortable running the tests, it’s time to move on.\n
  • #15 \n
  • #16 \n
  • #17 \n
  • #18 \n
  • #19 \n
  • #20 \n
  • #21 \n
  • #22 \n
  • #23 \n
  • #24 \n
  • #25 \n
  • #26 \n
  • #27 \n
  • #28 \n
  • #29 \n
  • #30 SCOPING dependencies to platform\n
  • #31 \n
  • #32 \n
  • #33 \n
  • #34 \n
  • #35 Tuesday at 2:00, Joe Kutner will be talking further about deployment\n
  • #36 \n
  • #37 \n