You are on page 1of 54

JRuby in Java Projects

Denis Lutz

12/06/11

Denis Lutz/ JRuby in Java Projects

Agenda

Motivation: Problem, Solution JRuby as language Java and Ruby Integration Ruby Community Show Case Demo Usage areas for JRuby

12/06/11

Denis Lutz/ JRuby in Java Projects

Motivation

12/06/11

Denis Lutz/ JRuby in Java Projects

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

Denis Lutz/ JRuby in Java Projects

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

Denis Lutz/ JRuby in Java Projects

Real world praxis

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

Denis Lutz/ JRuby in Java Projects

Entering JRuby

12/06/11

Denis Lutz/ JRuby in Java Projects

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

Denis Lutz/ JRuby in Java Projects

We can use JRuby in many project areas

Front End (MVC) Build management Testing, specifically integration testing Import/Export APIs Code generation

12/06/11

Denis Lutz/ JRuby in Java Projects

10

Facets of JRuby

12/06/11

Denis Lutz/ JRuby in Java Projects

11

Get started with JRuby in 3 mins

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

Denis Lutz/ JRuby in Java Projects

12

Most imressive language features

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

Denis Lutz/ JRuby in Java Projects

13

Simple object creation

12/06/11

Denis Lutz/ JRuby in Java Projects

14

Create your class and use it, lightweight and easy!

12/06/11

Denis Lutz/ JRuby in Java Projects

15

Everything is an object

12/06/11

Denis Lutz/ JRuby in Java Projects

16

Blocks, the most amazing and powerful feature ever

12/06/11

Denis Lutz/ JRuby in Java Projects

17

Lets call JRuby from Java

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

Denis Lutz/ JRuby in Java Projects

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

Denis Lutz/ JRuby in Java Projects

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

Denis Lutz/ JRuby in Java Projects

21

Endless amounts of gems

12/06/11

Denis Lutz/ JRuby in Java Projects

22

Find most popular gems in one place, rated by the community

12/06/11

Denis Lutz/ JRuby in Java Projects

23

Live Demo: Using a GEM

12/06/11

Denis Lutz/ JRuby in Java Projects

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

Easiest solution (a base for it) ?

12/06/11

Denis Lutz/ JRuby in Java Projects

25

Choose HTTParty as the gem for REST communication

Choose your gem HTTParty

12/06/11

Denis Lutz/ JRuby in Java Projects

26

Install the GEM

12/06/11

Denis Lutz/ JRuby in Java Projects

27

Write our nice little class

12/06/11

Denis Lutz/ JRuby in Java Projects

28

Use it immidiatelly on the console to play with it

12/06/11

Denis Lutz/ JRuby in Java Projects

29

Why was this great?

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

Denis Lutz/ JRuby in Java Projects

30

Integration with Java

12/06/11

Denis Lutz/ JRuby in Java Projects

31

Rails (Ruby) popularity

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

Denis Lutz/ JRuby in Java Projects

32

Deploy a Ruby on Rails Application in a java war

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

jruby -S gem install -y rails warbler $RAILS_APP_ROOT/jruby -S warble war

12/06/11

Denis Lutz/ JRuby in Java Projects

33

Java Build with Rake

12/06/11

Denis Lutz/ JRuby in Java Projects

34

Rake as build tool in Java projects

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

Denis Lutz/ JRuby in Java Projects

35

Problems with Ant and others

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

Denis Lutz/ JRuby in Java Projects

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

Denis Lutz/ JRuby in Java Projects

37

Rake is the solution, rubys build tool

Rake does all you want :

project setup methods operating system calls objects in your build file operations Ivy integration

12/06/11

Denis Lutz/ JRuby in Java Projects

38

Rake in 3 mins, nothing is easier!

12/06/11

Denis Lutz/ JRuby in Java Projects

39

Call classic and custom Ant tasks from rake if needed

12/06/11

Denis Lutz/ JRuby in Java Projects

40

Talk to your operating system as from the console

Operating system communication File management Folder management

12/06/11

Denis Lutz/ JRuby in Java Projects

41

Some rake possibilities = just full ruby power

12/06/11

Denis Lutz/ JRuby in Java Projects

42

In/Out API for your project

12/06/11

Denis Lutz/ JRuby in Java Projects

43

JRuby as your In/Out API of any project

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

JRuby as your In/Out API of any project

12/06/11

Denis Lutz/ JRuby in Java Projects

45

(Integration) Testing

12/06/11

Denis Lutz/ JRuby in Java Projects

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

WEBRAT GEM as JRuby Library

12/06/11

Denis Lutz/ JRuby in Java Projects

47

Webrat Example

12/06/11

Denis Lutz/ JRuby in Java Projects

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

Denis Lutz/ JRuby in Java Projects

49

Expected Result from Jruby integration

12/06/11

Denis Lutz/ JRuby in Java Projects

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

Denis Lutz/ JRuby in Java Projects

51

Achieve more with less work using ruby

My personal, subjective impression You will reach more, having to know less

12/06/11

Denis Lutz/ JRuby in Java Projects

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)

Editors Textmate (MacOs) http://www.e-texteditor.com/ (Windows)

IDEs http://www.jetbrains.com/ruby/ http://www.aptana.com/products/radrails (Eclipse Plugin) http://wiki.netbeans.org/Ruby (Netbeans Ruby Support)

12/06/11

Denis Lutz/ JRuby in Java Projects

53

Thanks a lot and have fun with JRuby!

12/06/11

Denis Lutz/ JRuby in Java Projects

54

You might also like