jruby and you

41
JRuby and You Hiro Asari Engine Yard Wednesday, October 26, 11

Upload: hiro-asari

Post on 01-Nov-2014

1.477 views

Category:

Technology


1 download

DESCRIPTION

The "missing" slides from Charlotte JUG + RUG joint meeting for October 26, 2011. Keynote presentation is available upon request.

TRANSCRIPT

Page 1: JRuby and You

JRuby and YouHiro AsariEngine Yard

Wednesday, October 26, 11

Page 2: JRuby and You

HistorySeptember 10, 2001

Source: Wikipedia

Wednesday, October 26, 11

Page 3: JRuby and You

Wednesday, October 26, 11

Page 4: JRuby and You

Project Status

• Current stable release: 1.6.5 (Oct 25, 2011)

• Rails 3.1-compatible

Wednesday, October 26, 11

Page 5: JRuby and You

What’s next in 1.7?

• JSR 292 (a.k.a. invokedynamic) support

• Improved Ruby 1.9 support

Wednesday, October 26, 11

Page 6: JRuby and You

What’s in it for Javaprogrammers?

• REPL

• Testing frameworks

• Fun

Wednesday, October 26, 11

Page 7: JRuby and You

What’s in it for Rubyists?

• Real threads

• No more GIL

• Mature garbage collector

• Easy deployment

Wednesday, October 26, 11

Page 8: JRuby and You

Try JRubyhttp://jruby.org/tryjruby

Wednesday, October 26, 11

Page 9: JRuby and You

Getting JRuby

wget http://bit.ly/jruby_bin_165 | \tar xzvf -

export PATH=$PATH:$PWD/jruby-1.6.5/bin

Wednesday, October 26, 11

Page 10: JRuby and You

jruby -S irbWednesday, October 26, 11

Page 11: JRuby and You

Java Integration

require 'java'

Wednesday, October 26, 11

Page 12: JRuby and You

Loading JAR

require 'example.jar'

Wednesday, October 26, 11

Page 13: JRuby and You

Importing Java Class

StringBuffer = java.lang.StringBuffersb = StringBuffer.new "foo"

Wednesday, October 26, 11

Page 14: JRuby and You

java_import

java_import java.lang.StringBufferjava_import 'java.lang.StringBuffer'

Wednesday, October 26, 11

Page 15: JRuby and You

java.lang.System.currentTimeMillisjava.lang.System.current_time_millis

Static methods

Wednesday, October 26, 11

Page 16: JRuby and You

Static Fields

java_import java.util.logging.Loggerjava_import java.util.loggin.Level

Logger.global.log Level::SEVERE, "…"

Wednesday, October 26, 11

Page 17: JRuby and You

Constructor

URL.new 'http://engineyard.com'URL.new 'http', 'engineyard.com', '/'

Wednesday, October 26, 11

Page 18: JRuby and You

#given# void setPrice(int):car.setPrice(2000)car.price = 2000

# int getPrice()car.price

Instance methods

Wednesday, October 26, 11

Page 19: JRuby and You

Explicit Coercion

measurement = 5measurement.to_java

Wednesday, October 26, 11

Page 20: JRuby and You

Overloaded methods

public class C { public static String method(String s) { return "String"; } public static String method(long l) { return "long"; }}

Wednesday, October 26, 11

Page 21: JRuby and You

Overloaded methods

obj = C.newobj.method("foo") # => "String"obj.method(5) # => "long"

Wednesday, October 26, 11

Page 22: JRuby and You

Overloaded methods

public class C { public String method(int i) { return "int"; } public String method(long l) { return "long"; }}

Wednesday, October 26, 11

Page 23: JRuby and You

Overloaded methods

obj = C.newobj.method(5) # => "long"obj.method(5.to_java(Java::int))obj.java_send :method, [Java::int], 5

Wednesday, October 26, 11

Page 24: JRuby and You

Java Interfaces

callable = java.util.concurrent.Executors.callable do puts "foo"end

callable.call

Wednesday, October 26, 11

Page 26: JRuby and You

GUI

Redcar

http://redcareditor.com/

Wednesday, October 26, 11

Page 27: JRuby and You

Grafting Rails into Spring MVC

Wednesday, October 26, 11

Page 28: JRuby and You

Pet Clinic

• Example database-backed web app for Spring framework

Wednesday, October 26, 11

Page 29: JRuby and You

App

Requests

/vets/ Java/Spring

JRuby

/rack/

Wednesday, October 26, 11

Page 30: JRuby and You

Adding Routediff --git a/src/main/webapp/WEB-INF/jsp/vets.jsp b/src/main/webapp/WEB-INF/jsp/vets.jspindex cff2154..0d99817 100644--- a/src/main/webapp/WEB-INF/jsp/vets.jsp+++ b/src/main/webapp/WEB-INF/jsp/vets.jsp@@ -23,7 +23,7 @@ <table class="table-buttons"> <tr> <td>- <a href="<spring:url value="/vets.xml" htmlEscape="true" />">View as XML</a>+ <a href="<spring:url value="/rack/vets.xml" htmlEscape="true" />">View as XML</a> </td> </tr> </table>

Wednesday, October 26, 11

Page 31: JRuby and You

Cucumber

• http://cukes.info

• Testing framework for describing software behavior in plain English

Wednesday, October 26, 11

Page 32: JRuby and You

CucumberFeature: Pets

Scenario: Edit Pet Given I am on the owners search page And I press "Find Owners" And I follow "George Franklin" And I follow "Edit Pet" When I fill in "Name" with "Leoni" And press "Update Pet" Then I should see "Leoni"

Wednesday, October 26, 11

Page 33: JRuby and You

Cucumber testStand by while Tomcat finishes booting...Using the default profile...............................................................F-.............

(::) failed steps (::)

expected: /xml/, got: "text/html;charset=utf-8" (using =~)Diff:@@ -1,2 +1,2 @@-/xml/+text/html;charset=utf-8 (RSpec::Expectations::ExpectationNotMetError)org/jruby/RubyProc.java:268:in `call'./features/step_definitions/xml_json_steps.rb:12:in `(root)':in `/^I should see an XML document$/'features/vets.feature:6:in `Then I should see an XML document'

Failing Scenarios:cucumber features/vets.feature:3 # Scenario: View vets as XML

13 scenarios (1 failed, 12 passed)75 steps (1 failed, 1 skipped, 73 passed)0m7.709srake aborted!Cucumber failed

Wednesday, October 26, 11

Page 34: JRuby and You

diff --git a/pom.xml b/pom.xmlindex 9e22f83..0810701 100644--- a/pom.xml+++ b/pom.xml@@ -211,6 +211,18 @@ <scope>provided</scope> </dependency> + <!-- JRuby and JRuby-Rack -->+ <dependency>+ <groupId>org.jruby</groupId>+ <artifactId>jruby-complete</artifactId>+ <version>1.6.0</version>+ </dependency>+ <dependency>+ <groupId>org.jruby.rack</groupId>+ <artifactId>jruby-rack</artifactId>+ <version>1.0.8</version>+ </dependency>+ <!-- Test dependencies --> <dependency> <groupId>org.junit</groupId>

Wednesday, October 26, 11

Page 35: JRuby and You

diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xmlindex 8d02684..60ed6cb 100644--- a/src/main/webapp/WEB-INF/web.xml+++ b/src/main/webapp/WEB-INF/web.xml@@ -87,6 +87,21 @@ <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>++ <listener>+ <listener-class>org.jruby.rack.RackServletContextListener</listener-class>+ </listener>++ <servlet>+ <servlet-name>rack</servlet-name>+ <servlet-class>org.jruby.rack.RackServlet</servlet-class>+ </servlet>++ <servlet-mapping>+ <servlet-name>rack</servlet-name>+ <url-pattern>/rack/*</url-pattern>+ </servlet-mapping>+ <!-- Defines the 'default' servlet (usually for service static resources).

Wednesday, October 26, 11

Page 36: JRuby and You

diff --git a/src/main/webapp/WEB-INF/lib/app.rb b/src/main/webapp/WEB-INF/lib/app.rbindex 6ab5b3c..4398fb4 100644--- a/src/main/webapp/WEB-INF/lib/app.rb+++ b/src/main/webapp/WEB-INF/lib/app.rb@@ -1,6 +1,33 @@ require 'builder' require 'erb'+require 'spring_helpers'++helpers do+ include Spring+end get '/rack/' do '<h1>Sinatra</h1>' end++get '/rack/vets.xml' do+ content_type 'application/vnd.petclinic+xml'+ builder do |xml|+ xml.instruct!+ xml.vets do+ clinic.vets.each do |vet|+ xml.vetList do+ xml.id vet.id+ xml.firstName vet.firstName+ xml.lastName vet.lastName+ vet.specialties.each do |spec|+ xml.specialties do+ xml.id spec.id+ xml.name spec.name+ end+ end+ end+ end+ end+ end+end

Wednesday, October 26, 11

Page 37: JRuby and You

App

/vets /owners/ /owners/1/pets

Requests

Java/Spring

/vets/ JRuby

Wednesday, October 26, 11

Page 38: JRuby and You

diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xmlindex 60ed6cb..f64b34d 100644--- a/src/main/webapp/WEB-INF/web.xml+++ b/src/main/webapp/WEB-INF/web.xml@@ -92,16 +92,6 @@ <listener-class>org.jruby.rack.RackServletContextListener</listener-class> </listener> - <servlet>- <servlet-name>rack</servlet-name>- <servlet-class>org.jruby.rack.RackServlet</servlet-class>- </servlet>-- <servlet-mapping>- <servlet-name>rack</servlet-name>- <url-pattern>/rack/*</url-pattern>- </servlet-mapping>- <!-- Defines the 'default' servlet (usually for service static resources).@@ -162,6 +152,16 @@ <url-pattern>/</url-pattern> </servlet-mapping> + <filter>+ <filter-name>RackFilter</filter-name>+ <filter-class>org.jruby.rack.RubyFirstRackFilter</filter-class>+ </filter>++ <filter-mapping>+ <filter-name>RackFilter</filter-name>+ <url-pattern>/*</url-pattern>+ </filter-mapping>+ <filter> <filter-name>httpMethodFilter</filter-name> <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>

Wednesday, October 26, 11

Page 39: JRuby and You

The Book

http://pragprog.com/book/jruby/using-jruby

25% off for one week only: http://bit.ly/vd4Mhj

Wednesday, October 26, 11

Page 40: JRuby and You

Engine Yard

500 free hourshttp://engineyard.com

Wednesday, October 26, 11