Upload
jazzman1980
View
3.968
Download
1
Embed Size (px)
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
Citation preview
Denis Lutz/ JRuby in Java Projects7. April 2023 1
JRuby in Java ProjectsDenis Lutz
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
Denis Lutz/ JRuby in Java Projects7. April 2023 3
Motivation
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
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
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
Denis Lutz/ JRuby in Java Projects7. April 2023 7
Entering JRuby
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
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
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
Denis Lutz/ JRuby in Java Projects7. April 2023 11
Facets of JRuby
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…
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
Denis Lutz/ JRuby in Java Projects7. April 2023 14
Simple object creation
Denis Lutz/ JRuby in Java Projects7. April 2023 15
Create your class and use it, lightweight and easy!
Denis Lutz/ JRuby in Java Projects7. April 2023 16
Everything is an object
Denis Lutz/ JRuby in Java Projects7. April 2023 17
Blocks, the most amazing and powerful feature ever
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()); }}
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
Denis Lutz/ JRuby in Java Projects7. April 2023 20
Ruby’s Community
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
Denis Lutz/ JRuby in Java Projects7. April 2023 22
Endless amounts of gems
Denis Lutz/ JRuby in Java Projects7. April 2023 23
Find most popular gems in one place, rated by the community
Denis Lutz/ JRuby in Java Projects7. April 2023 24
Live Demo: Using a GEM
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) ?
Denis Lutz/ JRuby in Java Projects7. April 2023 26
• Choose your gem
• HTTParty
Choose HTTParty as the gem for REST communication
Denis Lutz/ JRuby in Java Projects7. April 2023 27
Install the GEM
Denis Lutz/ JRuby in Java Projects7. April 2023 28
Write our nice little class
Denis Lutz/ JRuby in Java Projects7. April 2023 29
Use it immidiatelly on the console to play with it
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?
Denis Lutz/ JRuby in Java Projects7. April 2023 31
Integration with Java
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
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
Denis Lutz/ JRuby in Java Projects7. April 2023 34
Java Build with Rake
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?
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…
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
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
Denis Lutz/ JRuby in Java Projects7. April 2023 39
Rake in 3 mins, nothing is easier!
Denis Lutz/ JRuby in Java Projects7. April 2023 40
Call classic and custom Ant tasks from rake if needed
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
Denis Lutz/ JRuby in Java Projects7. April 2023 42
Some rake possibilities = just full ruby power
Denis Lutz/ JRuby in Java Projects7. April 2023 43
In/Out API for your project
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
Denis Lutz/ JRuby in Java Projects7. April 2023 45
JRuby as your In/Out API of any project
Denis Lutz/ JRuby in Java Projects7. April 2023 46
(Integration) Testing
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
Denis Lutz/ JRuby in Java Projects7. April 2023 48
Webrat Example
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
Denis Lutz/ JRuby in Java Projects7. April 2023 50
Expected Result from
Jruby integration
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
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
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
Denis Lutz/ JRuby in Java Projects7. April 2023 54
Thanks a lot and have fun with JRuby!