jruby and torquebox

83
Christopher Spring + = Thursday 07 February 13

Upload: christopher-spring

Post on 17-Dec-2014

1.974 views

Category:

Technology


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: jRuby and TorqueBox

Christopher Spring

+ =

Thursday 07 February 13

Page 3: jRuby and TorqueBox

Thursday 07 February 13

Page 4: jRuby and TorqueBox

Thursday 07 February 13

Page 5: jRuby and TorqueBox

“Java is a DSL for converting large XML files into Stack Traces”

@ davetron5000

Thursday 07 February 13

Page 6: jRuby and TorqueBox

... and that’s all I have to say about that

Thursday 07 February 13

Page 7: jRuby and TorqueBox

Thursday 07 February 13

Page 8: jRuby and TorqueBox

Structure

Thursday 07 February 13

Page 9: jRuby and TorqueBox

BytecodeJIT

JavaInteg

CoreClassesParser

jRuby

C APINative

JITGCThreads

JVM

Charles Nutter - Euroko2012Thursday 07 February 13

Page 10: jRuby and TorqueBox

BytecodeJIT

JavaInteg

CoreClassesParser

jRuby

Charles Nutter - Euroko2012Thursday 07 February 13

Page 11: jRuby and TorqueBox

“We could stop working on jRuby and it would continue to get faster.”

Charles Nutter - Euroko2012Thursday 07 February 13

Page 12: jRuby and TorqueBox

Performance

Thursday 07 February 13

Page 13: jRuby and TorqueBox

require 'benchmark'

def fib(n) return 1 if n < 2 fib(n - 2) + fib(n - 1)end

arr = []puts Benchmark.measure{ 40.times{ |i| arr << fib(i) }}puts arr.inspect# [1, 1, 2, 3, 5, 8, 13, 21, ... ]

Thursday 07 February 13

Page 14: jRuby and TorqueBox

0

15

30

45

60

Time in seconds

52.8

Thursday 07 February 13

Page 15: jRuby and TorqueBox

0

15

30

45

60

Time in seconds

52.8

10.4

Thursday 07 February 13

Page 16: jRuby and TorqueBox

0

15

30

45

60

Time in seconds

52.8

10.4

REE - 183.8

Thursday 07 February 13

Page 17: jRuby and TorqueBox

get '/' do 1_000.times do MultiJson.encode({ one: 'one', date: Date.today, two: 2 }) endend

# ruby jsonator.rb -s Puma

Thursday 07 February 13

Page 18: jRuby and TorqueBox

ab -n 1000 -c 32 http://localhost:4567/

Thursday 07 February 13

Page 19: jRuby and TorqueBox

Thursday 07 February 13

Page 20: jRuby and TorqueBox

0

7,5

15

22,5

30

Time in seconds

24.946

Thursday 07 February 13

Page 21: jRuby and TorqueBox

Thursday 07 February 13

Page 22: jRuby and TorqueBox

0

7,5

15

22,5

30

Time in seconds

24.946

8.489

Thursday 07 February 13

Page 23: jRuby and TorqueBox

Thursday 07 February 13

Page 24: jRuby and TorqueBox

0

7,5

15

22,5

30

Time in seconds

24.946

2.892

8.489

Thursday 07 February 13

Page 25: jRuby and TorqueBox

Runtime optimizations

Thursday 07 February 13

Page 26: jRuby and TorqueBox

method_one method_two method_three

Thursday 07 February 13

Page 27: jRuby and TorqueBox

method_one method_two method_three

Thursday 07 February 13

Page 28: jRuby and TorqueBox

Threading

Thursday 07 February 13

Page 29: jRuby and TorqueBox

vs.

Thursday 07 February 13

Page 30: jRuby and TorqueBox

• No GIL

• Native threads

• One process for all cores

Thursday 07 February 13

Page 31: jRuby and TorqueBox

Concurrency threshold

100 process system

100 thread system

vs.

Thursday 07 February 13

Page 32: jRuby and TorqueBox

Java Libraries

Thursday 07 February 13

Page 33: jRuby and TorqueBox

<html><head> <style type="text/css"> body{ font-family: sans-serif; text-align: center; background-color: #efefef; } h1{ color: blue; font-weight: bold;} div{ border: 4px dashed red; padding: 2em;margin: 2em; } </style> </head> <body> <h1>PDFs with Flying Saucer and jRuby</h1> <div> <p>I'm a lumberjack, and I'm ok...</p> <img src="flower.jpeg"></img> </div> </body></html>

Thursday 07 February 13

Page 34: jRuby and TorqueBox

require 'stringio'require 'itext'require 'flying_saucer'

io = StringIO.newcontent = File.read('example.html')

renderer = org.xhtmlrenderer.pdf.ITextRenderer.newrenderer.set_document_from_string(content)renderer.layoutrenderer.create_pdf(io.to_outputstream)

File.write('pdf_result.pdf', io.string)

Thursday 07 February 13

Page 35: jRuby and TorqueBox

html

Thursday 07 February 13

Page 36: jRuby and TorqueBox

pdf

Thursday 07 February 13

Page 37: jRuby and TorqueBox

A loads more....!!!

• Choice of GC

• Cross platform

• GUI apps

• Great tooling

• Massive ecosystem

Thursday 07 February 13

Page 38: jRuby and TorqueBox

Thursday 07 February 13

Page 39: jRuby and TorqueBox

Let’s take a step back...

Thursday 07 February 13

Page 40: jRuby and TorqueBox

Your App

Thursday 07 February 13

Page 41: jRuby and TorqueBox

Your App

BackgroundJobs

Thursday 07 February 13

Page 42: jRuby and TorqueBox

Your App

BackgroundJobs

Thursday 07 February 13

Page 43: jRuby and TorqueBox

Your App

BackgroundJobs

ScheduledJobs

Thursday 07 February 13

Page 44: jRuby and TorqueBox

Your App

BackgroundJobs

ScheduledJobs

Cron

Thursday 07 February 13

Page 45: jRuby and TorqueBox

Thursday 07 February 13

Page 46: jRuby and TorqueBox

TorqueBox

Thursday 07 February 13

Page 47: jRuby and TorqueBox

TorqueBox

Thursday 07 February 13

Page 48: jRuby and TorqueBox

TorqueBox

App1

App2

App3

Thursday 07 February 13

Page 49: jRuby and TorqueBox

TorqueBox

App1

App2

App3

Thursday 07 February 13

Page 50: jRuby and TorqueBox

TorqueBox

App1App2App3

BackgroundTasks

ScheduledTasks

Long runningservices

Async. Messages

Thursday 07 February 13

Page 51: jRuby and TorqueBox

TorqueBox

App1App2App3

BackgroundTasks

ScheduledTasks

Long runningservices

Async. Messages

JBoss AS

Clustering Load balancing High availability

Thursday 07 February 13

Page 52: jRuby and TorqueBox

Some of the toys...

• HornetQ

• Infinispan

• Quartz Scheduler

Thursday 07 February 13

Page 53: jRuby and TorqueBox

Thursday 07 February 13

Page 54: jRuby and TorqueBox

It’s still justbusiness as usual

Thursday 07 February 13

Page 55: jRuby and TorqueBox

AsynchronousStuffs

Thursday 07 February 13

Page 56: jRuby and TorqueBox

class Charge < ActiveRecord::Base # id :integer not null, primary key # cents :integer default(0) # state :string(255) default("pending")end

Thursday 07 February 13

Page 57: jRuby and TorqueBox

class Charge < ActiveRecord::Base # id :integer not null, primary key # cents :integer default(0) # state :string(255) default("pending")end

Thursday 07 February 13

Page 58: jRuby and TorqueBox

class Charge < ActiveRecord::Base # id :integer not null, primary key # cents :integer default(0) # state :string(255) default("pending")end

Thursday 07 February 13

Page 59: jRuby and TorqueBox

class Charge < ActiveRecord::Base # id :integer not null, primary key # cents :integer default(0) # state :string(255) default("pending")end

Thursday 07 February 13

Page 60: jRuby and TorqueBox

class Charge < ActiveRecord::Base # id :integer not null, primary key # cents :integer default(0) # state :string(255) default("pending")end

Thursday 07 February 13

Page 61: jRuby and TorqueBox

class ChargesController < ActionController::Base include TorqueBox::Injectors def create @charge = Charge.create( params[:charge] ) msg = 'Slaves turtles doing thy bidding' redirect_to( charges_url, notice: msg ) endend

fetch('/queue/charges').publish( @charge.id )

Thursday 07 February 13

Page 62: jRuby and TorqueBox

class ChargesController < ActionController::Base include TorqueBox::Injectors def create @charge = Charge.create( params[:charge] ) msg = 'Slaves turtles doing thy bidding' redirect_to( charges_url, notice: msg ) endend

fetch('/queue/charges').publish( @charge.id )

Thursday 07 February 13

Page 63: jRuby and TorqueBox

class ChargesController < ActionController::Base include TorqueBox::Injectors def create @charge = Charge.create( params[:charge] ) msg = 'Slaves turtles doing thy bidding' redirect_to( charges_url, notice: msg ) endend

fetch('/queue/charges').publish( @charge.id )

'/queue/charges' ChargeProcessor

Thursday 07 February 13

Page 64: jRuby and TorqueBox

fetch('/queue/charges').publish( @charge.id )

# config/torquebox.rbTorqueBox.configure do

queue '/queue/charges' do processor ChargeProcessor end

end

Thursday 07 February 13

Page 65: jRuby and TorqueBox

# app/processors/charge_processor.rbclass ChargeProcessor < TorqueBox::Messaging::MessageProcessor def on_message( charge_id ) # ... end def on_error( exception ) # optionally deal with exception # ... endend

Thursday 07 February 13

Page 66: jRuby and TorqueBox

# app/processors/charge_processor.rbclass ChargeProcessor < TorqueBox::Messaging::MessageProcessor def on_message( charge_id ) @charge = Charge.find( charge_id ) result = PaymentGateway.process( @charge ) generate_receipt if result.success? broadcast_status end

end

Thursday 07 February 13

Page 67: jRuby and TorqueBox

# app/processors/charge_processor.rbclass ChargeProcessor < TorqueBox::Messaging::MessageProcessor def on_message( charge_id ) @charge = Charge.find( charge_id ) result = PaymentGateway.process( @charge ) generate_receipt if result.success? broadcast_status end

end

def generate_receipt fetch('queue/receipts').publish( @charge.id ) end

Thursday 07 February 13

Page 68: jRuby and TorqueBox

# app/processors/charge_processor.rbclass ChargeProcessor < TorqueBox::Messaging::MessageProcessor def on_message( charge_id ) @charge = Charge.find( charge_id ) result = PaymentGateway.process( @charge ) generate_receipt if result.success? broadcast_status end

end

def generate_receipt fetch('queue/receipts').publish( @charge.id ) end

def broadcast_status fetch('topics/charge_status').publish( @charge.id )end

Thursday 07 February 13

Page 69: jRuby and TorqueBox

Queue

Topic

Processor

Processor

Processor

Processor

Thursday 07 February 13

Page 70: jRuby and TorqueBox

# config/torquebox.rbTorqueBox.configure do queue '/queue/charges' do # processors/charge_processor.rb processor ChargeProcessor end topic '/topic/charge_status' do processor StatusProcessor processor TesterProcessor processor AlertAdminProcessor endend

Thursday 07 February 13

Page 71: jRuby and TorqueBox

And WebSockets?

Thursday 07 February 13

Page 72: jRuby and TorqueBox

Hells yes!!

Thursday 07 February 13

Page 73: jRuby and TorqueBox

Topic/Queue

Processor

Stomplet

Client

Thursday 07 February 13

Page 74: jRuby and TorqueBox

Topic/Queue

Processor

Stomplet

Client

‘/stomp/statuses’

Thursday 07 February 13

Page 75: jRuby and TorqueBox

Topic/Queue

Processor

Stomplet

Client

‘/stomp/statuses’‘/topics/charge_status’

Thursday 07 February 13

Page 76: jRuby and TorqueBox

# config/torquebox.rbstomplet ChargeStatusStomplet do route '/stomp/status'end

Thursday 07 February 13

Page 77: jRuby and TorqueBox

# app/stomplets/charge_status_stompletclass ChargeStatusStomplet < TorqueBox::Stomp::JmsStomplet def on_subscribe( client ) topic = destination_for('/topics/charge_status', :topic) subscribe_to( client , topic ) end # configure(config) # destroy() # on_subscribe(subscriber) # on_unsubscribe(subscriber) # on_message(message)end

Thursday 07 February 13

Page 78: jRuby and TorqueBox

javascript_include_tag '/stilts-stomp'

Thursday 07 February 13

Page 79: jRuby and TorqueBox

var stompUrl = '#{stomp_url}'

$( function() { if (stompUrl) { var client = Stomp.client(stompUrl);

client.connect( username, password, function() { client.subscribe( '/stomp/status', function(msg) { alert(msg.body); }); }); }});

module ApplicationHelper include TorqueBox::Injectors def stomp_url inject('stomp-endpoint') endend

Thursday 07 February 13

Page 80: jRuby and TorqueBox

Thursday 07 February 13

Page 81: jRuby and TorqueBox

Thursday 07 February 13

Page 82: jRuby and TorqueBox

Thanks for Listening!

Thursday 07 February 13

Page 83: jRuby and TorqueBox

Questions?

Thursday 07 February 13