• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Jruby synergy-of-ruby-and-java
 

Jruby synergy-of-ruby-and-java

on

  • 1,089 views

 

Statistics

Views

Total Views
1,089
Views on SlideShare
1,087
Embed Views
2

Actions

Likes
0
Downloads
6
Comments
0

1 Embed 2

http://www.linkedin.com 2

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Jruby synergy-of-ruby-and-java Jruby synergy-of-ruby-and-java Presentation Transcript

  • JRuby -- Java/Ruby Synergy Keith Bennett keithrbennett --at-- gmail.com @keithrbennett
  • What is JRuby?An implementation of the Ruby language on the JavaVirtual Machine:• Open Source• Main site at http://jruby.org/• Source Code at https://github.com/jruby/jruby• Matz writes C so we dont have to; the JRuby team writes Java so we dont have to.• Both1.8 and 1.9 compatible.
  • Non-Java Languages on the JVM"The Java virtual machine knows nothing of the Javaprogramming language, only of a particular binary format,the class file format."- from http://java.sun.com/developer/technicalArticles/DynTypeLang/• JRuby• Scala• Clojure• Groovy• JavaScript
  • Technical Benefits of Running Ruby on the JVM• mature and stable platform• more portable – runs on older and obscure OSs that have JVMs• access to a huge number of libraries, e.g. XML, SOAP• excellent performance, garbage collection, and multithreading• performance tuning and profiling tools (e.g. jvisualvm)• excellent I18N (internationalization) support• highly scalable• can drive Java APIs
  • Benefits of Ruby on the JVM for Enterprise Java Shops• all the technical benefits previously mentioned, plus:• can use Ruby DB tools such as Active Record (esp. useful for migrations), DBI, etc.• can introduce development staff to scripting languages in general, and Ruby in particular• can automate one-off tasks that were previously too cost prohibitive to automate• can grow Ruby expertise that can be applied to various kinds of tasks (e.g. testing, scripting, web development)• can introduce it in a way that will not be distributed with production code• can use it for saner and more rapid development of Swing or SWT client-side GUI apps (see JRuby as a Better Language for the JVM - http://krbtech.wordpress.com/2009/02/26/jruby-a- better-language-for-the-javavirtual-machine/
  • Benefits of Using JRuby for Ruby Developers• all the technical benefits previously mentioned, plus:• enables expanding customer base to include Java shops, many of which are very large and well funded.• enables creation of better solutions by increasing the set of parts that can be assembled into a solution -- sometimes Java is better (e.g. XML, SOAP support).
  • Calling Java from Rubyrequire javajava.lang.System.properties.sort.each do |k,v| printf("%-30s %sn", k, v) }enddisplays:awt.nativeDoubleBuffering trueawt.toolkit apple.awt.CToolkitfile.encoding UTF-8file.encoding.pkg sun.iofile.separator /gopherProxySet falsejava.awt.printerjob apple.awt.CPrinterJobjava.class.pathjava.class.version 50.0…
  • Adding Ruby methods to Java Classesrequire javajava_import java.lang.Systemclass System def self.properties_pp s = properties.sort.each do |k,v| s << sprintf("%-34s %sn", k, v) end s endendputs System.properties_pp
  • JRuby Method GenerationJRuby generates snake-cased named methods for Javacamel-case named methods, and conventional readerand writer methods à la attr_accessor:# The java.util.Locale class contains only getDefault and setDefault.# JRuby adds the others:jruby-1.6.5 :003 > puts Locale.methods.sort.grep /[Dd]efault/defaultdefault=getDefaultget_defaultsetDefaultset_defaultAlso, JRuby makes the Enumerable interface availableto some kinds of Java objects, enabling the above, andthe following:Locale.iSOCountries.each { |country_code| puts country_code }Locale.iSOLanguages.each { |language_code| puts language_code }
  • JRuby to Java Type Conversionsjruby-1.6.5 :027 > 123.class => Fixnumjruby-1.6.5 :028 > 123.to_java.class => Java::JavaLang::Long
  • Calling JRuby from Javaimport org.jruby.embed.ScriptingContainer;public class ScriptingContainerExample { public static void main(String[] args) { ScriptingContainer container = new ScriptingContainer(); container.put("$greeting", "Hello from JRuby!"); // optional // container.setLoadPaths(aJavaListOfDirectories); // optional container.setCompatVersion(org.jruby.CompatVersion.RUBY1_9); // optional container.runScriptlet("puts $greeting"); }}// >javac -cp ".:$JRUBY_JAR" ScriptingContainerExample.java// >java -cp ".:$JRUBY_JAR" ScriptingContainerExample// Hello from JRuby!
  • Special Java Support Calls• java_import - Imports java classes• java_send - for specifying which function to call, by signature• java_alias - for creating an alias for a Java function with signature• java_method - for creating a callable reference to a Java function with signature• field_accessor - for accessing Java instance variables, even private ones• add_class_annotation - Adds a class annotation to a Ruby class• become_java! - "promotes" a Ruby class to be a Java class• include - can be used to signal that this class implements a Java interface
  • Compiling JRubyThe JRuby distribution comes with jrubyc, a compiler thatcan produce either Java class files, or Java source (runjrubyc --help for details).rvm jrubyecho "puts hello" > hello.rbjrubyc hello.rbls -l hello*javap -v hello | lessFree code obfuscation: Since only the .class file is need torun your app, you can withhold the source code fromyour users and provide only the .class file.The .class file can be decompiled, but will be difficult tocomprehend, since Java byte code is similar in concept toassembler code.
  • Omitting the ‘J’ in JRubyNormally, it is necessary to run JRuby commands (jruby,jirb) with their distinctive names beginning with j.rvm eliminates the need for this.rvm jrubywhich ruby# /Users/kbennett/.rvm/rubies/jruby-1.6.5/bin/rubywhich jruby# /Users/kbennett/.rvm/rubies/jruby-1.6.5/bin/jrubyls -l /Users/kbennett/.rvm/rubies/jruby-1.6.5/bin# lrwxr-xr-x 1 kbennett staff 5 Jan 19 13:25 ruby -> jruby
  • Defaulting to 1.9Most people will want JRuby to run in 1.9 mode.To automate this, ensure that the environment variableJRUBY_OPTS will contain --1.9.Most developers will accomplish this by inserting thefollowing line in their shell startup script(e.g. .bashrc, .zshrc):export JRUBY_OPTS=--1.9This can be overridden by eliminating or replacing thatenvironment variable’s value:JRUBY_OPTS= ruby ...# orexport JRUBY_OPTS=
  • NailgunNailgun is a tool packaged with JRuby that enablessharing a single JRuby virtual machine instance by JRubyscripts to eliminate the delay associated with JVMstartup.# If $JRUBY_OPTS contains “--1.9” so that JRuby runs# in 1.9 mode by default, then this must be overridden# for the Nailgun server to start.# See but at http://jira.codehaus.org/browse/JRUBY-5611.# Start up the shared Nailgun server:JRUBY_OPTS= jruby –ng-servertime ruby -e "puts 123"#123#ruby -e "puts 123" 2.89s user 0.14s system 217% cpu 1.390 totaltime ruby --ng -e "puts 123"#123#ruby --ng -e "puts 123" 0.00s user 0.00s system 0% cpu 0.488 total
  • Warbler“Warbler is a gem that makes a .war file out of a Rails,Merb, or Rack-based application. The intent is to providea minimal, flexible, ruby-like way to bundle all yourapplication files for deployment to a Java applicationserver.”- http://kenai.com/projects/warbler/pages/HomeSimilarly, you can combine jruby.jar and your Ruby codeinto a single .jar file that can be executed simply andautomatically by the operating system.
  • JVisualVMJVisualVM is a monitoring, profiling, and troubleshootingtool packaged with the JDK (Java Development Kit).
  • Sample Multithreaded Programnum_threads = 5threads = [](0...num_threads).each do |n| threads << Thread.new do loop { puts "#{ * (5 * n)}#{n}n" } endendthreads.each { |thread| thread.join }
  • Multithreading CPU Usage by Ruby Versionrvm 1.8ruby multithread.rbPID COMMAND %CPU TIME #TH #WQ #POR #MREG RPRVT RSHRD RSIZE367 Terminal 105.8 01:20.36 5/1 1 123- 166+ 267M+ 43M 278M+1104 ruby 82.7 00:13.40 1/1 0 18 28 1248K 240K 2108Krvm 1.9ruby multithread.rbPID COMMAND %CPU TIME #TH #WQ #POR #MREG RPRVT RSHRD RSIZE1249 ruby 167.6 00:20.85 7/2 0 41 61 2972K+ 240K 4632K+367 Terminal 153.0 03:49.55 5/1 1 125 267 716M+ 43M 772M+# What happened? I thought 1.9 used a GIL (Global Interpreter Lock)?rvm jrubyruby multithread.rbID COMMAND %CPU TIME #TH #WQ #POR #MREG RPRVT RSHRD RSIZE1364 java 137.1 00:25.84 23/2 1 201 236 80M 3144K 93M367 Terminal 120.8 05:05.62 6/1 2 127- 291 813M+ 43M 901M+
  • References• JRuby Main Site: http://jruby.org• JRuby Book: http://pragprog.com/book/jruby/using-jruby• Charlie Nutters JRuby Slide Show: http://www.slideshare.net/CharlesNutter/rubyconf-uruguay-2010-jruby• JRuby as a Better Language for the JVM - http://krbtech.wordpress.com/ 2009/02/26/jruby-a-better-language-for-the-javavirtual-machine/• Multilanguage Swing Github Repo: https://github.com/keithrbennett/multilanguage-swing• This slideshow: http://www.slideshare.net/keithrbennett/jruby-synergyofrubyandjava