Successfully reported this slideshow.

Introduction to JRuby

2,202 views

Published on

Slides from the Introduction to JRuby work

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Introduction to JRuby

  1. 1. Introduction to JRuby Anthony Juckel ajuckel@gmail.com @ajuckel
  2. 2. What is Ruby?• Dynamic/duck-typed language• Strongly object oriented• Open class definitions• Closures, blocks, and generators, oh my!
  3. 3. What is JRuby?
  4. 4. What is JRuby?• JRuby == Ruby
  5. 5. What is JRuby?• JRuby == Ruby• JRuby != Ruby
  6. 6. What is JRuby?• JRuby == Ruby• JRuby != Ruby• JRuby == Ruby++
  7. 7. How to install it?• Download from jruby.org• Install via rvm (but not on this wifi)
  8. 8. 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
  9. 9. 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
  10. 10. 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
  11. 11. RubyGems• Gems can install library files • $ gem install json • require ‘json’• Gems can install scripts • $ gem install rspec • $ jruby --1.9 -S rspec
  12. 12. RubyGems• Some gems are only of interest on JRuby • jruby-openssl • activerecord-jdbc-adapter
  13. 13. RubyGems• Others have JRuby-specific versions • nokogiri • Uses libxml on MRI • Uses Java XML libraries on JRuby
  14. 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. 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. 16. Launch Time• What to do about it? • Tweak startup options to make JVM startup as fast as possible
  17. 17. Launch Time• JVM Options (prefixed with -J with jruby) • -client (-J-client) • -XX:+TieredCompilation • -Xshare:dump / -Xshare:on
  18. 18. Launch Time• JRuby Options • -X-C (turn off compilation) • --ng-server / --ng • --ng will get faster after several executions while the JVM optimizes
  19. 19. Launch Time ITERATIONS=1000 rspec1000 1.9.3 JRuby100 JRuby -X-C JRuby NailGun 10 1 0.1 1000 100000 10000000
  20. 20. Lab 3: Try it out• -J-client• -J-XX:+TieredCompilation• -J-Xshare:on• -X-C (turn off compilation)• --ng-server / --ng
  21. 21. Launch Time (cont.)• Can be improved, but for small tasks cannot (yet?) match MRI• What options are left? • Guard • Spork
  22. 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. 23. ActiveRecord• An Object-Relational Mapping (ORM) library• Developed as part of Rails• Maps database data to Ruby classes
  24. 24. ActiveRecord• activerecord: Core functionality• activerecord-*-adapter: Database-specific adapter layer
  25. 25. ActiveRecord• MRI’s activerecord-*-adapters use native database libraries.• JRuby can use JDBC drivers• Must have JDBC adapter on classpath• activerecord-jdbc*-adapters
  26. 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. 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. 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. 29. 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
  30. 30. Lab 5: Convert an App• $ jrlint• Update Gemfile • :platforms => :jruby • activerecord-jdbc*-adapter • :platforms => :ruby • pg, sqlite3
  31. 31. 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
  32. 32. 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!
  33. 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. 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. 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. 36. 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
  37. 37. Questions?• https://github.com/ajuckel/intro-to-jruby• @ajuckel• ajuckel@gmail.com

×