jruby in java projects

54
Denis Lutz/ JRuby in Java Projects Montag, 6. Juni 2022 1 JRuby in Java Projects Denis Lutz

Upload: jazzman1980

Post on 01-Sep-2014

3.969 views

Category:

Technology


1 download

DESCRIPTION

My goals have been: - focusing on several project areas, where you can use jruby successfully - share the experience that I made using ruby in the last years - proove that things can be done easier as they are done in typical java projects

TRANSCRIPT

Page 1: JRuby in Java Projects

Denis Lutz/ JRuby in Java Projects7. April 2023 1

JRuby in Java ProjectsDenis Lutz

Page 2: JRuby in Java Projects

Denis Lutz/ JRuby in Java Projects7. April 2023 2

• Motivation: Problem, Solution

• JRuby as language

• Java and Ruby Integration

• Ruby Community

• Show Case Demo

• Usage areas for JRuby

Agenda

Page 3: JRuby in Java Projects

Denis Lutz/ JRuby in Java Projects7. April 2023 3

Motivation

Page 4: JRuby in Java Projects

Denis Lutz/ JRuby in Java Projects7. April 2023 4

• 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

Motivation

Page 5: JRuby in Java Projects

Denis Lutz/ JRuby in Java Projects7. April 2023 5

• 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

Theory

Page 6: JRuby in Java Projects

Denis Lutz/ JRuby in Java Projects7. April 2023 6

• 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

Real world praxis

Page 7: JRuby in Java Projects

Denis Lutz/ JRuby in Java Projects7. April 2023 7

Entering JRuby

Page 8: JRuby in Java Projects

Denis Lutz/ JRuby in Java Projects7. April 2023 8

• 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

Goal

Page 9: JRuby in Java Projects

Denis Lutz/ JRuby in Java Projects7. April 2023 9

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

Page 10: JRuby in Java Projects

Denis Lutz/ JRuby in Java Projects7. April 2023 10

• Front End (MVC)

• Build management

• Testing, specifically integration testing

• Import/Export APIs

• Code generation

We can use JRuby in many project areas

Page 11: JRuby in Java Projects

Denis Lutz/ JRuby in Java Projects7. April 2023 11

Facets of JRuby

Page 12: JRuby in Java Projects

Denis Lutz/ JRuby in Java Projects7. April 2023 12

• 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

Get started with JRuby in 3 mins…

Page 13: JRuby in Java Projects

Denis Lutz/ JRuby in Java Projects7. April 2023 13

• Simple Syntax

• Everything is an object

• Blocks

• Self contained ( contains most useful libs out of the box)

• Open classes

• Principle of least surprise

Most imressive language features

Page 14: JRuby in Java Projects

Denis Lutz/ JRuby in Java Projects7. April 2023 14

Simple object creation

Page 15: JRuby in Java Projects

Denis Lutz/ JRuby in Java Projects7. April 2023 15

Create your class and use it, lightweight and easy!

Page 16: JRuby in Java Projects

Denis Lutz/ JRuby in Java Projects7. April 2023 16

Everything is an object

Page 17: JRuby in Java Projects

Denis Lutz/ JRuby in Java Projects7. April 2023 17

Blocks, the most amazing and powerful feature ever

Page 18: JRuby in Java Projects

Denis Lutz/ JRuby in Java Projects7. April 2023 18

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()); }}

Page 19: JRuby in Java Projects

Denis Lutz/ JRuby in Java Projects7. April 2023 19

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

Lets call Java from Jruby und use your legacy systems

Page 20: JRuby in Java Projects

Denis Lutz/ JRuby in Java Projects7. April 2023 20

Ruby’s Community

Page 21: JRuby in Java Projects

Denis Lutz/ JRuby in Java Projects7. April 2023 21

• 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

Community

Page 22: JRuby in Java Projects

Denis Lutz/ JRuby in Java Projects7. April 2023 22

Endless amounts of gems

Page 23: JRuby in Java Projects

Denis Lutz/ JRuby in Java Projects7. April 2023 23

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

Page 24: JRuby in Java Projects

Denis Lutz/ JRuby in Java Projects7. April 2023 24

Live Demo: Using a GEM

Page 25: JRuby in Java Projects

Denis Lutz/ JRuby in Java Projects7. April 2023 25

• 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

The task

Easiest solution (a base for it) ?

Page 26: JRuby in Java Projects

Denis Lutz/ JRuby in Java Projects7. April 2023 26

• Choose your gem

• HTTParty

Choose HTTParty as the gem for REST communication

Page 27: JRuby in Java Projects

Denis Lutz/ JRuby in Java Projects7. April 2023 27

Install the GEM

Page 28: JRuby in Java Projects

Denis Lutz/ JRuby in Java Projects7. April 2023 28

Write our nice little class

Page 29: JRuby in Java Projects

Denis Lutz/ JRuby in Java Projects7. April 2023 29

Use it immidiatelly on the console to play with it

Page 30: JRuby in Java Projects

Denis Lutz/ JRuby in Java Projects7. April 2023 30

• 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, doesn’t matter what you install

• its my experience after two years using ruby, yours will be the same

Why was this great?

Page 31: JRuby in Java Projects

Denis Lutz/ JRuby in Java Projects7. April 2023 31

Integration with Java

Page 32: JRuby in Java Projects

Denis Lutz/ JRuby in Java Projects7. April 2023 32

• Yes its right, Rails got over JSF (java standard) in the meanwhile

• Is this still a new and not known technology for you?

Rails (Ruby) popularity

Page 33: JRuby in Java Projects

Denis Lutz/ JRuby in Java Projects7. April 2023 33

• 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

Deploy a Ruby on Rails Application in a java war

jruby -S gem install -y rails warbler

$RAILS_APP_ROOT/jruby -S warble war

Page 34: JRuby in Java Projects

Denis Lutz/ JRuby in Java Projects7. April 2023 34

Java Build with Rake

Page 35: JRuby in Java Projects

Denis Lutz/ JRuby in Java Projects7. April 2023 35

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?

Page 36: JRuby in Java Projects

Denis Lutz/ JRuby in Java Projects7. April 2023 36

• 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

Problems with Ant and others…

Page 37: JRuby in Java Projects

Denis Lutz/ JRuby in Java Projects7. April 2023 37

• 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

What we want

Page 38: JRuby in Java Projects

Denis Lutz/ JRuby in Java Projects7. April 2023 38

Rake does all you want :

• project setup

• methods

• operating system calls

• objects in your build

• file operations

• Ivy integration

Rake is the solution, rubys build tool

Page 39: JRuby in Java Projects

Denis Lutz/ JRuby in Java Projects7. April 2023 39

Rake in 3 mins, nothing is easier!

Page 40: JRuby in Java Projects

Denis Lutz/ JRuby in Java Projects7. April 2023 40

Call classic and custom Ant tasks from rake if needed

Page 41: JRuby in Java Projects

Denis Lutz/ JRuby in Java Projects7. April 2023 41

• Operating system communication

• File management

• Folder management

Talk to your operating system as from the console

Page 42: JRuby in Java Projects

Denis Lutz/ JRuby in Java Projects7. April 2023 42

Some rake possibilities = just full ruby power

Page 43: JRuby in Java Projects

Denis Lutz/ JRuby in Java Projects7. April 2023 43

In/Out API for your project

Page 44: JRuby in Java Projects

Denis Lutz/ JRuby in Java Projects7. April 2023 44

• 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

JRuby as your In/Out API of any project

Page 45: JRuby in Java Projects

Denis Lutz/ JRuby in Java Projects7. April 2023 45

JRuby as your In/Out API of any project

Page 46: JRuby in Java Projects

Denis Lutz/ JRuby in Java Projects7. April 2023 46

(Integration) Testing

Page 47: JRuby in Java Projects

Denis Lutz/ JRuby in Java Projects7. April 2023 47

• 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

Integration Testing

Page 48: JRuby in Java Projects

Denis Lutz/ JRuby in Java Projects7. April 2023 48

Webrat Example

Page 49: JRuby in Java Projects

Denis Lutz/ JRuby in Java Projects7. April 2023 49

• 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

Webrat

Page 50: JRuby in Java Projects

Denis Lutz/ JRuby in Java Projects7. April 2023 50

Expected Result from

Jruby integration

Page 51: JRuby in Java Projects

Denis Lutz/ JRuby in Java Projects7. April 2023 51

• 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

Expected result

Page 52: JRuby in Java Projects

Denis Lutz/ JRuby in Java Projects7. April 2023 52

• My personal, subjective impression

• You will reach more, having to know less

Achieve more with less work using ruby

Page 53: JRuby in Java Projects

Denis Lutz/ JRuby in Java Projects7. April 2023 53

• 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)

• IDE’s

• http://www.jetbrains.com/ruby/

• http://www.aptana.com/products/radrails (Eclipse Plugin)

• http://wiki.netbeans.org/Ruby (Netbeans Ruby Support)

Further informations

Page 54: JRuby in Java Projects

Denis Lutz/ JRuby in Java Projects7. April 2023 54

Thanks a lot and have fun with JRuby!