Upgrade to Pro — share decks privately, control downloads, hide ads and more …

jRuby and TorqueBox

autonomous
February 07, 2013

jRuby and TorqueBox

Super quick introduction to both jRuby and TorqueBox from RubyFuZa 2013 presentation

autonomous

February 07, 2013
Tweet

More Decks by autonomous

Other Decks in Technology

Transcript

  1. “Java is a DSL for converting large XML files into

    Stack Traces” @ davetron5000 Thursday 07 February 13
  2. Bytecode JIT Java Integ Core Classes Parser jRuby C API

    Native JIT GC Threads JVM Charles Nutter - Euroko2012 Thursday 07 February 13
  3. “We could stop working on jRuby and it would continue

    to get faster.” Charles Nutter - Euroko2012 Thursday 07 February 13
  4. 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
  5. 0 15 30 45 60 Time in seconds 52.8 10.4

    Thursday 07 February 13
  6. 0 15 30 45 60 Time in seconds 52.8 10.4

    REE - 183.8 Thursday 07 February 13
  7. get '/' do 1_000.times do MultiJson.encode({ one: 'one', date: Date.today,

    two: 2 }) end end # ruby jsonator.rb -s Puma Thursday 07 February 13
  8. 0 7,5 15 22,5 30 Time in seconds 24.946 2.892

    8.489 Thursday 07 February 13
  9. • No GIL • Native threads • One process for

    all cores Thursday 07 February 13
  10. <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
  11. require 'stringio' require 'itext' require 'flying_saucer' io = StringIO.new content

    = File.read('example.html') renderer = org.xhtmlrenderer.pdf.ITextRenderer.new renderer.set_document_from_string(content) renderer.layout renderer.create_pdf(io.to_outputstream) File.write('pdf_result.pdf', io.string) Thursday 07 February 13
  12. A loads more....!!! • Choice of GC • Cross platform

    • GUI apps • Great tooling • Massive ecosystem Thursday 07 February 13
  13. TorqueBox App1 App2 App3 Background Tasks Scheduled Tasks Long running

    services Async. Messages Thursday 07 February 13
  14. TorqueBox App1 App2 App3 Background Tasks Scheduled Tasks Long running

    services Async. Messages JBoss AS Clustering Load balancing High availability Thursday 07 February 13
  15. class Charge < ActiveRecord::Base # id :integer not null, primary

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

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

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

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

    key # cents :integer default(0) # state :string(255) default("pending") end Thursday 07 February 13
  20. 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 ) end end fetch('/queue/charges').publish( @charge.id ) Thursday 07 February 13
  21. 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 ) end end fetch('/queue/charges').publish( @charge.id ) Thursday 07 February 13
  22. 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 ) end end fetch('/queue/charges').publish( @charge.id ) '/queue/charges' ChargeProcessor Thursday 07 February 13
  23. # app/processors/charge_processor.rb class ChargeProcessor < TorqueBox::Messaging::MessageProcessor def on_message( charge_id )

    # ... end def on_error( exception ) # optionally deal with exception # ... end end Thursday 07 February 13
  24. # app/processors/charge_processor.rb class 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
  25. # app/processors/charge_processor.rb class 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
  26. # app/processors/charge_processor.rb class 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
  27. # config/torquebox.rb TorqueBox.configure do queue '/queue/charges' do # processors/charge_processor.rb processor

    ChargeProcessor end topic '/topic/charge_status' do processor StatusProcessor processor TesterProcessor processor AlertAdminProcessor end end Thursday 07 February 13
  28. # app/stomplets/charge_status_stomplet class 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
  29. 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') end end Thursday 07 February 13