Slide 1

Slide 1 text

JRuby How Java and Ruby meet on the JVM Alissa Bonas @ PolyConf 2015

Slide 2

Slide 2 text

What

Slide 3

Slide 3 text

● An implementation of the Ruby programming language atop the JVM ● Written in Java What is JRuby

Slide 4

Slide 4 text

JRuby project ● Free and open source software ● http://jruby.org/ ● https://github.com/jruby/jruby

Slide 5

Slide 5 text

Some JRuby facts ● Exists since 2001 ● Can run Ruby on Rails apps since 2006 ● Tightly integrated with Java

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

Why

Slide 8

Slide 8 text

Reasons to use JRuby ● Integrate Java and Ruby projects ● Enjoy libraries in both languages ● Bonus ○ Multithreading ○ Packaging

Slide 9

Slide 9 text

Packaging ● Simple deployment/packaging ● Creating a standard war file

Slide 10

Slide 10 text

Multithreading ● Green threads vs native threads ○ GIL (global interpreter lock) ● One process instead of multiple

Slide 11

Slide 11 text

Things to consider ● Native C extensions ● Slower startup

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

How

Slide 14

Slide 14 text

● RVM ● Rbenv ● Download and extract ● Packaged for various OS Install JRuby

Slide 15

Slide 15 text

Install JRuby with RVM ● Fast and easy ● Quick switch between several Rubies

Slide 16

Slide 16 text

Managing Rubies with RVM

Slide 17

Slide 17 text

Ruby versions flavors

Slide 18

Slide 18 text

JRuby in action

Slide 19

Slide 19 text

Java Calling Ruby ● Eval of Ruby code ● Sharing variables with a Ruby script ● Reading the return value

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

JRuby Embed core ● Add jruby.jar to your Java project ● specify JRUBY_HOME ○ needed if JRuby installed via RVM

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

Java calling Ruby example

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

Example of Rails with JRuby ● Open source Java project - Spago BI ● Several Java reports engines ● Adding a Ruby on Rails reports engine

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

Preparation steps ● Use JDBC database drivers ● Package the application ● Deploy

Slide 29

Slide 29 text

Database drivers ● JDBC drivers ● Specify them in Gemfile ● Specify them in config/database.yml

Slide 30

Slide 30 text

Packaging

Slide 31

Slide 31 text

Packaging ● Use Warbler ● Create a .war file ● Flexible configuration possibilities ○ option to precompile the classes - avoid surprise in production

Slide 32

Slide 32 text

Rails production mode ● Enabled by default in web.xml

Slide 33

Slide 33 text

Deploy and Enjoy!

Slide 34

Slide 34 text

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