crank up your apps with torquebox
DESCRIPTION
Presentation by Jim Crossley at Magic Ruby 2011TRANSCRIPT
![Page 1: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/1.jpg)
MagicRubyFeb 2011
Crank Up Your Apps With
Creative Commons BY-SA 3.0
Jim Crossley
1
![Page 2: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/2.jpg)
Jim who?
• “recovering java architect”
• java since ’96, ruby since ’06
• emacs and guitars since forever
• Red Hat Senior Engineer
• proud member of
2
![Page 3: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/3.jpg)
3
![Page 4: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/4.jpg)
Bob McWhirter
4
![Page 5: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/5.jpg)
Agenda• High-level BS and Development
Process
• Web, Messaging, Scheduling, Services, Clustering
5
![Page 6: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/6.jpg)
TorqueBoxthe power of JBoss with the expressiveness of Ruby
6
![Page 7: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/7.jpg)
TorqueBox: what?
• A “real” app server for Ruby
• Founded in 2008
• Bob’s “labor of love”
• 100% open-source, LGPL license
• Reflects a strong commitment to Ruby from Red Hat
7
![Page 8: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/8.jpg)
TorqueBox: why?
• “Native” support for Rack apps
• Built-in background processing
• Built-in scheduling
• Built-in clustering
• Easily scalable
• Optionally enterprisey
8
![Page 9: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/9.jpg)
JRubya good idea done well
9
![Page 10: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/10.jpg)
JRuby: why?
• Very fast runtime
• Real threads
• Java libraries
• Java tools
• Healthy community
10
![Page 11: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/11.jpg)
JRuby: why not?
• Slower start up
• Native gems
• FFI
• C extension support
• Some Ruby libs not thread-safe
11
![Page 12: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/12.jpg)
JBoss ASbut before we get to that...
12
![Page 13: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/13.jpg)
Enterprise Javamisconceptions
13
![Page 14: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/14.jpg)
Scary?Is it
14
![Page 15: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/15.jpg)
Acronymious?
AWT
EAR
EJBJAR
JAX-RPC
JAX-RS
JAX-WS
JAXB
JAXPJCK
JCP
JDBC JDK
JDO
JDOMJEE
JLSJME
JMM
JMS
JMXJNDI
JNIJNLP JPA
JPQL
JREJSE
JSF
JSP JSR
JSTLJTA
JVM
JTS JWS
NPE
NIO
SAF POJO
WAR SLSB
15
![Page 16: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/16.jpg)
Bloated?
16
![Page 17: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/17.jpg)
Bewildering?
17
![Page 18: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/18.jpg)
Enterprise Java is all thatand more! but...
18
![Page 19: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/19.jpg)
I promise...
• No XML
• No Java *
• No war files *
• Only Ruby, YAML and...
* Unless you really want to
19
![Page 20: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/20.jpg)
JBoss ASthe enterprisey good parts
20
![Page 21: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/21.jpg)
JBoss AS6
• Tomcat for web
• Infinispan for caching
• HornetQ for messaging
• Quartz for scheduling
• mod_cluster for clustering
21
![Page 22: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/22.jpg)
AS = Application Server
• Not just “web server + interpreter”
• More like initd than httpd
• Can host multiple, disparate apps simultaneously
• Provides basic services to all the apps it hosts
22
![Page 23: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/23.jpg)
Hot Deployment
• anything added to deploy/ will get deployed
• anything removed from deploy/ will get undeployed
• anything updated in deploy/ will get redeployed
• TorqueBox deployers make JBoss grok YAML and ruby archives
$JBOSS_HOME/server/default/deploy/
23
![Page 24: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/24.jpg)
Hot Deployment
• anything added to deploy/ will get deployed
• anything removed from deploy/ will get undeployed
• anything updated in deploy/ will get redeployed
• TorqueBox deployers make JBoss grok YAML and ruby archives
$JBOSS_HOME/server/default/deploy/
deployment descriptors
24
![Page 25: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/25.jpg)
Hot Deployment
• anything added to deploy/ will get deployed
• anything removed from deploy/ will get undeployed
• anything updated in deploy/ will get redeployed
• TorqueBox deployers make JBoss grok YAML and ruby archives
$JBOSS_HOME/server/default/deploy/
zip files (archives)
25
![Page 26: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/26.jpg)
Development Processhow do they work?
26
![Page 27: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/27.jpg)
easy install
$ wget http://torquebox.org/torquebox-dev.zip$ unzip torquebox-dev.zip
$ export TORQUEBOX_HOME=$PWD/torquebox-1*$ export JBOSS_HOME=$TORQUEBOX_HOME/jboss$ export JRUBY_HOME=$TORQUEBOX_HOME/jruby
$ export PATH=$JRUBY_HOME/bin:$PATH
27
![Page 28: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/28.jpg)
easy install
$ wget http://torquebox.org/torquebox-dev.zip$ unzip torquebox-dev.zip
$ export TORQUEBOX_HOME=$PWD/torquebox-1*$ export JBOSS_HOME=$TORQUEBOX_HOME/jboss$ export JRUBY_HOME=$TORQUEBOX_HOME/jruby
$ export PATH=$JRUBY_HOME/bin:$PATH
28
![Page 29: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/29.jpg)
easy install
$ wget http://torquebox.org/torquebox-dev.zip$ unzip torquebox-dev.zip
$ export TORQUEBOX_HOME=$PWD/torquebox-1*$ export JBOSS_HOME=$TORQUEBOX_HOME/jboss$ export JRUBY_HOME=$TORQUEBOX_HOME/jruby
$ export PATH=$JRUBY_HOME/bin:$PATH
29
![Page 30: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/30.jpg)
easy install
$ wget http://torquebox.org/torquebox-dev.zip$ unzip torquebox-dev.zip
$ export TORQUEBOX_HOME=$PWD/torquebox-1*$ export JBOSS_HOME=$TORQUEBOX_HOME/jboss$ export JRUBY_HOME=$TORQUEBOX_HOME/jruby
$ export PATH=$JRUBY_HOME/bin:$PATH
Make sure the jruby found in your path is in $JRUBY_HOME/bin.
30
![Page 31: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/31.jpg)
easy install
$ jruby -S gem install bundler$ jruby -S gem install rails$ jruby -S gem install sinatra
31
![Page 32: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/32.jpg)
Rakefile
rake tasks
require "org.torquebox.rake-support"
32
![Page 33: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/33.jpg)
rake tasks
rake torquebox:run Run TorqueBox server
rake torquebox:deploy[context_path] Deploy the app in the current directory
rake torquebox:undeploy Undeploy the app in the current directory
33
![Page 34: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/34.jpg)
rake tasks
Start torquebox:run in its own shell and leave it running. Instead of script/server or shotgun or thin or whatever else, use torquebox:deploy.
34
![Page 35: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/35.jpg)
deployment descriptors
torquebox:deploy creates a deployment descriptor in the JBoss deploy/ directory
35
![Page 36: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/36.jpg)
deploy/myapp-rails,rack.yml
deployment descriptors
application: root: /path/to/myapp env: developmentweb: context: myapp host: www.yourhost.com static: public rackup: config.ruenvironment: MAIL_HOST: mail.yourhost.com REPLY_TO: [email protected]
36
![Page 37: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/37.jpg)
deploy/myapp-rails,rack.yml
deployment descriptors
application: root: /path/to/myapp env: developmentweb: context: myapp host: www.yourhost.com static: public rackup: config.ruenvironment: MAIL_HOST: mail.yourhost.com REPLY_TO: [email protected]
The fully-qualified path to the app.
This will be the value of either
RAILS_ROOT or RACK_ROOT
37
![Page 38: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/38.jpg)
deploy/myapp-rails,rack.yml
deployment descriptors
application: root: /path/to/myapp env: developmentweb: context: myapp host: www.yourhost.com static: public rackup: config.ruenvironment: MAIL_HOST: mail.yourhost.com REPLY_TO: [email protected]
The runtime mode of the app. This will be either RAILS_ENV
or RACK_ENV
38
![Page 39: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/39.jpg)
deploy/myapp-rails,rack.yml
deployment descriptors
application: root: /path/to/myapp env: developmentweb: context: myapp host: www.yourhost.com static: public rackup: config.ruenvironment: MAIL_HOST: mail.yourhost.com REPLY_TO: [email protected]
The app’s context path (or “sub URI”):
http://localhost:8080/myappCan be set via rake:
rake torquebox:deploy[myapp]The default is root:
http://localhost:8080/
39
![Page 40: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/40.jpg)
deploy/myapp-rails,rack.yml
deployment descriptors
application: root: /path/to/myapp env: developmentweb: context: myapp host: www.yourhost.com static: public rackup: config.ruenvironment: MAIL_HOST: mail.yourhost.com REPLY_TO: [email protected]
A list of virtual hostnames to which
to bind the app.
40
![Page 41: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/41.jpg)
deploy/myapp-rails,rack.yml
deployment descriptors
application: root: /path/to/myapp env: developmentweb: context: myapp host: www.yourhost.com static: public rackup: config.ruenvironment: MAIL_HOST: mail.yourhost.com REPLY_TO: [email protected]
The location of the app’s static
content, either absolute or relative to the app’s root.
41
![Page 42: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/42.jpg)
deploy/myapp-rails,rack.yml
deployment descriptors
application: root: /path/to/myapp env: developmentweb: context: myapp host: www.yourhost.com static: public rackup: config.ruenvironment: MAIL_HOST: mail.yourhost.com REPLY_TO: [email protected]
The name of the rackup script used to
boot your app
42
![Page 43: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/43.jpg)
deploy/myapp-rails,rack.yml
deployment descriptors
application: root: /path/to/myapp env: developmentweb: context: myapp host: www.yourhost.com static: public rackup: config.ruenvironment: MAIL_HOST: mail.yourhost.com REPLY_TO: [email protected]
Any environment variables required
by the app.
43
![Page 44: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/44.jpg)
deployment descriptors
• config/torquebox.yml
• internal descriptors have the same structure as the external ones in deploy/
•may be used to provide your own reasonable defaults
44
![Page 45: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/45.jpg)
Webmake rack, not war
45
![Page 46: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/46.jpg)
jruby-rack
• All rack-based frameworks supported: rails, sinatra, etc
• No packaging required: apps deploy from where they sit on disk
• No redeploy necessary to see changes when using rack reloading or rails development mode
46
![Page 47: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/47.jpg)
Gemfile
database connectivity
gem "activerecord-jdbc-adapter"
gem "jdbc-postgres"# gem "jdbc-sqlite3"# gem "jdbc-mysql"
47
![Page 48: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/48.jpg)
rails template
• Adds TorqueBox rake tasks
• Adds the JDBC sqlite3 gems
• Adds TorqueBox session_store
• Adds Backgroundable module
48
![Page 49: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/49.jpg)
my first rails app
$ jruby -S rails new myapp -m \ $TORQUEBOX_HOME/share/rails/template.rb
$ cd myapp$ jruby -S bundle install
$ jruby -S rake torquebox:deploy
49
![Page 50: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/50.jpg)
Messagingasynchronicity
50
![Page 51: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/51.jpg)
Resque
ActiveMessaging
cron
Taskr
rufus-scheduler
ConveyorWorkerQueue
Ap4r
CronEdit
lots of options
RailsCronOpen4
Spawn
Sparrow
Kestral
RabbitMQ
ActiveMQ
Amazon SQS
beanstalkd
starling
workling
async_observer
daemons
BackgroundDRb
BackgroundJob
Background-Fu
JobFu
DelayedJob
rake
51
![Page 52: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/52.jpg)
TorqueBox::Messaging
• JMS (Java Message Service) is an API for messaging
•HornetQ is the JBoss JMS implementation
52
![Page 53: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/53.jpg)
TorqueBox::Messaging
•Task•Backgroundable•MessageProcessor
•Queue•Topic
53
![Page 54: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/54.jpg)
TorqueBox::Messaging
• No extra tables in your database
• No external system to manage
• Little to no config required at all
• No redeploy necessary in dev mode
• Efficient loading of rails environment
• Automatic load balancing and retries
• Works on Windows, if you care
54
![Page 55: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/55.jpg)
app/tasks/email_task.rb
Tasks
class EmailTask < TorqueBox::Messaging::Task def welcome(payload) person = payload[:person] person ||= Person.find_by_id(payload[:id]) if person # send the email person.welcomed = true person.save! end endend
55
![Page 56: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/56.jpg)
app/tasks/email_task.rb
Tasks
class EmailTask < TorqueBox::Messaging::Task def welcome(payload) person = payload[:person] person ||= Person.find_by_id(payload[:id]) if person # send the email person.welcomed = true person.save! end endend
56
![Page 57: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/57.jpg)
app/tasks/email_task.rb
Tasks
class EmailTask < TorqueBox::Messaging::Task def welcome(payload) person = payload[:person] person ||= Person.find_by_id(payload[:id]) if person # send the email person.welcomed = true person.save! end endend
57
![Page 58: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/58.jpg)
app/tasks/email_task.rb
Tasks
class EmailTask < TorqueBox::Messaging::Task def welcome(payload) person = payload[:person] person ||= Person.find_by_id(payload[:id]) if person # send the email person.welcomed = true person.save! end endend
58
![Page 59: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/59.jpg)
app/tasks/email_task.rb
Tasks
class EmailTask < TorqueBox::Messaging::Task def welcome(payload) person = payload[:person] person ||= Person.find_by_id(payload[:id]) if person # send the email person.welcomed = true person.save! end endend
59
![Page 60: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/60.jpg)
app/controllers/people_controller.rb
Tasks
class PeopleController < ApplicationController def create @person = Person.new(params[:person]) respond_to do |format| if @person.save EmailTask.async(:welcome, :id => person.id) # respond appropriately end end endend
60
![Page 61: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/61.jpg)
app/controllers/people_controller.rb
Tasks
class PeopleController < ApplicationController def create @person = Person.new(params[:person]) respond_to do |format| if @person.save EmailTask.async(:welcome, :id => person.id) # respond appropriately end end endend
61
![Page 62: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/62.jpg)
Tasks
Call them from your controllers, models, and observers, or even other tasks. Even in non-Rails apps!
62
![Page 63: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/63.jpg)
Backgroundable
Inspired by DelayedJob’s handle_asynchronously, it’s trivial to create implicit background Tasks.
63
![Page 64: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/64.jpg)
lib/something.rb
Backgroundable
include TorqueBox::Messaging
class Something include Backgroundable always_background :foo def foo;; end def bar;; [email protected]@something.background.bar
64
![Page 65: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/65.jpg)
lib/something.rb
Backgroundable
include TorqueBox::Messaging
class Something include Backgroundable always_background :foo def foo;; end def bar;; [email protected]@something.background.bar
65
![Page 66: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/66.jpg)
lib/something.rb
Backgroundable
include TorqueBox::Messaging
class Something include Backgroundable always_background :foo def foo;; end def bar;; [email protected]@something.background.bar
66
![Page 67: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/67.jpg)
lib/something.rb
Backgroundable
include TorqueBox::Messaging
class Something include Backgroundable always_background :foo def foo;; end def bar;; [email protected]@something.background.bar
67
![Page 68: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/68.jpg)
app/models/slow_poke.rb
Backgroundable
class SlowPoke < ActiveRecord::Base def takes_forever;; end def might_take_awhile;; endend
SlowPoke.always_background :takes_forever
@[email protected]_take_awhile
68
![Page 69: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/69.jpg)
Queues
Tasks are built on top of Queues. Of course, you may build your own “MOM” apps by defining your own Queues, Topics, and their message Processors yourself.
69
![Page 70: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/70.jpg)
config/queues.yml
Queues
/queues/questions:
/queues/answers: durable: false
70
![Page 71: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/71.jpg)
Processors
You can create a processor class to receive messages from a Topic or Queue
71
![Page 72: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/72.jpg)
config/messaging.yml
Processors
/topics/print: PrintHandler/queues/popular: - PopularHandler - AdultObserver: filter: "age >= 18" concurrency: 5/queues/students: PrintHandler: config: color: true
72
![Page 73: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/73.jpg)
config/messaging.yml
Processors
/topics/print: PrintHandler/queues/popular: - PopularHandler - AdultObserver: filter: "age >= 18" concurrency: 5/queues/students: PrintHandler: config: color: true
73
![Page 74: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/74.jpg)
config/messaging.yml
Processors
/topics/print: PrintHandler/queues/popular: - PopularHandler - AdultObserver: filter: "age >= 18" concurrency: 5/queues/students: PrintHandler: config: color: true
74
![Page 75: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/75.jpg)
config/messaging.yml
Processors
/topics/print: PrintHandler/queues/popular: - PopularHandler - AdultObserver: filter: "age >= 18" concurrency: 5/queues/students: PrintHandler: config: color: true
75
![Page 76: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/76.jpg)
app/models/print_handler.rb
Processors
include TorqueBox::Messaging
class PrintHandler < MessageProcessor def on_message(body) puts "Processing #body of #message" end def configure(opts) @color = opts['color'] endend
76
![Page 77: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/77.jpg)
Queues (again)
But how do you send a message?
77
![Page 78: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/78.jpg)
example
Queues
include TorqueBoxreq = Messaging::Queue.new '/queues/questions'res = Messaging::Queue.new '/queues/answers' Thread.new do req.publish "What time is it?" puts res.receive( :timeout => 1000 )end puts req.receiveres.publish Time.now
78
![Page 79: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/79.jpg)
example
Queues
include TorqueBoxreq = Messaging::Queue.new '/queues/questions'res = Messaging::Queue.new '/queues/answers' Thread.new do req.publish "What time is it?" puts res.receive( :timeout => 1000 )end puts req.receiveres.publish Time.now
79
![Page 80: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/80.jpg)
example
Queues
include TorqueBoxreq = Messaging::Queue.new '/queues/questions'res = Messaging::Queue.new '/queues/answers' Thread.new do req.publish "What time is it?" puts res.receive( :timeout => 1000 )end puts req.receiveres.publish Time.now
80
![Page 81: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/81.jpg)
example
Queues
include TorqueBoxreq = Messaging::Queue.new '/queues/questions'res = Messaging::Queue.new '/queues/answers' Thread.new do req.publish "What time is it?" puts res.receive( :timeout => 1000 )end puts req.receiveres.publish Time.now
81
![Page 82: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/82.jpg)
“on the fly”
Queues
include TorqueBox
queue = Messaging::Queue.new '/queues/foo'queue.create ... queue.destroy
82
![Page 83: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/83.jpg)
Topics
• behavior is different, but interface is the same.
• all subscribers of a topic see each message, but only one subscriber will see any message from a queue
• use topics.yml to define topics
• use TorqueBox::Messaging::Topic
83
![Page 84: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/84.jpg)
Schedulingget regular later
84
![Page 85: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/85.jpg)
app/jobs/newsletter_sender.rb
Jobs
class NewsletterSender def run() subscriptions = Subscription.find(:all) subscriptions.each do |e| send_newsletter( e ) end end end
85
![Page 86: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/86.jpg)
config/jobs.yml
Jobs
monthly_newsletter: description: first of month job: NewsletterSender cron: ‘0 0 0 1 * ?’
sandbox: job: Sandbox cron: ‘*/5 * * * * ?’
86
![Page 87: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/87.jpg)
Seconds Minutes Hours DOM Month DOW Year
0-59 0-59 0-23 1-31? L W
1-12JAN-DEC
1-7SUN-SAT? L #
1970-2099empty
0 */30 10-13 ? * FRI#3
“Fire every half hour from 10am until 1pm on the third Friday of each month”
Jobs
87
![Page 88: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/88.jpg)
Jobs
• More portable. What is the first day of the week on BSD again? What’s cron on Windows?
• Self contained within the app. No external systems to manage and keep in sync.
• Full rails environment loaded and available.
88
![Page 89: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/89.jpg)
Servicesrun along, lil’ daemon
89
![Page 90: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/90.jpg)
Services
Long-running, non-web “daemons” that share the runtime environment and deployment lifecycle of your app.
90
![Page 91: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/91.jpg)
Services
• Represented as a class with optional initialize(Hash), start() and stop() methods, which should each return quickly.
• Typically will start a long-running loop in a thread and respond to external events.
• Configured via services.yml
91
![Page 92: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/92.jpg)
config/services.yml
Services
IrcBot: server: freenode.net channel: #torquebox publish: /topics/irc
MyMudServer:
SomeOtherService:
92
![Page 93: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/93.jpg)
app/models,etc/my_service.rb
Services
class MyService def initialize opts= name = opts[:publish] @queue = Messaging::Queue.new(name) end def start Thread.new run end def stop @done = true endend
93
![Page 94: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/94.jpg)
app/models,etc/my_service.rb
Services
class MyService def initialize opts= name = opts[:publish] @queue = Messaging::Queue.new(name) end def start Thread.new run end def stop @done = true endend
94
![Page 95: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/95.jpg)
app/models,etc/my_service.rb
Services
class MyService def initialize opts= name = opts[:publish] @queue = Messaging::Queue.new(name) end def start Thread.new run end def stop @done = true endend
95
![Page 96: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/96.jpg)
app/models,etc/my_service.rb
Services
class MyService def initialize opts= name = opts[:publish] @queue = Messaging::Queue.new(name) end def start Thread.new run end def stop @done = true endend
96
![Page 97: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/97.jpg)
app/models,etc/my_service.rb
Services
class MyService def run until @done @queue.publish(Time.now) sleep(1) end endend
97
![Page 98: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/98.jpg)
app/models,etc/my_service.rb
Services
class MyService def run until @done @queue.publish(Time.now) sleep(1) end endend
98
![Page 99: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/99.jpg)
app/services/evented_service.rb
Services
class EventedService def initialize opts= @opts = opts end def start Thread.new EventMachine.run ... end def stop EventMachine.stop endend
99
![Page 100: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/100.jpg)
Clusteringless failure faster
100
![Page 101: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/101.jpg)
out-of-the-box• JBoss provides
• session replication
• load-balanced messaging
• mod_cluster provides
• session affinity
• intelligent load-balanced web
• failover
101
![Page 102: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/102.jpg)
JBOSS_CONFTorqueBox ships with two JBoss server configurations: default (not clustered) and all (clustered). To enable clustering...
$ export JBOSS_CONF=all
102
![Page 103: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/103.jpg)
deploy and run
$ export JBOSS_CONF=all$ jruby -S rake torquebox:deploy$ jruby -S rake torquebox:run
103
![Page 104: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/104.jpg)
mod_cluster
A reverse proxy implemented as an Apache module with JBoss awareness. Constantly gathers load statistics and deployment availability for intelligent request distribution.
104
![Page 105: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/105.jpg)
mod_cluster
105
![Page 106: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/106.jpg)
mod_cluster
• Dynamic configuration
• Server-side load factor calculation
• Fine-grained web app lifecycle
• AJP (Apache JServ Protocol) is optional. HTTP[S] is also supported.
106
![Page 107: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/107.jpg)
Futurecoming soon
107
![Page 108: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/108.jpg)
Future• Performance benchmarking and
optimization
• Infinispan for ActiveSupport::Cache and NoSQL persistence
• JBoss SSO, HASingleton, logging
• CDI injection of Java components into Ruby
• More JavaEE bridged to Ruby where it makes sense, e.g. BPM, Drools, Transactions, etc.
108
![Page 109: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/109.jpg)
Resources
• http://torquebox.org
• #torquebox on freenode
• https://github.com/torquebox
• http://twitter.com/torquebox
• http://projectodd.org
109
![Page 110: Crank Up Your Apps With TorqueBox](https://reader034.vdocuments.net/reader034/viewer/2022042518/54bd19a24a7959ff308b46ab/html5/thumbnails/110.jpg)
Thanks!questions?
110