Download - Torquebox
![Page 1: Torquebox](https://reader036.vdocuments.pub/reader036/viewer/2022081403/55592fb4d8b42a543d8b489e/html5/thumbnails/1.jpg)
Torquebox 2
Przemysław Wróblewski
![Page 2: Torquebox](https://reader036.vdocuments.pub/reader036/viewer/2022081403/55592fb4d8b42a543d8b489e/html5/thumbnails/2.jpg)
Co to jest Torquebox ?
● Torquebox to zmodyfikowany JBoss AS 7, który integruje środowisko JEE ze środowiskiem Rubiego.
● Dostarcza interfejsy Ruby do usług dostarczanych przez server aplikacji
● pozwala uruchamiać Jave i Ruby w tym samym kontenerze aplikacji
● umożliwia szybkie i proste sklastrowanie aplikacji
![Page 3: Torquebox](https://reader036.vdocuments.pub/reader036/viewer/2022081403/55592fb4d8b42a543d8b489e/html5/thumbnails/3.jpg)
JRuby• połączenie Ruby i JVM• Java i Ruby mogą się integrować require 'java'java.lang.System.setProperty('key','value')java_import java.util.HashMaphashmap = HashMap.newhashmap['key'] = 'value'
![Page 4: Torquebox](https://reader036.vdocuments.pub/reader036/viewer/2022081403/55592fb4d8b42a543d8b489e/html5/thumbnails/4.jpg)
JRuby
![Page 5: Torquebox](https://reader036.vdocuments.pub/reader036/viewer/2022081403/55592fb4d8b42a543d8b489e/html5/thumbnails/5.jpg)
Torquebox - co jest w środku ?
![Page 6: Torquebox](https://reader036.vdocuments.pub/reader036/viewer/2022081403/55592fb4d8b42a543d8b489e/html5/thumbnails/6.jpg)
![Page 7: Torquebox](https://reader036.vdocuments.pub/reader036/viewer/2022081403/55592fb4d8b42a543d8b489e/html5/thumbnails/7.jpg)
Kolejki
![Page 8: Torquebox](https://reader036.vdocuments.pub/reader036/viewer/2022081403/55592fb4d8b42a543d8b489e/html5/thumbnails/8.jpg)
Workery - konfiguracjaconfig/torquebox.yml: queues: /queues/raw_offer: durable: false messaging: default_message_encoding: marshal /queues/raw_offer: RawOfferMessageProcessor: concurrency: 18
![Page 9: Torquebox](https://reader036.vdocuments.pub/reader036/viewer/2022081403/55592fb4d8b42a543d8b489e/html5/thumbnails/9.jpg)
Workeryclass RawOfferMessageProcessor < TorqueBox::Messaging::MessageProcessor include TorqueBox::Injectors include AirbrakeReportable def on_message(body) out_msg = OfferChecker.new.check body if out_msg queue = inject('/queues/offer') queue.publish(out_msg) end end def on_error(error) notify_airbrake(error, {parameters: {message: @message.decode}}) endend
![Page 10: Torquebox](https://reader036.vdocuments.pub/reader036/viewer/2022081403/55592fb4d8b42a543d8b489e/html5/thumbnails/10.jpg)
Backgroundable
class Something def foo sleep 5 endend something = Something.newsomething.foo
standardowa klasa + blokujące wywołanie
![Page 11: Torquebox](https://reader036.vdocuments.pub/reader036/viewer/2022081403/55592fb4d8b42a543d8b489e/html5/thumbnails/11.jpg)
Backgroundable
class Something include TorqueBox::Messaging::Backgroundable def foo sleep 5 endend something = Something.newsomething.background.foo
wywołanie nie blokujące
![Page 12: Torquebox](https://reader036.vdocuments.pub/reader036/viewer/2022081403/55592fb4d8b42a543d8b489e/html5/thumbnails/12.jpg)
Backgroundable
something = Something.newfuture = something.background.foo future.started?future.complete?future.error?future.result
sprawdz stan zadania nieblokującego
![Page 13: Torquebox](https://reader036.vdocuments.pub/reader036/viewer/2022081403/55592fb4d8b42a543d8b489e/html5/thumbnails/13.jpg)
Backgroundable
class Something def foo ... count+=1 future.status=count ... endend
sprawdz stan zadania nieblokującego
something = Something.newfuture = something.background.foofuture.status_changed?future.status #=>42
![Page 14: Torquebox](https://reader036.vdocuments.pub/reader036/viewer/2022081403/55592fb4d8b42a543d8b489e/html5/thumbnails/14.jpg)
Cronconfig/torquebox.yml: jobs: data_import: description: "Import danych z importu 2.0" job: DataImportJob cron: '0 10 18 * * ?' singleton: true
![Page 15: Torquebox](https://reader036.vdocuments.pub/reader036/viewer/2022081403/55592fb4d8b42a543d8b489e/html5/thumbnails/15.jpg)
Cronapp/jobs/data_import_job.rb: class DataImportJob def initialize dump_directory = File.join(Date.today.to_s(:number)) @importer = DataImporter.new(dump_directory) end def run ... endend
![Page 16: Torquebox](https://reader036.vdocuments.pub/reader036/viewer/2022081403/55592fb4d8b42a543d8b489e/html5/thumbnails/16.jpg)
Usługiapp/services/time_machine.rb: class TimeMachine def initialize(opts) @queue=Queue.new(opts['queue']) end def start Thread.new { run } end def stop @done=true end ...end
![Page 17: Torquebox](https://reader036.vdocuments.pub/reader036/viewer/2022081403/55592fb4d8b42a543d8b489e/html5/thumbnails/17.jpg)
Usługiconfig/torquebox.yml: services: TimeMachine: config: queue: /queue/time
![Page 18: Torquebox](https://reader036.vdocuments.pub/reader036/viewer/2022081403/55592fb4d8b42a543d8b489e/html5/thumbnails/18.jpg)
Resource Injectionapp/controllers/my_contoller.rb: class MyController < ApplicationController include TorqueBox::Injectors def stop # Service defined with a unique name in torquebox.yml the_torque_service = inject( 'service:TimeMachine' ) the_torque_service.stop end end
![Page 19: Torquebox](https://reader036.vdocuments.pub/reader036/viewer/2022081403/55592fb4d8b42a543d8b489e/html5/thumbnails/19.jpg)
Resource Injection
inject_cdi(...)Injects CDI resources by literal class name.
inject_queue(...)Injects a messaging queue by literal name.
inject_topic(...)Injects a messaging topic by literal name.
inject_naming(...)Injects JNDI resources by literal name.
inject_msc(...)Injects MSC services by literal name.
inject_service(...)Injects a TorqueBox application service component
metody
![Page 20: Torquebox](https://reader036.vdocuments.pub/reader036/viewer/2022081403/55592fb4d8b42a543d8b489e/html5/thumbnails/20.jpg)
Cache
![Page 21: Torquebox](https://reader036.vdocuments.pub/reader036/viewer/2022081403/55592fb4d8b42a543d8b489e/html5/thumbnails/21.jpg)
Cache
config/application.rb: module MyApp class Application < Rails::Application ... config.cache_store=:torque_box_store endend
RailsCaching
![Page 22: Torquebox](https://reader036.vdocuments.pub/reader036/viewer/2022081403/55592fb4d8b42a543d8b489e/html5/thumbnails/22.jpg)
Cache
include ActiveSupport::Cachemy_cache = TorqueBoxStore.new(:name=>'MyCache', :mode=>:replicated, :sync=>true)my_cache.put('klucz','wartość')my_cache.put_if_absent('klucz','inna wartość')my_cache.replace('klucz','jeszcze inna wartość')Torquebox.transaction do my_cache.remove( 'klucz' )endmy_cache.transaction do my_cache.put_if_absent('klucz','zupełnie inna wartość')end
bezpośredni dostep do własnej przestrzeni
![Page 23: Torquebox](https://reader036.vdocuments.pub/reader036/viewer/2022081403/55592fb4d8b42a543d8b489e/html5/thumbnails/23.jpg)
Cache
LocalDomyślny, wszystko jest trzymane w ramach pojedyńczego node'a, odpowiednik trzymania w pamięci ale pozwala na zapewnienie persystencji, daje transakcje
InvalidationDomyślny w trybie clustra, nie wysyla zmian do innych node'ów tylko komunikaty o przedawnieniu klucza. Dobrze sprawuje sie do cacheowania widoków i innych szybkich do odtworzenia wartości
ReplicatedKopiuje zawartość klucza do kazdego node'a, odczyty będą wykonywane już lokalnie
Distributedczyli sharding + replikacja, memcache, redis schowajcie sie :-P
tryby pracy
![Page 24: Torquebox](https://reader036.vdocuments.pub/reader036/viewer/2022081403/55592fb4d8b42a543d8b489e/html5/thumbnails/24.jpg)
Cache noSQL data mapperrequire 'dm-core'require 'datamapper/dm-infinispan-adapter'
class Beer include DataMapper::Resource property :id, Serial property :name, String property :desc, Text property :likes, Integer property :created_at, DateTimeend
DataMapper.setup(:default, :adapter=>'infinispan', :persist=>'db/infinispan')DataMapper::Model.raise_on_save_failure = trueDataMapper.finalize
Beer.create(:name=>'Catawba White Zombie Ale', :description=>'A Belgium white ale made with coriander, orange peel, and wheat. This beer is refreshing and tasty with a sweet and shimmering frothy head.', :created_at=>Time.now)beers = Beer.all(:desc.like => "%belgium%", :name.like => "%ale%")
![Page 25: Torquebox](https://reader036.vdocuments.pub/reader036/viewer/2022081403/55592fb4d8b42a543d8b489e/html5/thumbnails/25.jpg)
monitoring
![Page 26: Torquebox](https://reader036.vdocuments.pub/reader036/viewer/2022081403/55592fb4d8b42a543d8b489e/html5/thumbnails/26.jpg)
monitoring
![Page 27: Torquebox](https://reader036.vdocuments.pub/reader036/viewer/2022081403/55592fb4d8b42a543d8b489e/html5/thumbnails/27.jpg)
monitoring
![Page 28: Torquebox](https://reader036.vdocuments.pub/reader036/viewer/2022081403/55592fb4d8b42a543d8b489e/html5/thumbnails/28.jpg)
jboss2:8080/monitoring
![Page 29: Torquebox](https://reader036.vdocuments.pub/reader036/viewer/2022081403/55592fb4d8b42a543d8b489e/html5/thumbnails/29.jpg)
Torquebox 2http://torquebox.org/
TorqueBox – moc Javy, piękno Rubiego – Marek Goldmann
Wideo: http://video-it.pl/2011/07/torquebox-moc-javy-piekno-rubiego-marek-goldmann/Slajdy: http://www.slideshare.net/marekgoldmann/torquebox-moc-javy-pikno-rubiego
Demo: https://github.com/goldmann/confitura-2011-torquebox-demo