Professional Documents
Culture Documents
Denis Lutz
12/06/11
Agenda
Motivation: Problem, Solution JRuby as language Java and Ruby Integration Ruby Community Show Case Demo Usage areas for JRuby
12/06/11
Motivation
12/06/11
Motivation
Lets redefine our thinking What is our task? What is the shortest path to the solution?
Since Not the solving of complicated tasks is the goal but solving our task in the easiest possible way
12/06/11
Theory
Java has a huge set of tools JVM scripting languages are present (Groovy) You can find a tool for everything The java magazines are writing about scripting languages
12/06/11
Projectwide scripting support not the rule, more an exception Ant = scripting language => XML is our scripting language If scripting language integration => Ant / Spring is the environment Adhoc scripting areas are randomly used by single devs Instead a major scripting support strategy is missing Young developers face only java or ant Young devs are on their own to find more expressive languages
12/06/11
Entering JRuby
12/06/11
Goal
scripting tool as one of main and known tools scripting language as the surrounding environment and entry point (not Ant) clearly decleared scripting language standard definition of taks areas for the scripting language easy and powerfull communication with the underlying operating system powerfull language features instead of XML tools (Ant, Maven) knowledge reuse because of definition of several scripting areas in a projekt from GUIs and IDEs to automation and console procedures from enterprisy java community to a fun community in ruby
Denis Lutz/ JRuby in Java Projects 8
12/06/11
What is JRuby
JRuby = implementation of Ruby in Java Runs in JVM and integrates perfectly into Java environments very expressive mature huge community powered by the Ruby on Rails framework
12/06/11
Front End (MVC) Build management Testing, specifically integration testing Import/Export APIs Code generation
12/06/11
10
Facets of JRuby
12/06/11
11
Download Jruby at www.jruby.org Extract Add to $PATH Test your installation with jruby v on the console
Command execution with jruby S command jruby S irb -> Will start the JRuby console
12/06/11
12
Simple Syntax Everything is an object Blocks Self contained ( contains most useful libs out of the box) Open classes Principle of least surprise
12/06/11
13
12/06/11
14
12/06/11
15
Everything is an object
12/06/11
16
12/06/11
17
public void callJRuby() { ScriptEngineManager m = new ScriptEngineManager(); ScriptEngine rubyEngine = m.getEngineByName("jruby"); if (rubyEngine==null) throw new RuntimeException("Did not find my ruby engine"); ScriptContext context = rubyEngine.getContext(); context.setAttribute("world","Programmierer",ScriptContext.ENGINE_SCOPE); try{ File f = new File("hello1.rb"); BufferedReader br = new BufferedReader(new FileReader(f)); rubyEngine.eval(br, context); // (1) } catch (ScriptException e) { e.printStackTrace(); } catch (FileNotFoundException fnfe) { System.err.println(fnfe.getMessage()); } }
12/06/11
18
Lets call Java from Jruby und use your legacy systems
require 'java java_import java.lang.System => Java::JavaLang::System version = System.getProperties["java.runtime.version"] => "1.6.0_17-b04-248-10M3025
import java.util.ArrayList list = ArrayList.new => #<Java::JavaUtil::ArrayList:0x2bf09a31> ruby-1.8.7-p334 :042 > iterator = list.iterator => #<#<Class:01x41a7c484>:0x367c218e> ruby-1.8.7-p334 :043 > iterator.java_class => class java.util.AbstractList$Itr ruby-1.8.7-p334 :044 > list.get(1) NativeException: java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
12/06/11 Denis Lutz/ JRuby in Java Projects 19
Rubys Community
12/06/11
20
Community
Very strong community, why? Got powered by Ruby on Rails (MVC Framework) Fun oriented community Motivated hobby programmers, who can be 9-5 employees ;-) Get into positive cycle of beeing threated well and wanting more ruby GEMS = ruby libraries, created by the community
12/06/11
21
12/06/11
22
12/06/11
23
12/06/11
24
The task
we want to communicate with a REST API should be usable within a build or inside a java class of course Should provide an easy API for our java class
12/06/11
25
12/06/11
26
12/06/11
27
12/06/11
28
12/06/11
29
We knew from one site about the best fitting GEM for our task We had no troubles installing it I can try my written code on the console it just works, doesnt matter what you install its my experience after two years using ruby, yours will be the same
12/06/11
30
12/06/11
31
Yes its right, Rails got over JSF (java standard) in the meanwhile Is this still a new and not known technology for you?
12/06/11
32
Yes, possible and widely used JRuby on Rails Frontend as war file in Tomcat Rails Front Ends in Java Projects possible everything else is waste of money and developers frustration No risk, tell to your java influenced boss: its just a war file No server infrastructure changes needed
12/06/11
33
12/06/11
34
Basic problem: ANT is accepted, Maven the new standard None of them is sufficient for the task we want to do Why?
12/06/11
35
Projectsetup, its in the Wiki, well because Ant cant do it ;-) Folders and File management if else , can you declare it out of your head now in Ant? Declare a method in your build? Oh, yea no problem let me just look into Ant API Loading of fixtures, possible with some XML setup again Server startup? I am doing it by hand and each new developer has to learn the specifics Maven the biggest hype that helps me even less then Ant
12/06/11
36
What we want
Full language power in your build, not XML We can still call our old Ant tasks UNIX operations as if we would be on the console Method declarations, as simple as possible Reuse of a language we already use not learning new XML frameworks
12/06/11
37
project setup methods operating system calls objects in your build file operations Ivy integration
12/06/11
38
12/06/11
39
12/06/11
40
12/06/11
41
12/06/11
42
12/06/11
43
Its difficult to process files as well as different formats in java Import or Export is mostly a focused single task Can be done separated, by one developer No requirement to do it in java Customer data import is a very common and important task
Use JRuby to provide an import / export API Data processing much faster Easy CSV, Excel, XML processing Generate projects specific formats for import
Denis Lutz/ JRuby in Java Projects 44
12/06/11
12/06/11
45
(Integration) Testing
12/06/11
46
Integration Testing
Integration tests are the best candidate to do it with JRuby Abstract, mimal input, very high coverage Are easy to keep out of the java environment system Can be perfectly done with pure ruby
12/06/11
47
Webrat Example
12/06/11
48
Webrat
GUI Testing API Evaluates the pure HTML output No browser setup or dependency to run your tests Write your tests fast in ruby Cover the complete application workflow with minimal effort Can be run automatically in backgroud while developing Excellent to give fast feedback about application stability
12/06/11
49
12/06/11
50
Expected result
All mentioned task areas can be done easier in JRuby More choices of tools, as would it be only with Java Knowledge reuse in different project areas Work is getting more lightweigt From java IDEs to fast editors Perfect interaction between different parts of the build
12/06/11
51
My personal, subjective impression You will reach more, having to know less
12/06/11
52
Further informations
http://www.jruby.org/ http://kenai.com/projects/jruby/pages/Home
Jruby console with jruby S irb http://www.cygwin.com/ (Unix Console replacement for Windows)
12/06/11
53
12/06/11
54