Class: TorqueBox::Messaging::Future

Inherits:
Object
  • Object
show all
Defined in:
messaging/lib/torquebox/messaging/future.rb

Constant Summary

NO_TIMEOUT =
1

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Future) initialize(response_queue, options = { })

Returns a new instance of Future



43
44
45
46
47
48
# File 'messaging/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

- (Object) method_missing(method, *args, &block)



95
96
97
# File 'messaging/lib/torquebox/messaging/future.rb', line 95

def method_missing(method, *args, &block)
  result.send( method, *args, &block )
end

Instance Attribute Details

- (Object) :all_statuses (readonly)

Returns the value of attribute :all_statuses



32
33
34
# File 'messaging/lib/torquebox/messaging/future.rb', line 32

def :all_statuses
  @:all_statuses
end

- (Object) :correlation_id (readonly)

Returns the value of attribute :correlation_id



28
29
30
# File 'messaging/lib/torquebox/messaging/future.rb', line 28

def :correlation_id
  @:correlation_id
end

- (Object) :default_result_timeout

Returns the value of attribute :default_result_timeout



29
30
31
# File 'messaging/lib/torquebox/messaging/future.rb', line 29

def :default_result_timeout
  @:default_result_timeout
end

- (Object) :error (readonly)

Returns the value of attribute :error



27
28
29
# File 'messaging/lib/torquebox/messaging/future.rb', line 27

def :error
  @:error
end

Class Method Details

+ (Object) unique_id



101
102
103
# File 'messaging/lib/torquebox/messaging/future.rb', line 101

def self.unique_id
  java.util.UUID.randomUUID.to_s
end

Instance Method Details

- (Boolean) complete?

Returns:

  • (Boolean)


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

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

- (Boolean) error?

Returns:

  • (Boolean)


60
61
62
63
# File 'messaging/lib/torquebox/messaging/future.rb', line 60

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

- (Object) receive(timeout = NO_TIMEOUT)



111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'messaging/lib/torquebox/messaging/future.rb', line 111

def receive(timeout = NO_TIMEOUT)
  response = @queue.receive( :timeout => timeout, :selector => "JMSCorrelationID = '#{@correlation_id}'" )

  if response
    @started = true
    if response.has_key?( :status )
      @status = response[:status]
      @all_statuses << @status
    end
    @complete = response.has_key?( :result )
    @result ||= response[:result]
    @error ||= response[:error]
  end
end

- (Object) result(timeout = default_result_timeout)

Raises:



85
86
87
88
89
90
91
92
# File 'messaging/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

- (Object) retrieve_status



106
107
108
109
# File 'messaging/lib/torquebox/messaging/future.rb', line 106

def retrieve_status
  receive unless @complete || @error
  @status
end

- (Boolean) started?

Returns:

  • (Boolean)


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

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

- (Object) status



69
70
71
# File 'messaging/lib/torquebox/messaging/future.rb', line 69

def status
  @prior_status = retrieve_status
end

- (Boolean) status_changed?

Returns:

  • (Boolean)


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

def status_changed?
  @prior_status != retrieve_status
end