1JRuby in Java ProjectsDenis Lutz
2Motivation: Problem, SolutionJRuby as languageJava and Ruby Integration Ruby CommunityShow Case DemoUsage areas for JRubyAgenda
3Motivation
4Lets redefine our thinkingWhat 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 wayMotivation
5Java has a huge set of toolsJVM scripting languages are present (Groovy)You can find a tool for everythingThe java magazines are writing about scripting languagesTheory
6Projectwide scripting support not the rule, more an exceptionAnt = scripting language =>  XML is our scripting languageIf scripting language integration => Ant / Spring is the environmentAdhoc scripting areas are randomly used by single devsInstead a major scripting support strategy is missingYoung developers face only java or ant Young devs are on their own to find more expressive languagesReal world praxis
7Entering JRuby
8scripting tool as one of main and known toolsscripting language as the surrounding environment and entry point (not Ant)clearly decleared scripting language standarddefinition of taks areas for the scripting languageeasy and powerfull communication with the underlying operating systempowerfull language features instead of XML tools (Ant, Maven)knowledge reuse because of definition of several scripting areas in a projektfrom GUIs and IDEs to automation and console proceduresfrom enterprisy java community to a fun community in rubyGoal
9What is JRubyJRuby = 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 framework10Front End (MVC)Build managementTesting, specifically integration testingImport/Export APIsCode generationWe can use JRuby in many project areas
11Facets of JRuby
12Download Jruby at www.jruby.orgExtractAdd to  $PATHTest your installation with “jruby –v” on the consoleCommand execution with“jruby –S ‘command’ ”jruby –S irb        -> Will start the JRuby consoleGet started with JRuby in 3 mins…
13Simple SyntaxEverything is an objectBlocksSelf contained ( contains most useful libs out of the box)Open classesPrinciple of least surpriseMost imressive language features
14Simple object creation
15Create your class and use it, lightweight and easy!
16Everything is an object
17Blocks, the most amazing and powerful feature ever
18Lets call JRuby from Javapublic 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());   }}
19require '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.ArrayListlist = 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: 0Lets call Java from Jruby und use your legacy systems
20Ruby’s Community
21Very strong community, why?Got powered by Ruby on Rails (MVC Framework)Fun oriented communityMotivated hobby programmers, who can be 9-5 employees ;-)Get into positive cycle of beeing threated well and wanting more rubyGEMS = ruby libraries, created by the communityCommunity
22Endless amounts of gems
23Find most popular gems in one place, rated by the community
24Live Demo: Using a GEM
25we want to communicate with a REST APIshould be usable within a buildor inside a java class of courseShould provide an easy API for our java classThe taskEasiest solution (a base for it) ?
26Choose your gemHTTPartyChoose HTTParty as the gem for REST communication
27Install the GEM
28Write our nice little class
29Use it immidiatelly on the console to play with it
30We knew from one site about the best fitting GEM for our taskWe had no troubles installing itI can try my written code on the consoleit just works, doesn’t matter what you installits my experience after two years using ruby, yours will be the sameWhy was this great?
31Integration with Java
32Yes its right, Rails got over JSF (java standard) in the meanwhileIs this still a new and not known technology for you?Rails (Ruby) popularity
33Yes, possible and widely usedJRuby on Rails Frontend as war file in TomcatRails Front Ends in Java Projects possible everything else is waste of money and developers frustrationNo risk, tell to your java influenced boss: „its just a war file“  No server infrastructure changes neededDeploy a Ruby on Rails Application in a java warjruby -S gem install -y rails warbler$RAILS_APP_ROOT/jruby -S warble war
34Java Build with Rake
35Rake as build tool in Java projectsBasic problem:ANT is accepted, Maven the „new standard“None of them is sufficient for the task we want to doWhy?
36Projectsetup, 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 APILoading of fixtures, possible with some XML setup againServer 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 AntProblems with Ant and others…
37Full language power in your build, not XMLWe can still call our old Ant tasksUNIX operations as if we would be on the consoleMethod declarations, as simple as possibleReuse of a language we already usenot learning new XML frameworksWhat we want
38Rake does all you want :project setupmethodsoperating system callsobjects in your buildfile operationsIvy integrationRake is the solution, rubys build tool
39Rake in 3 mins, nothing is easier!
40Call classic and custom Ant tasks from rake if needed
41Operating system communicationFile managementFolder managementTalk to your operating system as from the console
42Some rake possibilities = just full ruby power
43In/Out API for your project
44Its difficult to process files as well as different formats in javaImport or Export is mostly a focused single taskCan be done separated, by one developerNo requirement to do it in javaCustomer data import is a very common and important taskUse JRuby to provide an import / export APIData processing much fasterEasy CSV, Excel, XML processing Generate projects specific formats for importJRuby as your In/Out API of any project
45JRuby as your In/Out API of any project
46(Integration) Testing
47Integration tests are the best candidate to do it with JRubyAbstract, mimal input, very high coverageAre easy to keep out of the java environment systemCan be perfectly done with pure rubyWEBRAT GEM as JRuby LibraryIntegration Testing
48Webrat Example
49GUI Testing APIEvaluates the pure HTML outputNo browser setup or dependency to run your testsWrite your tests fast in ruby Cover the complete application workflow with minimal effortCan be run automatically in backgroud while developingExcellent to give fast feedback about application stabilityWebrat
50Expected Result from Jruby integration
51All mentioned task areas can be done easier in JRubyMore choices of tools, as would it be only with JavaKnowledge reuse in different project areasWork is getting more lightweigtFrom java IDEs to fast editorsPerfect interaction between different parts of the buildExpected result

JRuby in Java Projects

  • 1.
    1JRuby in JavaProjectsDenis Lutz
  • 2.
    2Motivation: Problem, SolutionJRubyas languageJava and Ruby Integration Ruby CommunityShow Case DemoUsage areas for JRubyAgenda
  • 3.
  • 4.
    4Lets redefine ourthinkingWhat 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 wayMotivation
  • 5.
    5Java has ahuge set of toolsJVM scripting languages are present (Groovy)You can find a tool for everythingThe java magazines are writing about scripting languagesTheory
  • 6.
    6Projectwide scripting supportnot the rule, more an exceptionAnt = scripting language => XML is our scripting languageIf scripting language integration => Ant / Spring is the environmentAdhoc scripting areas are randomly used by single devsInstead a major scripting support strategy is missingYoung developers face only java or ant Young devs are on their own to find more expressive languagesReal world praxis
  • 7.
  • 8.
    8scripting tool asone of main and known toolsscripting language as the surrounding environment and entry point (not Ant)clearly decleared scripting language standarddefinition of taks areas for the scripting languageeasy and powerfull communication with the underlying operating systempowerfull language features instead of XML tools (Ant, Maven)knowledge reuse because of definition of several scripting areas in a projektfrom GUIs and IDEs to automation and console proceduresfrom enterprisy java community to a fun community in rubyGoal
  • 9.
    9What is JRubyJRuby= implementation of Ruby in Java
  • 10.
    Runs in JVMand integrates perfectly into Java environments
  • 11.
  • 12.
  • 13.
    huge community poweredby the Ruby on Rails framework10Front End (MVC)Build managementTesting, specifically integration testingImport/Export APIsCode generationWe can use JRuby in many project areas
  • 14.
  • 15.
    12Download Jruby atwww.jruby.orgExtractAdd to $PATHTest your installation with “jruby –v” on the consoleCommand execution with“jruby –S ‘command’ ”jruby –S irb -> Will start the JRuby consoleGet started with JRuby in 3 mins…
  • 16.
    13Simple SyntaxEverything isan objectBlocksSelf contained ( contains most useful libs out of the box)Open classesPrinciple of least surpriseMost imressive language features
  • 17.
  • 18.
    15Create your classand use it, lightweight and easy!
  • 19.
  • 20.
    17Blocks, the mostamazing and powerful feature ever
  • 21.
    18Lets call JRubyfrom Javapublic 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()); }}
  • 22.
    19require '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.ArrayListlist = 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: 0Lets call Java from Jruby und use your legacy systems
  • 23.
  • 24.
    21Very strong community,why?Got powered by Ruby on Rails (MVC Framework)Fun oriented communityMotivated hobby programmers, who can be 9-5 employees ;-)Get into positive cycle of beeing threated well and wanting more rubyGEMS = ruby libraries, created by the communityCommunity
  • 25.
  • 26.
    23Find most populargems in one place, rated by the community
  • 27.
  • 28.
    25we want tocommunicate with a REST APIshould be usable within a buildor inside a java class of courseShould provide an easy API for our java classThe taskEasiest solution (a base for it) ?
  • 29.
    26Choose your gemHTTPartyChooseHTTParty as the gem for REST communication
  • 30.
  • 31.
    28Write our nicelittle class
  • 32.
    29Use it immidiatellyon the console to play with it
  • 33.
    30We knew fromone site about the best fitting GEM for our taskWe had no troubles installing itI can try my written code on the consoleit just works, doesn’t matter what you installits my experience after two years using ruby, yours will be the sameWhy was this great?
  • 34.
  • 35.
    32Yes its right,Rails got over JSF (java standard) in the meanwhileIs this still a new and not known technology for you?Rails (Ruby) popularity
  • 36.
    33Yes, possible andwidely usedJRuby on Rails Frontend as war file in TomcatRails Front Ends in Java Projects possible everything else is waste of money and developers frustrationNo risk, tell to your java influenced boss: „its just a war file“ No server infrastructure changes neededDeploy a Ruby on Rails Application in a java warjruby -S gem install -y rails warbler$RAILS_APP_ROOT/jruby -S warble war
  • 37.
  • 38.
    35Rake as buildtool in Java projectsBasic problem:ANT is accepted, Maven the „new standard“None of them is sufficient for the task we want to doWhy?
  • 39.
    36Projectsetup, its inthe 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 APILoading of fixtures, possible with some XML setup againServer 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 AntProblems with Ant and others…
  • 40.
    37Full language powerin your build, not XMLWe can still call our old Ant tasksUNIX operations as if we would be on the consoleMethod declarations, as simple as possibleReuse of a language we already usenot learning new XML frameworksWhat we want
  • 41.
    38Rake does allyou want :project setupmethodsoperating system callsobjects in your buildfile operationsIvy integrationRake is the solution, rubys build tool
  • 42.
    39Rake in 3mins, nothing is easier!
  • 43.
    40Call classic andcustom Ant tasks from rake if needed
  • 44.
    41Operating system communicationFilemanagementFolder managementTalk to your operating system as from the console
  • 45.
    42Some rake possibilities= just full ruby power
  • 46.
    43In/Out API foryour project
  • 47.
    44Its difficult toprocess files as well as different formats in javaImport or Export is mostly a focused single taskCan be done separated, by one developerNo requirement to do it in javaCustomer data import is a very common and important taskUse JRuby to provide an import / export APIData processing much fasterEasy CSV, Excel, XML processing Generate projects specific formats for importJRuby as your In/Out API of any project
  • 48.
    45JRuby as yourIn/Out API of any project
  • 49.
  • 50.
    47Integration tests arethe best candidate to do it with JRubyAbstract, mimal input, very high coverageAre easy to keep out of the java environment systemCan be perfectly done with pure rubyWEBRAT GEM as JRuby LibraryIntegration Testing
  • 51.
  • 52.
    49GUI Testing APIEvaluatesthe pure HTML outputNo browser setup or dependency to run your testsWrite your tests fast in ruby Cover the complete application workflow with minimal effortCan be run automatically in backgroud while developingExcellent to give fast feedback about application stabilityWebrat
  • 53.
    50Expected Result fromJruby integration
  • 54.
    51All mentioned taskareas can be done easier in JRubyMore choices of tools, as would it be only with JavaKnowledge reuse in different project areasWork is getting more lightweigtFrom java IDEs to fast editorsPerfect interaction between different parts of the buildExpected result