jror deploying rails on jruby

28
JRoR Deploying Rails on JRuby Nordija A/S Nørgaardsvej 7 DK-2800 Kgs. Lyngby www.nordija.com +45 70 20 25 10 Per Olesen <[email protected]>

Upload: elliando-dias

Post on 15-May-2015

1.456 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: JRoR Deploying Rails on JRuby

JRoRDeploying Rails on JRuby

Nordija A/S Nørgaardsvej 7 DK-2800 Kgs. Lyngby www.nordija.com +45 70 20 25 10

Per Olesen <[email protected]>

Page 2: JRoR Deploying Rails on JRuby

Agenda

• JRuby

• Rails Integration

• Future Perspectives

Page 3: JRoR Deploying Rails on JRuby

JRuby

Page 4: JRoR Deploying Rails on JRuby

Why JRuby?

• Dynamic Languages are hot and cool :-)

• To leverage the lovely Ruby language on the JVM

• To make all the nice, proven libraries of the Java platform available to Ruby code

• The best of both worlds?

• To ease the transition from Java into dynamic languages

• To fight M$ and the DLR?

Page 5: JRoR Deploying Rails on JRuby

JRuby: The Project

• Ruby on the Java VM

• Partly sponsored by Sun– Charles Oliver Nutter (Sun employee now)– Thomas Enebo (Sun employee now)– Ola Bini (ThoughtWorker)– Nick Sieger (Digital River, Inc)

• Goal– Provide a platform to execute Ruby code on the JVM

Page 6: JRoR Deploying Rails on JRuby

JRuby Runtime

JRuby: The Platform

Ruby source

InterpreterJIT

Compiler

Ruby std. Libs (in ruby)

The JVM

Ruby native codelibs rewritten in

Java(bytecode)

JIT compiledrubycode

(bytecode)

Page 7: JRoR Deploying Rails on JRuby

Example: The IRB Console

• Download the zip and unzip

• Set JRUBY_HOME=path to unzip place

• Set PATH=%JRUBY_HOME%\bin;%PATH%

• Type: jirb <enter>

• Take a tour in the unzipped dirs

• Live example.....

Page 8: JRoR Deploying Rails on JRuby

Java Integration: (J)Ruby into Java

• With JSE6, you can use JSR-223 APIs– Needs: https://scripting.dev.java.net/

• Or else, there is BSF integration

• Or, simply code it– Ruby runtime = Ruby.getDefaultInstance()– runtime.evalScript(”...”)

• Live example....

Page 9: JRoR Deploying Rails on JRuby

Java Integration: Java into (J)Ruby

• Low-level API– Wrapping in JavaObject, JavaMethod, JavaField– java_method.invoke(java_object, java_field, ...)

• High-level API– Wrapping low-level API in a JavaProxy– A JavaProxy has all the methods of the Java object

wrapped– Calls the actual Java object methods using low-level

API– Output from a proxy is automatically wrapped

• Live example....

Page 10: JRoR Deploying Rails on JRuby

Performance Tips

• Turn on -server VM (JVM setting)

• Turn on JIT (JRuby setting)

• Turning off ObjectSpace– Breaks ability to look into the heap from app. code

• Turn on thread-pooling– Ruby uses green-threads (yuk!)– Some Ruby libs spawns a lot of threads– Expensive with native threads

Page 11: JRoR Deploying Rails on JRuby

Rails Integration

Page 12: JRoR Deploying Rails on JRuby

Why Rails Integration?

• Rails deployment can be complex– Rails is not thread-safe– Multi-process deployment– Needs load-balancing front-end– Harder to monitor– Not everything in one deployment unit

• Rails deployment for a Java-shop– A completely new platform to learn– Not what Joe-systems-management likes– Existing (Java appserver) setup not leveraged

Page 13: JRoR Deploying Rails on JRuby

Parts of Rails Integration

• ActiveRecord-JDBC– Actually separate, but important part of rails

• RailsIntegration– A plugin for rails to do war building– Some session handling code– A dispatcher servlet into JRuby runtime

Page 14: JRoR Deploying Rails on JRuby

Installing Rails Integration

• First, you need a rails project– Gem install rails -y– Rails helloworld

• Then, install rails plugin

script/plugin install svn://rubyforge.org/var/svn/jruby-extras/trunk/railsintegration/plugins/goldspike

Page 15: JRoR Deploying Rails on JRuby

Installing ActiveRecord-JDBC

• Gem install activerecord-jdbc -y

• config\environment.rb– After the ”require File.join(...'boot')” line, add this:

if RUBY_PLATFORM =~ /java/ require 'rubygems' require 'active_record' require 'active_record/connection_adapters/jdbc_adapter' RAILS_CONNECTION_ADAPTERS = %w(jdbc)end

Page 16: JRoR Deploying Rails on JRuby

Using Rails Integration

• See the new tasks with: ”rake --tasks”

• Using war packaging– Rake war:standalone:create– Rake war:standalone:run

Page 17: JRoR Deploying Rails on JRuby

Using ActiveRecord-JDBC

• Generate model and controller– script\generate model post ...– script\generate controller Post– Add ”scaffold :post”

• Edit config\database.yml– Adapter: mysql– Url: jdbc:mysql://...

• Migrate

• Add jdbc driver jar dependency in ”config\war.rb”:– maven_library 'mysql', 'mysql-connector-java', '3.1.12'

Page 18: JRoR Deploying Rails on JRuby

Deployment in a war

• Why?– One deployment unit– Leverage JEE appservers multi-threading

• RailsServlet– Instantiates a pool of Ruby runtimes– Pooling overcomes threading issues– Rails is initialized in each runtime– Dispatches incoming requests into rails

Page 19: JRoR Deploying Rails on JRuby

Future Perspectives

Page 20: JRoR Deploying Rails on JRuby

Why is this hard? (part 1)

• No RubySpec (yet)– There is a wiki working on this

• Large parts of the Ruby platform is in native code– Must be rewritten in Ruby or Java– Rubinius might help out here– Lots of 3. part libs will not be rewritten

(as part of the JRuby project)

• Parts of the Ruby platform are un-doable on JSE platform– Green threading– File operations– Fine grained timing

Page 21: JRoR Deploying Rails on JRuby

Why is this hard? (part 2)

• Dynamic languages are different!– You can do things that are not possible in statically

typed languages

• The JVM is not a good fit for dynamic languages (yet)

• JVM fixes for dynamic languages– Invokedynamic– Code hotswapping– JSR-292

Page 22: JRoR Deploying Rails on JRuby

Performance on the JVM

• Today– Significantly slower on JRuby– But, not many optimizations done (yet)– Faster on some places where hotspot kicks in

• Future– Invokedynamic, hotswapping, etc...– Might enable better performance– The hotspot compiler might kick-ass then

• What is ”performance enough”?– How much do you really need if you can scale out?– Ease of Development is important

Page 23: JRoR Deploying Rails on JRuby

Ruby 1.9

• Current Ruby 1.8 mostly run on MRI

• Ruby 1.9– The upcoming implementation to measure against– Will be a true bytecode-based VM

• VM bytecode targeted ruby language– Will give better ability to tailor performance to the

language

Page 24: JRoR Deploying Rails on JRuby

Other X-platform Ruby Projects

• XRuby– Ruby to Java Bytecode Compiler

• Rubinius– A Ruby implementation in Ruby– Based on Smalltalk-80 (Squeak)– Rewriting the complete Ruby standard lib. in Ruby– Others can use the rewrite on their implementations

• Ruby on .Net compiler– Gardens Point Ruby.NET compiler (MSIL based)– M$ IronRuby (DLR based)

Page 25: JRoR Deploying Rails on JRuby

M$: Dynamic Language Runtime

• A Dynamic Runtime on top of the CLR

• Type System– A unified type-system for dynamic languages and the

.Net platform– Passing objects between IronRuby, IronPython and

.Net platform

• Part of IronPython sources now (I think)

• SilverLight will utilize this (I think)

Page 26: JRoR Deploying Rails on JRuby

Who are doing the JRuby Dance?

• ThoughtWorks– Mingle– CruiseControl.rb (simply ruby)– A lot of Ruby projects

• Others, I guess!?– This is new stuff– Promising and steaming hot, but new stuff!

Page 27: JRoR Deploying Rails on JRuby

Resources

• http://jruby.codehaus.org/• http://www.headius.com/rubyspec/• http://rubyforge.org/projects/jruby-extras/

• Me– Per Olesen <[email protected]>– http://techpolesen.blogspot.com/

Page 28: JRoR Deploying Rails on JRuby