the Java Virtual Machine: • Open Source • Main site at http://jruby.org/ • Source Code at https://github.com/jruby/jruby • Just as Matz writes C so we don't have to, the JRuby team writes Java so we don't have to. • Both 1.8 and 1.9 compatible. • JRuby's lead developers are Charles Nutter, Thomas Enebo, Ola Bini and Nick Sieger. 2 Tuesday, August 28, 2012
nothing of the Java programming language, only of a particular binary format, the class file format." - from http://java.sun.com/developer/technicalArticles/DynTypeLang/ • Groovy • JRuby • Scala • Clojure • JavaScript 3 Tuesday, August 28, 2012
and stable platform • excellent performance, garbage collection, and multithreading • performance tuning and profiling tools (e.g. jvisualvm) • access to a huge number of libraries, e.g. Hadoop, XML, SOAP • great for driving Java API's • excellent I18N (internationalization) support • highly scalable • adds to the pool of systems on which Ruby can run (e.g. mainframes with JVM’s) 4 Tuesday, August 28, 2012
Ruby software such as rspec, rake, 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/ 5 Tuesday, August 28, 2012
• virtually no new infrastructure, means lower sysadmin costs • can still use Java web servers, profiling tools, etc. • can continue to use Java libraries • can decide to implement a hybrid Java-Ruby product where both sides run in the same process (the JVM) • if converting entire product to Ruby, can replace Java code with Ruby code in increments instead of all at once • less learning required In these ways, JRuby lowers the cost of experimenting with Ruby (or switching entirely to it), increasing the probability of adoption and success. 6 Tuesday, August 28, 2012
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). 7 Tuesday, August 28, 2012
to all Properties instances by # opening the Properties class and defining method 'pp': require 'java' java_import java.util.Properties class Properties def pp s = '' sort.each do |key, value| s << sprintf("%-34s %s\n", key, value) end s end end puts java.lang.System.properties.pp 9 Tuesday, August 28, 2012
camel-case named methods, and conventional reader and 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/ default default= getDefault get_default setDefault set_default Also, JRuby makes the Enumerable interface available to some kinds of Java objects, enabling the above, and the following: Locale.iSOCountries.each { |country_code| puts country_code } Locale.iSOLanguages.each { |language_code| puts language_code } 10 Tuesday, August 28, 2012
'java' require 'rspec' java_import org.apache.commons.lang3.StringUtils describe 'StringUtils' do subject { StringUtils } it "should consider a 1-space string to be blank" do subject.blank?(' ').should be_true end end # >CLASSPATH=commons-lang.jar rspec string_utils_spec.rb # . # # Finished in 0.039 seconds # 1 example, 0 failures 13 Tuesday, August 28, 2012
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 Special Java Support Calls 15 Tuesday, August 28, 2012
that can produce either Java class files, or Java source (run jrubyc --help for details). rvm jruby echo "puts 'hello'" > hello.rb jrubyc hello.rb ls -l hello* javap -v hello | less Free code obfuscation: Since only the .class file is need to run your app, you can withhold the source code from your users and provide only the .class file. The .class file can be decompiled, but will be difficult to comprehend, since Java byte code is similar in concept to assembler code. 16 Tuesday, August 28, 2012
run JRuby commands (jruby, jirb) with their distinctive names beginning with j. rvm eliminates the need for this. rvm jruby which ruby # /Users/kbennett/.rvm/rubies/jruby-1.6.5/bin/ruby which jruby # /Users/kbennett/.rvm/rubies/jruby-1.6.5/bin/jruby ls -l /Users/kbennett/.rvm/rubies/jruby-1.6.5/bin # lrwxr-xr-x 1 kbennett staff 5 Jan 19 13:25 ruby -> jruby 17 Tuesday, August 28, 2012
issues. Run on current rspec code base: >jrlint JRuby-Lint version 0.3.1 For more on gem compatibility see http:// wiki.jruby.org/C-Extension-Alternatives ./Gemfile:8: [gems, warning] Found gem 'nokogiri' which is reported to have some issues: For best results, use the pure-Java version of Nokogiri (default after v1.5). ./lib/spec/example/example_methods.rb:37: [timeout, warning] Timeout in JRuby does not work in many cases ./lib/spec/extensions/instance_exec.rb:20: [threads, warning] Use of Thread.critical is discouraged. Use a Mutex instead. ... Processed 323 files in 10.25 seconds Found 7 items 18 Tuesday, August 28, 2012
in 1.9 mode. To automate this, ensure that the environment variable JRUBY_OPTS will contain --1.9. Most developers will accomplish this by inserting the following line in their shell startup script (e.g. .bashrc, .zshrc): export JRUBY_OPTS=--1.9 This can be overridden by eliminating or replacing that environment variable’s value: JRUBY_OPTS= ruby ... # or export JRUBY_OPTS= 20 Tuesday, August 28, 2012
sharing a single JRuby virtual machine instance by JRuby scripts to eliminate the delay associated with JVM startup. # 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 bug at http://jira.codehaus.org/browse/JRUBY-5611. # Start up the shared Nailgun server: JRUBY_OPTS=’’ jruby –-ng-server time ruby -e "puts 123" #123 #ruby -e "puts 123" 2.89s user 0.14s system 217% cpu 1.390 total time ruby --ng -e "puts 123" #123 #ruby --ng -e "puts 123" 0.00s user 0.00s system 0% cpu 0.488 total 21 Tuesday, August 28, 2012
from the Java Servlet API to Ruby’s Rack API. • enables Ruby applications to run on virtually any existing Java web server • regularly tested with Tomcat 6/7, Jetty 6/7, JBoss 5/6, Resin 4, and GlassFish 3 (see http://www.engineyard.com/blog/2011/taking-stock-jruby-web-servers/ for an overview of Ruby adapters to Java web servers) 22 Tuesday, August 28, 2012
out of a Rails, Merb, or Rack-based application. The intent is to provide a minimal, flexible, ruby-like way to bundle all your application files for deployment to a Java application server.” - http://kenai.com/projects/warbler/pages/Home From the Rails root directory, to create a war file for your Java application server: > warble war Or, to create a file containing the Tomcat web server so that it can be run directly from your command line: # can execute on cmd line like this: java -jar myapp.war > warble executable war 23 Tuesday, August 28, 2012
any Ruby application.” “Because of its built-in support for features such as clustering, TorqueBox is often distinguished as enterprise-grade software. But it does this without any of the drawbacks we programmers often associate with “enterprisey” things.” - Joe Kutner, “Deploying with JRuby” • supports Rack based applications (e.g. Rails, Sinatra) • built on top of JBoss • clustering - clusters can easily communicate & share session data • messaging (can use Rabbit MQ) • cacheing • scheduling • daemons 25 Tuesday, August 28, 2012
from outside your application in order to access information about, or modify, your running application. • separate from your running application • have their own port, different from app users’ port (offering better security) • can access using management tools such as JConsole (see example, p. 174-5, “Deploying with JRuby”) 26 Tuesday, August 28, 2012
with the JDK (Java Development Kit). For more info on thread states, see: http://docs.oracle.com/ javase/7/docs/api/java/lang/ Thread.State.html 27 Tuesday, August 28, 2012
written in JRuby • uses Java Swing library, so illustrates Java interop • available at: https://github.com/keithrbennett/life-game-viewer 30 Tuesday, August 28, 2012
• Deploying with JRuby Book: http://pragprog.com/book/jkdepj/deploying-with-jruby • Charlie Nutter's JRuby Slide Show: http://www.slideshare.net/CharlesNutter/rubyconf-uruguay-2010-jruby • Replacing Java Incrementally with JRuby: http://www.youtube.com/watch?v=l3jz8OqdBd0 • JRuby Polyglot Heaven by Thomas E Enebo and Charles Oliver Nutter: http://www.youtube.com/ watch?v=ikhmBdQljVI • Check out how LinkedIn uses JRuby on its Front-end: http://www.youtube.com/watch? v=qZcmF3yonjs • JRuby as a Better Language for the JVM - http://krbtech.wordpress.com/2009/02/26/jruby-a- better-language-for-the-javavirtual-machine/ • Life Game Viewer (JRuby Swing app) Github Repo: https://github.com/keithrbennett/life-game-viewer • This slideshow: https://speakerdeck.com/u/keithrbennett/p/jruby-the-synergy-of-ruby-and-the- jvm 31 Tuesday, August 28, 2012