TorqueBox

2,403 views

Published on

Presentation on TorqueBox at Strange Loop 2009 in St. Louis, Missouri

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,403
On SlideShare
0
From Embeds
0
Number of Embeds
154
Actions
Shares
0
Downloads
19
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

TorqueBox

  1. 1. TorqueBox Ruby on Rails (and Rack) with Enterprise Goodness Bob McWhirter John Williams Red Hat
  2. 2. Agenda: Part 1  Who are these guys up here, talking to you?  The Language Cusp  Ruby vs Java  Polyglotism  Picture of Bill  App servers for Java and Ruby  Basic of Rails on TorqueBox  Beyond Rails on TorqueBox  Rack on TorqueBox  Sinatra on TorqueBox
  3. 3. Agenda: Part 2  Why TorqueBox  Deploying with Bundles  Deploying using Capistrano
  4. 4. Who is Bob McWhirter?  Active in open-source  Doing Java for a dozen years  Doing Ruby for a handful of years  Research & Prototyping group at JBoss
  5. 5. Who is John Williams?  Early user of TorqueBox  Interested in anything open-source  Doing RoR for 4 years  Doing web development for 8 years  Wrote theme music to Star Wars
  6. 6. Who are you?
  7. 7. Java is facing competition from other languages
  8. 8. The Language Cusp
  9. 9. polyglot: a mixture or confusion of languages
  10. 10. Polyglotism “Polyglotism is the worst idea I ever heard” -Bill Burke, coworker
  11. 11. Polyglotism Ruby Java Scala Groovy Polyglotism may indeed be bad within a single developer’s head Ruby Java or even within a single team.
  12. 12. Polyglotism Underlying infrastructure, if Ruby polyglotic, can Other JBoss support a larger Java community and market.
  13. 13. Services vs APIs JBoss already has a full suite of enterprise-grade services. Web Container Message Bus SOAP JBoss AS 5.x
  14. 14. Services vs APIs Wrapped with standard Java APIs... Java Application Servlet API JMS API JAX-WS API Web Container Message Bus SOAP JBoss AS 5.x
  15. 15. Services vs APIs Why not wrap with Ruby APIs? Ruby Application Rails TorqueBox TorqueBox Web Container Message Bus SOAP JBoss AS 5.x
  16. 16. Ruby App Server Then, you end up with an enterprise-grade Ruby app server.
  17. 17. Ruby App Server in 4 Steps
  18. 18. Step 1 Ruby on Rails
  19. 19. Step 1: Rails  JRuby  The guys got regular Rails running well under mongrel using JRuby  There is also Warbler for creating deployable WAR files  Gl*ssfish can run Rails apps in- place
  20. 20. Step 1: Rails But that’s not good enough
  21. 21. Step 1: Rails on JBoss  JBoss  Run Rails apps in-place under JBoss  No WAR-creation required  Runs alongside other JEE apps  Runs alongside other Servlets within the same application
  22. 22. Step 1.5 Databases
  23. 23. Step 1.5: Databases  Since Java has the very nice JDBC drivers, let’s use them  But don’t want to teach Rubyists JDBC  Add a few ActiveRecord driver gems, and your Rails application accesses the DB through JDBC
  24. 24. Step 1.5: Databases  No changes to config/database.yml required  Rails is managing the connections itself
  25. 25. Step 1.75: Managed connections on Rails  If’n you want to use a managed datasource deployed outside of the application...  You can make changes to config/ database.yml to use a datasource  Datasource located via JNDI
  26. 26. Step 1.75: Managed connections on Rails  You can even deploy your datasource from within your Rails application:  config/mydb-ds.xml
  27. 27. Step 1.97: Deployment Deployment with TorqueBox is slightly different, but familiar to JBoss users.
  28. 28. Inversion of Deployment Traditional Rails  You pull HTTP functionality into your app  You run your app, which listens on a port
  29. 29. Inversion of Deployment: Traditional HTTP listener web-server gems mongrel, etc controllers views controllers views controllers views models models models models Rails Application
  30. 30. Inversion of Deployment Rails in an app server  Load your app into an app-server which already listens to HTTP  App server routes some requests to your app or other apps
  31. 31. Inversion of Deployment: App server HTTP listener controllers views controllers views controllers views controllers models views models controllers views models controllers views models models Rails Application models models models Rails Application App Server
  32. 32. Deployment  You don’t “start the app”  You “deploy” it into an App Server  TorqueBox comes with Rake tasks to help  rake torquebox:rails:deploy  rake torquebox:run
  33. 33. Deployment Descriptor
  34. 34. Simple Deployment application: RAILS_ENV: development RAILS_ROOT: /path/to/my/app web: context: /
  35. 35. Simple Deployment application: RAILS_ENV: development RAILS_ROOT: /path/to/my/app web: context: /
  36. 36. Simple Deployment application: RAILS_ENV: development RAILS_ROOT: /path/to/my/app web: context: /
  37. 37. Simple Deployment application: RAILS_ENV: development RAILS_ROOT: /path/to/my/app web: context: / host: www.myhost.com
  38. 38. Act like normal  Once deployed, continue to edit  Models  Views  Controllers  Without re-deploying your app
  39. 39. Go beyond Rails
  40. 40. Step 2: Scheduled Jobs Sometimes you’ve got a recurring task not associated with a web request A cron job
  41. 41. Step 2: Scheduled Jobs  Let’s use Quartz, it comes with JBoss config/jobs.yml github.commit_poller: description: Poll GitHub job: Github::CommitPoller cron: 12 */10 * * * ?
  42. 42. Step 2: Scheduled Jobs  We’re used to  app/controllers/**.rb  app/views/**.erb  app/models/**.rb  So let’s go with  app/jobs/**.rb
  43. 43. Step 2: Scheduled Jobs module GitHub class CommitPoller include TorqueBox::Jobs::Base def run() # do work here end end end
  44. 44. Step 2: Scheduled Jobs module GitHub class CommitPoller include TorqueBox::Jobs::Base def run() # do work here end end end
  45. 45. Step 2: Scheduled Jobs module GitHub class CommitPoller include TorqueBox::Jobs::Base def run() # do work here end end end
  46. 46. Step 2: Scheduled Jobs module GitHub class CommitPoller include TorqueBox::Jobs::Base def run() # do work here end end end
  47. 47. Step 2: Scheduled Jobs  Jobs will deploy with your app  Jobs will undeploy with your app  Jobs have complete access to your ActiveRecord models  Jobs have complete access to your lib/ classes  Jobs can be live edited like anything else
  48. 48. Well, that was easy
  49. 49. Step 3: Async Task Queues  Sometimes you want something non- recurring to happen  Perhaps outside of the context of a web request  Perhaps triggered by a web request, though
  50. 50. That sounds like a message queue. JBoss has one of those.
  51. 51. Step 3: Async Task Queues  Like you’d expect...  app/queues/**.rb  A class per queue  A method per task
  52. 52. Step 3: Async Task Queues class MyQueue include TorqueBox::Queue::Base def do_something(payload={}) # do work here end end
  53. 53. Step 3: Async Task Queues class MyQueue include TorqueBox::Queue::Base def do_something(payload={}) # do work here end end
  54. 54. Step 3: Async Task Queues class MyQueue include TorqueBox::Queue::Base def do_something(payload={}) # do work here end end
  55. 55. Step 3: Async Task Queues class MyQueue include TorqueBox::Queue::Base def do_something(payload={}) # do work here end end
  56. 56. Step 3: Enqueuing MyQueue.enqueue(:do_something,{ :quantity=>100, :cheese=>:gouda })
  57. 57. Step 3: Enqueuing MyQueue.enqueue(:do_something,{ :quantity=>100, :cheese=>:gouda })
  58. 58. Step 3: Enqueuing MyQueue.enqueue(:do_something,{ :quantity=>100, :cheese=>:gouda })
  59. 59. Step 3: Async Task Queues  A JMS queue is created for each queue class  The payload is anything that can be serialized into bytes  Including ActiveRecord models
  60. 60. Sometimes you’ve got to use SOAP
  61. 61. Step 4: SOAP  Sure, SOAP is obnoxious  SOAP from Ruby is obnoxious, and underpowered  Apache CXF is some good stuff  Sometimes you have to do SOAP, so at least you can do it from Ruby
  62. 62. Step 4: SOAP  Goal is not to generate WSDL from Ruby endpoints  Instead, only supports binding Ruby endpoints to existing WSDL  If you’re doing greenfield development, prefer REST. Or sockets. Or pigeons.
  63. 63. Step 4: SOAP  As you’d expect, again...  app/endpoints/**.rb  app/endpoints/**.wsdl
  64. 64. Step 4: SOAP module Amazon class Ec2Endpoint include TorqueBox::Endpoints::Base end end
  65. 65. Step 4: SOAP module Amazon class Ec2Endpoint include TorqueBox::Endpoints::Base endpoint_configuration do target_namespace 'http://ec2.amazonaws.com/doc/2008-12-01/' port_name 'AmazonEC2' security do inbound do verify_timestamp verify_signature end end end end end
  66. 66. Step 4: SOAP module Amazon class Ec2Endpoint include TorqueBox::Endpoints::Base endpoint_configuration do target_namespace 'http://ec2.amazonaws.com/doc/2008-12-01/' port_name 'AmazonEC2' security do inbound do verify_timestamp verify_signature end end end end end
  67. 67. Step 4: SOAP module Amazon class Ec2Endpoint include TorqueBox::Endpoints::Base endpoint_configuration do target_namespace 'http://ec2.amazonaws.com/doc/2008-12-01/' port_name 'AmazonEC2' security do inbound do verify_timestamp verify_signature end end end end end
  68. 68. Step 4: SOAP module Amazon class Ec2Endpoint def describe_instances response = create_response request.instancesSet.each do |instance_id| reservation_info = response.reservationSet.create reservation_info.ownerId = ... end return response end end end
  69. 69. Step 4: SOAP  TorqueBox provides...  full request/response XSD data- binding (like JAXB)  security, such as X.509 signature verification
  70. 70. Now you have a pretty nice Ruby app server. Not too shabby.
  71. 71. Wait, that was just Ruby on Rails...
  72. 72. Anything Rack TorqueBox uses Rack plumbing to handle RoR Now Soon will allow deploying of arbitrary Rack applications
  73. 73. config.ru app = lambda{|env| [ 200, {'Content-Type'=>'text/html'}, 'Hello World'] } run app
  74. 74. config.ru app = lambda{|env| [ 200, {'Content-Type'=>'text/html'}, 'Hello World'] } run app
  75. 75. config.ru app = lambda{|env| [ 200, {'Content-Type'=>'text/html'}, 'Hello World'] } run app
  76. 76. config.ru app = lambda{|env| [ 200, {'Content-Type'=>'text/html'}, 'Hello World'] } run app
  77. 77. config.ru app = lambda{|env| [ 200, {'Content-Type'=>'text/html'}, 'Hello World'] } run app
  78. 78. config.ru app = lambda{|env| [ 200, {'Content-Type'=>'text/html'}, 'Hello World'] } run app
  79. 79. *-rack.yml application: RACK_ROOT: /path/to/myapp RACK_ENV: production rackup: config.ru web: host: myapp.com
  80. 80. *-rack.yml application: RACK_ROOT: /path/to/myapp RACK_ENV: production rackup: config.ru web: host: myapp.com
  81. 81. *-rack.yml application: RACK_ROOT: /path/to/myapp RACK_ENV: production rackup: config.ru web: host: myapp.com
  82. 82. *-rack.yml application: RACK_ROOT: /path/to/myapp RACK_ENV: production rackup: config.ru web: host: myapp.com
  83. 83. *-rack.yml application: RACK_ROOT: /path/to/myapp RACK_ENV: production rackup: config.ru web: host: myapp.com
  84. 84. *-rack.yml application: RACK_ROOT: /path/to/myapp RACK_ENV: production rackup: config.ru web: host: myapp.com
  85. 85. Sinatra!
  86. 86. config.ru require 'app' use TorqueBox::Rack::Reloader, File.dirname(__FILE__) run Sinatra::Application
  87. 87. config.ru require 'app' use TorqueBox::Rack::Reloader, File.dirname(__FILE__) run Sinatra::Application
  88. 88. config.ru require 'app' use TorqueBox::Rack::Reloader, File.dirname(__FILE__) run Sinatra::Application
  89. 89. config.ru require 'app' use TorqueBox::Rack::Reloader, File.dirname(__FILE__) run Sinatra::Application
  90. 90. app.rb require 'rubygems' require 'sinatra' get '/' do "Hello world" end
  91. 91. app.rb require 'rubygems' require 'sinatra' get '/' do "Hello world" end
  92. 92. app.rb require 'rubygems' require 'sinatra' get '/' do "Hello world" end
  93. 93. app.rb require 'rubygems' require 'sinatra' get '/' do "Hello world" end
  94. 94. Questions?
  95. 95. Deploying Rails Applications to TorqueBox
  96. 96. Why TorqueBox?
  97. 97. Why TorqueBox? Rails Application Rails Application 20 Mongrels 20 Mongrels TorqueBox Rails Application Rails Application Capistrano Deployed Bundle Deployed
  98. 98. Methods to Deploy Application Bundle OR Capistrano
  99. 99. Application Bundle What is an Application Bundle?
  100. 100. Application Bundle Web Application Archive (WAR) of your Rails application. It will be created with a .rails file extension.
  101. 101. Application Bundle How do I create it?
  102. 102. Application Bundle App Bundle Rake tasks: rake torquebox:rails:bundle rake torquebox:rails:deploy:bundle
  103. 103. Application Bundle What to do next?
  104. 104. Application Bundle Drop it in your TorqueBox deploy directory. OR Deploy it locally using the Rake tasks using: rake torquebox:rails:deploy:bundle
  105. 105. Application Bundle Don’t forget...
  106. 106. Application Bundle config/web.yml AND config/rails-env.yml
  107. 107. config/web.yml Host AND Context
  108. 108. config/web.yml Example: host: torquebox.org context: /
  109. 109. config/rails-env.yml Rails Environment
  110. 110. config/rails-env.yml Example: RAILS_ENV: production
  111. 111. Capistrano Complete Example: http://github.com/torquebox/ballast-rails
  112. 112. Capistrano Supports: daemontools AND init.d
  113. 113. Capistrano Don’t Forget... require 'recipes/torquebox'
  114. 114. Questions?

×