+ All Categories
Home > Technology > Bitter Java, Sweeten with JRuby

Bitter Java, Sweeten with JRuby

Date post: 15-May-2015
Category:
Upload: brian-sam-bodden
View: 6,997 times
Download: 0 times
Share this document with a friend
Description:
Ways to make Java applications better and less complex with JRuby
Popular Tags:
64
Bitter Java? Sweeten with JRuby Brian Sam-Bodden
Transcript
Page 1: Bitter Java, Sweeten with JRuby

Bitter Java? Sweeten with JRuby

Brian Sam-Bodden

Page 2: Bitter Java, Sweeten with JRuby

Java Today

Page 3: Bitter Java, Sweeten with JRuby

Java is a 10+ year old programming language

Java is Dead

Long live Java!

Java is the new COBOL

Page 4: Bitter Java, Sweeten with JRuby

Java is not going anywhere

Because Java is everywhere!

But Java is not perfect

and it is adjusting to keep up

Page 5: Bitter Java, Sweeten with JRuby

Java has its jewels

JDBC, JMS, Servlets

and its blemishes...

EJB < 3, JSF

Page 6: Bitter Java, Sweeten with JRuby

But we have a secret weapon...

presenting the amazing, highly tuned, multi-platform computing engine that dominates the

present and permeates the future

On this corner...

The JVM

Page 7: Bitter Java, Sweeten with JRuby

The are more than 200+ languages implementations for the Java VM

The Java VM is like that neighborhood with low taxes and good schools... everyone wants in

Regardless of where we go with Java “the language”, Java “the platform” is here to stay

Page 8: Bitter Java, Sweeten with JRuby

Groovy, JRuby and all the new kids on the block are just new additions to our beloved Swiss army knife

Page 9: Bitter Java, Sweeten with JRuby

What’s Missing?

Page 10: Bitter Java, Sweeten with JRuby

Java is solid on the server-side

Java powers the enterprise

Web development with Java has not been all that successful in the presentation tier

Dynamic languages and frameworks based on dynamic languages have proved to be better suited for this

Page 11: Bitter Java, Sweeten with JRuby

Why Ruby?

Page 12: Bitter Java, Sweeten with JRuby

Why not?

Page 13: Bitter Java, Sweeten with JRuby

Rubyis...

✓Object-Oriented

✓Reflective ✓Dynamic

✓ Interpreted

✓General Purpose

✓ Multi-paradigm

✓ Garbage-collected

✓Elegant

Page 14: Bitter Java, Sweeten with JRuby

Created by Yukihiro “Matz” Matsumoto

Developed in 1993, released in 1995

Blended parts of his favorite languages:

Perl, Smalltalk, Sather, Eiffel, CLU, Ada and Lisp

More popular than Python in Japan

Page 15: Bitter Java, Sweeten with JRuby

Ruby Ideals:

Programming should be fun!

Principle of Least Surprise

“I believe people want to express themselves when they program. They don’t want to fight with the

language. Programming languages must feel natural to programmers.”

Matz

Page 16: Bitter Java, Sweeten with JRuby

JRuby

Page 17: Bitter Java, Sweeten with JRuby

A non-Java language for the Java platform

A Ruby interpreter written in Java

Started life in 2001

Current version is 1.0 (1.1b1)

Open Source

Page 18: Bitter Java, Sweeten with JRuby

Oh, come on! Grow up!

Page 19: Bitter Java, Sweeten with JRuby

Ruby libraries from Java (like ActiveRecord)

Java libraries from Ruby (Swing, SWT)

➡Using Bean Scripting Framework (JDK <= 1.5)

➡Using JSR 223 Scripting API (JDK > 1.5)

Page 20: Bitter Java, Sweeten with JRuby

RubyGems comes pre-installed

Mongrel supports JRuby

Rails just works

No native extensions (unless by native you mean Java ;-)

Page 21: Bitter Java, Sweeten with JRuby

JRuby does not produce bytecode artifacts for the Ruby code (yet!)

This is key difference with Groovy*

Corporate backing :-) Sun and Thoughtworks have folks working on JRuby full-time

Page 22: Bitter Java, Sweeten with JRuby

All you need to run JRuby is the Java JDK (=>1.4)

Page 23: Bitter Java, Sweeten with JRuby

Familiar?

pure Ruby with Java players

Page 24: Bitter Java, Sweeten with JRuby

http://www.tc.umn.edu/%7Eenebo/webstart/jirb-console/console.jnlp

Let’s try some interactive JRuby

Fire up the JRuby console using Java Web Start

Java version of IRB (Interactive RuBy) tool

Page 25: Bitter Java, Sweeten with JRuby

IRB is the best to get acquainted with Ruby

Page 26: Bitter Java, Sweeten with JRuby

A Simple Comparison

Page 27: Bitter Java, Sweeten with JRuby

Let’s do some file I/O...

First we need a text file

Page 28: Bitter Java, Sweeten with JRuby

package com.integrallis;

import java.io.BufferedReader;import java.io.FileReader;import java.io.IOException;

public class MyFileReader {

public static void main(String[] args) { try { BufferedReader in = new BufferedReader(new FileReader("read_me.txt")); String str; while ((str = in.readLine()) != null) { System.out.println(str); } in.close(); } catch (IOException e) { e.printStackTrace(); } }}

Let’s open the text file and print out its contents

...in Java

Page 29: Bitter Java, Sweeten with JRuby

Let’s do it the Ruby way!

Page 30: Bitter Java, Sweeten with JRuby

But wait! This just in...

... Ruby encapsulates common behaviors

➡ A one-liner version, yet is readable!

Page 31: Bitter Java, Sweeten with JRuby

Why?

Page 32: Bitter Java, Sweeten with JRuby

For Web applications...

Best of both worlds:

➡Dynamic-Framework on the Front End (Rails)

➡Solid Back End battle tested technologies (Spring/EJB3)

Page 33: Bitter Java, Sweeten with JRuby

Got API-itis?

Ruby’s ability to create concise DSLs

paired with Java’s richness of APIs and frameworks

Java APIs are robust, fully-featured, flexible...

...which makes them complex

A JRuby DSL can help us in the 80-20 scenarios

Page 34: Bitter Java, Sweeten with JRuby

Got API-itis? JRuby+SWT

Page 35: Bitter Java, Sweeten with JRuby

There are JRuby DSLs all ready to use...

Today!

Raven - Wrapper for Rake, RubyGems and Maven

AntBuilder - Wrapper for Ant

JRake - Wrapper for Rake

ActiveRecord-JDBC - OR-Wrapping

JtestR - Test Java code with the best Ruby testing tools

Page 36: Bitter Java, Sweeten with JRuby

JRuby on Rails

Page 37: Bitter Java, Sweeten with JRuby

Java & JEE

the most prevalent language

the most complete enterprise platform

a rich ecosystem of web frameworks

a vibrant open source community

Page 38: Bitter Java, Sweeten with JRuby

plagued by mediocre web frameworks

not well suited for rapid prototyping

in need of a dynamic language web framework

one level of abstraction too low for the web

Java & JEE

Page 39: Bitter Java, Sweeten with JRuby

Who could help Java on the Web Tier?

Page 40: Bitter Java, Sweeten with JRuby

Ruby on Rails

a revolutionary web framework

complete MVC solution

fast prototyping

killer AJAX support

Page 41: Bitter Java, Sweeten with JRuby

a DSL for the web

leading the pack on the web 2.0 front

born from a product, not a committee

a vibrant open source community

Ruby on Rails

Page 42: Bitter Java, Sweeten with JRuby

JRuby on Rails

the agility of rails - the scalability of Java

JRuby likely to surpass Ruby in speed

sneak Ruby and RoR into the enterprise!

Page 43: Bitter Java, Sweeten with JRuby

JRuby on Rails

Page 44: Bitter Java, Sweeten with JRuby

JRuby on Rails

Page 45: Bitter Java, Sweeten with JRuby

JRuby on Rails

JEE servers are good at scaling up

no more multi-process management

use JRuby “multi-VM” support

leverage the maturity of the JVM and JEE servers

Page 46: Bitter Java, Sweeten with JRuby

JRuby on Rails

deploy your Rails app as a WAR

leverage JDBC from ActiveRecord

Page 47: Bitter Java, Sweeten with JRuby

JRuby & Rapid Spring Prototyping

Page 48: Bitter Java, Sweeten with JRuby

JRuby+Spring RAD

a simple example by the folks at jobster

use JRuby for rapid prototyping

Spring controller that delegates to a JRuby controller

JRuby controllers live alongside your JSPs

Page 49: Bitter Java, Sweeten with JRuby

JRuby+Spring RAD

Simple JRuby controller

Page 50: Bitter Java, Sweeten with JRuby

JRuby+Spring RAD

JSP view provides input to the Ruby controller

Page 51: Bitter Java, Sweeten with JRuby

JRuby+Spring RAD

JSP view provides input to the Ruby controller

Page 52: Bitter Java, Sweeten with JRuby

JRuby+Spring RAD

this experimental library supports:

✓access to Spring beans from JRuby code

✓ access to Hibernate queries

Page 53: Bitter Java, Sweeten with JRuby

JRuby on Springy Rails

Page 54: Bitter Java, Sweeten with JRuby

JRoR on Spring

✓use rails-integration plugin

✓ expose WebApplicationContext in a RoR controller

Chris Nelson’s work

✓include Spring - Spring plugin for JRuby

Page 55: Bitter Java, Sweeten with JRuby

JRoR on Spring

✓install goldspike rails-integration plugin on you JRoR app

✓ rake war:standalone:create to package a WAR with:

➡Your RoR app

➡JRuby and Rails libraries

Page 56: Bitter Java, Sweeten with JRuby

JRoR on Spring

rake war:standalone:create in action:

Page 57: Bitter Java, Sweeten with JRuby

JRoR on Spring

generated WARs work on many servers:

✓Tomcat

✓Jetty

✓Glassfish

✓JBoss

Page 58: Bitter Java, Sweeten with JRuby

JRoR on Spring✓ add Chris’ Spring module:

Page 59: Bitter Java, Sweeten with JRuby

JRoR on Spring

✓Add Spring application

✓Expand the generated WAR

➡Application Code

➡ Libraries (Jars)

➡Config Files

Page 60: Bitter Java, Sweeten with JRuby

JRoR on Spring

✓ access your Spring beans from a RoR controller

Page 61: Bitter Java, Sweeten with JRuby

JRoR on Spring

✓add a new face to your Spring applications

✓ leverage thousands of open source projects

✓leverage the good parts of JEE like JDBC and JMS

Page 62: Bitter Java, Sweeten with JRuby

ResourcesJRuby

www.jruby.orgrubyforge.org/projects/jruby-extras

JRuby Rails Integrationwww.headius.com/jrubywiki/index.php/Rails_Integration

headius.blogspot.com/2006/11/advanced-rails-deployment-with-jruby.html

RAD Spring Prototyping with JRubythebogles.com/blog/projects/ruby-on-spring/

www.botspiritcompany.com/src/springruby/example_ruby_spring.html

Chris Nelson’s Workmysterycoder.blogspot.com/2007/06/spring-jruby.html

mysterycoder.blogspot.com/2007/06/include-spring.html

Page 64: Bitter Java, Sweeten with JRuby

Recommended