/samples/hello_world_for_rails2/vendor/plugins/ap4r/lib/ap4r_client.rb

https://github.com/futonaka/ap4r · Ruby · 132 lines · 14 code · 8 blank · 110 comment · 0 complexity · 2b5e16486757aa534d234e53ab0fb8bf MD5 · raw file

  1. # Author:: Kiwamu Kato
  2. # Copyright:: Copyright (c) 2007 Future Architect Inc.
  3. # Licence:: MIT Licence
  4. require 'forwardable'
  5. require 'ap4r/async_helper'
  6. module Ap4r #:nodoc:
  7. # This +Client+ is the Rails plugin for asynchronous processing.
  8. # Asynchronous logics are called via various protocols, such as XML-RPC,
  9. # SOAP, HTTP POST, and more. Now default protocol is HTTP POST.
  10. #
  11. # Examples: The part of calling next asynchronous logics in a controller in the HelloWorld Sample.
  12. #
  13. # req = WorldRequest.new([:world_id => 1, :message => "World"})
  14. # ap4r.async_to({:controller => 'async_world', :action => 'execute'},
  15. # {:world_id => 1, :message => "World"},
  16. # {:dispatch_mode => :HTTP}) # skippable
  17. #
  18. # render :action => 'response'
  19. #
  20. # Complement: Above +ap4r+ method is defiend init.rb in +%RAILS_ROOT%/vendor/plugin/ap4r/+.
  21. #
  22. class Client
  23. extend Forwardable
  24. include ::Ap4r::AsyncHelper::Base
  25. def initialize controller
  26. @controller = controller
  27. end
  28. def_delegators :@controller, :logger, :url_for, :controller_path
  29. # Queue a message for next asynchronous logic. Some options are supported.
  30. #
  31. # Use options to specify target url, etc.
  32. # Accurate meanings are defined by a individual converter class.
  33. # * :controller (name of next logic)
  34. # * :action (name of next logic)
  35. #
  36. # Use rm_options to pass parameter in queue-put.
  37. # Listings below are AP4R extended options.
  38. # See the reliable-msg docuememt for more details.
  39. # * :target_url (URL of target, prevail over :controller)
  40. # * :target_action (action of target, prevail over :action)
  41. # * :target_method (HTTP method, e.g. "GET", "POST", etc.)
  42. # * :dispatch_mode (protocol in dispatching)
  43. # * :queue_name (prevail over :controller and :action)
  44. #
  45. # Object of argumemts (async_params, options and rm_options) will not be modified.
  46. # Implementors (of this class and converters) should not modify them.
  47. #
  48. # Examples: the most simple
  49. #
  50. # ap4r.async_to({:controller => 'next_controller', :action => 'next_action'},
  51. # {:world_id => 1, :message => "World"})
  52. #
  53. #
  54. # Examples: taking block
  55. #
  56. # ap4r.async_to({:controller => 'next_controller', :action => 'next_action'}) do
  57. # body :world_id, 1
  58. # body :message, "World"
  59. # end
  60. #
  61. #
  62. # Examples: transmitting ActiveRecord object
  63. #
  64. # ap4r.async_to({:controller => 'next_controller', :action => 'next_action'}) do
  65. # body :world, World.find(1)
  66. # body :message, "World"
  67. # end
  68. #
  69. #
  70. # Examples: transmitting with xml format over http (now support text, json and yaml format).
  71. #
  72. # ap4r.async_to({:controller => 'next_controller', :action => 'next_action'}) do
  73. # body :world, World.find(1)
  74. # body :message, "World"
  75. # format :xml
  76. # end
  77. #
  78. #
  79. # Examples: direct assignment for formatted message body
  80. #
  81. # ap4r.async_to({:controller => 'next_controller', :action => 'next_action'}) do
  82. # world = World.find(1).to_xml :except => ...
  83. # body_as_xml world
  84. # end
  85. #
  86. #
  87. # Examples: setting message header
  88. #
  89. # ap4r.async_to({:controller => 'next_controller', :action => 'next_action'}) do
  90. # body :world_id, 1
  91. # body :message, "World"
  92. #
  93. # header :priority, 1
  94. # http_header "Content-type", ...
  95. # end
  96. #
  97. alias :async_to :async_dispatch
  98. # Provides at-least-once QoS level.
  99. # +block+ are tipically composed of database accesses and +async_to+ calls.
  100. # Database accesses are executed transactionallly by +active_record_class+'s transaction method.
  101. # In the +block+, +async_to+ calls invoke NOT immediate queueing but just storing messages
  102. # to the database (assumed to be the same one as application uses).
  103. #
  104. # If the execution of +block+ finishes successfully, database transaction is committed and
  105. # forward process of each stored message begins.
  106. # Forward process composed in two parts. First puts the message into a queue, secondary update
  107. # or delete the entry from a management table.
  108. #
  109. # SAF (store and forward) processing like this guarantees that any message
  110. # is never lost and keeps reasonable performance (without two phase commit).
  111. #
  112. # Examples: Just call async_to method in this block.
  113. #
  114. # ap4r.transaction do
  115. # req = WorldRequest.new([:world_id => 1, :message => "World"})
  116. # ap4r.async_to({:controller => 'async_world', :action => 'execute'},
  117. # {:world_id => 1, :message => "World"})
  118. #
  119. # render :action => 'response'
  120. # end
  121. #
  122. alias :transaction :transaction_with_saf
  123. end
  124. end