Class: TorqueBox::Messaging::Future
- Inherits:
-
Object
- Object
- TorqueBox::Messaging::Future
- Defined in:
- lib/torquebox/messaging/future.rb
Overview
A Future encapsulates the result of a long running process, and is used in conjunction with a FutureResponder.
Constant Summary collapse
- NO_TIMEOUT =
We can’t really do no timeout - 1ms is as close as we can get.
1
Instance Attribute Summary collapse
-
#all_statuses ⇒ Object
readonly
Returns all of the statuses seen by this future as an array.
-
#correlation_id ⇒ Object
readonly
Returns the value of attribute correlation_id.
-
#default_result_timeout ⇒ Object
Returns the value of attribute default_result_timeout.
-
#error ⇒ Object
readonly
Returns the remote error (if any).
Class Method Summary collapse
-
.unique_id ⇒ String
A unique id useful for correlating a result to its call.
Instance Method Summary collapse
- #complete? ⇒ Boolean
- #error? ⇒ Boolean
-
#initialize(response_queue, options = { }) ⇒ Future
constructor
A new instance of Future.
-
#method_missing(method, *args, &block) ⇒ Object
Delegates to #result with the default timeout.
-
#result(timeout = default_result_timeout) ⇒ Object
Attempts to return the remote result.
- #started? ⇒ Boolean
-
#status ⇒ Object
Returns the latest response from the remote processor, if any.
-
#status_changed? ⇒ Boolean
Returns true if the status has changed since the last call to #status.
Constructor Details
#initialize(response_queue, options = { }) ⇒ Future
Returns a new instance of Future.
43 44 45 46 47 48 |
# File 'lib/torquebox/messaging/future.rb', line 43 def initialize(response_queue, = { }) @queue = response_queue @correlation_id = [:correlation_id] || self.class.unique_id @default_result_timeout = [:default_result_timeout] || 30_000 @all_statuses = [] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
Delegates to #result with the default timeout.
95 96 97 |
# File 'lib/torquebox/messaging/future.rb', line 95 def method_missing(method, *args, &block) result.send( method, *args, &block ) end |
Instance Attribute Details
#all_statuses ⇒ Object (readonly)
Returns all of the statuses seen by this future as an array.
32 33 34 |
# File 'lib/torquebox/messaging/future.rb', line 32 def all_statuses @all_statuses end |
#correlation_id ⇒ Object (readonly)
Returns the value of attribute correlation_id.
28 29 30 |
# File 'lib/torquebox/messaging/future.rb', line 28 def correlation_id @correlation_id end |
#default_result_timeout ⇒ Object
Returns the value of attribute default_result_timeout.
29 30 31 |
# File 'lib/torquebox/messaging/future.rb', line 29 def default_result_timeout @default_result_timeout end |
#error ⇒ Object (readonly)
Returns the remote error (if any)
27 28 29 |
# File 'lib/torquebox/messaging/future.rb', line 27 def error @error end |
Class Method Details
.unique_id ⇒ String
Returns a unique id useful for correlating a result to its call.
101 102 103 |
# File 'lib/torquebox/messaging/future.rb', line 101 def self.unique_id java.util.UUID.randomUUID.to_s end |
Instance Method Details
#complete? ⇒ Boolean
55 56 57 58 |
# File 'lib/torquebox/messaging/future.rb', line 55 def complete? receive unless @complete || @error !!@complete end |
#error? ⇒ Boolean
60 61 62 63 |
# File 'lib/torquebox/messaging/future.rb', line 60 def error? receive unless @complete || @error !!@error end |
#result(timeout = default_result_timeout) ⇒ Object
Attempts to return the remote result.
85 86 87 88 89 90 91 92 |
# File 'lib/torquebox/messaging/future.rb', line 85 def result(timeout = default_result_timeout) receive( timeout ) unless @started raise TimeoutException.new( "timeout expired waiting for processing to start" ) unless @started receive( timeout ) unless @complete || @error raise TimeoutException.new( "timeout expired waiting for processing to finish" ) unless @complete || @error raise @error if @error @result end |
#started? ⇒ Boolean
50 51 52 53 |
# File 'lib/torquebox/messaging/future.rb', line 50 def started? receive unless @started !!@started end |
#status ⇒ Object
Returns the latest response from the remote processor, if any. Status reporting is optional, and must be handled by the processed task itself.
69 70 71 |
# File 'lib/torquebox/messaging/future.rb', line 69 def status @prior_status = retrieve_status end |
#status_changed? ⇒ Boolean
Returns true if the status has changed since the last call to #status.
75 76 77 |
# File 'lib/torquebox/messaging/future.rb', line 75 def status_changed? @prior_status != retrieve_status end |