Class: TorqueBox::Messaging::Future

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response_queue, options = { }) ⇒ Future

Returns a new instance of Future.

Parameters:

  • response_queue (TorqueBox::Messaging::Queue)

    The queue where response messages are to be received.

  • options (Hash) (defaults to: { })

    Additional options

Options Hash (options):

  • :correlation_id (String) — default: Future.unique_id

    The correlation_id used on the messages to uniquely identify the call they are for.

  • :default_result_timeout (Integer) — default: 30_000

    The timeout used by default for the receive call. The processing must at least start before the timeout expires, and finish before 2x this timeout.



43
44
45
46
47
48
# File 'lib/torquebox/messaging/future.rb', line 43

def initialize(response_queue, options = { })
  @queue = response_queue
  @correlation_id = options[:correlation_id] || self.class.unique_id
  @default_result_timeout = options[: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_statusesObject (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_idObject (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_timeoutObject

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

#errorObject (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_idString

Returns a unique id useful for correlating a result to its call.

Returns:

  • (String)

    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

Returns:

  • (Boolean)


55
56
57
58
# File 'lib/torquebox/messaging/future.rb', line 55

def complete?
  receive unless @complete || @error
  !!@complete
end

#error?Boolean

Returns:

  • (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.

Parameters:

  • timeout (Integer) (defaults to: default_result_timeout)

    The processing must at least start before the timeout expires, and finish before 2x this timeout.

Returns:

  • the remote result

Raises:



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

Returns:

  • (Boolean)


50
51
52
53
# File 'lib/torquebox/messaging/future.rb', line 50

def started?
  receive unless @started
  !!@started
end

#statusObject

Returns the latest response from the remote processor, if any. Status reporting is optional, and must be handled by the processed task itself.

See Also:

  • FutureResponder#status


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.

Returns:

  • (Boolean)


75
76
77
# File 'lib/torquebox/messaging/future.rb', line 75

def status_changed?
  @prior_status != retrieve_status
end