Upgrade to Pro — share decks privately, control downloads, hide ads and more …

JRuby - How Java and Ruby meet on the JVM, PolyConf 2015

JRuby - How Java and Ruby meet on the JVM, PolyConf 2015

Slides from the talk at PolyConf
Poznan, Poland
July 2015

4f477cfca5c1d10d09157c07cdfa3af4?s=128

Alissa Bonas

July 03, 2015
Tweet

Transcript

  1. JRuby How Java and Ruby meet on the JVM Alissa

    Bonas @ PolyConf 2015
  2. What

  3. • An implementation of the Ruby programming language atop the

    JVM • Written in Java What is JRuby
  4. JRuby project • Free and open source software • http://jruby.org/

    • https://github.com/jruby/jruby
  5. Some JRuby facts • Exists since 2001 • Can run

    Ruby on Rails apps since 2006 • Tightly integrated with Java
  6. Versions support • Jruby 1.7.x ◦ Supports Ruby 1.9.3 ◦

    Experimental Ruby 2.0 support • Jruby 9.0.0.0 - in the works ◦ Supports Ruby 2.2
  7. Why

  8. Reasons to use JRuby • Integrate Java and Ruby projects

    • Enjoy libraries in both languages • Bonus ◦ Multithreading ◦ Packaging
  9. Packaging • Simple deployment/packaging • Creating a standard war file

  10. Multithreading • Green threads vs native threads ◦ GIL (global

    interpreter lock) • One process instead of multiple
  11. Things to consider • Native C extensions • Slower startup

  12. None
  13. How

  14. • RVM • Rbenv • Download and extract • Packaged

    for various OS Install JRuby
  15. Install JRuby with RVM • Fast and easy • Quick

    switch between several Rubies
  16. Managing Rubies with RVM

  17. Ruby versions flavors

  18. JRuby in action

  19. Java Calling Ruby • Eval of Ruby code • Sharing

    variables with a Ruby script • Reading the return value
  20. Java Calling Ruby • JRuby Embed (aka Red Bridge) ◦

    Embed core ◦ JSR 223 - Java’s scripting engine ◦ BSF (Bean Scripting Framework) • No need to compile Ruby into bytecode
  21. JRuby Embed core • Add jruby.jar to your Java project

    • specify JRUBY_HOME ◦ needed if JRuby installed via RVM
  22. Ruby script example # GET http://10.35.0.23:8080/api require ‘net/http’ endpoint =

    URI::HTTP.build(:host => @host, :port => @port) endpoint.path = ‘/api’ request = Net::HTTP::Get.new(endpoint.to_s) resp = Net::HTTP.start(endpoint.host, endpoint.port) { | http | http.request(request) } resp.body
  23. Java calling Ruby example

  24. Output { "versions": [ "v1beta3", "v1" ] }

  25. require ‘java’ java_import java.lang.System jruby_home = System.getProperties["jruby.home"] puts jruby_home ------------------------------------------------------------

    Output: /home/abonas/.rvm/rubies/jruby-1.7.20 Ruby Calling Java
  26. Example of Rails with JRuby • Open source Java project

    - Spago BI • Several Java reports engines • Adding a Ruby on Rails reports engine
  27. Reports Engine 1 Reports Engine 2 Reports Engine 3 UI

    Reports Engine 4 http /engine1 http /engine2 http /engine3 http /engine4 SOA Tomcat SOA SOA SOA SOA
  28. Preparation steps • Use JDBC database drivers • Package the

    application • Deploy
  29. Database drivers • JDBC drivers • Specify them in Gemfile

    • Specify them in config/database.yml
  30. Packaging

  31. Packaging • Use Warbler • Create a .war file •

    Flexible configuration possibilities ◦ option to precompile the classes - avoid surprise in production
  32. Rails production mode • Enabled by default in web.xml

  33. Deploy and Enjoy!

  34. Credits and sources • Tomcat-logo by The Apache Tomcat Project

    Team • Mug by Alex Getty from the Noun Project • Diamond by MarkieAnn Packer from the Noun Project • Box by Cornelius Danger from the Noun Project • Clapperboard by Edgar Vargas from the Noun Project • Questions by Rediffusion from the Noun Project • Rails by Luis Martins from the Noun Project