Class: TorqueBox::Messaging::FutureResponder

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

Overview

A FutureResponder encapsulates sending the results of a long running process to a Future.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response_queue, correlation_id, message_ttl = nil) ⇒ FutureResponder

Returns a new instance of FutureResponder.

Parameters:

  • response_queue (TorqueBox::Messaging::Queue)

    The queue where response messages are to be published.

  • correlation_id (String)

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

  • message_ttl (Integer) (defaults to: nil)

    The time-to-live used on messages to prevent them from staying in the queue indefinately if the result is never accessed.



31
32
33
34
35
# File 'lib/torquebox/messaging/future_responder.rb', line 31

def initialize(response_queue, correlation_id, message_ttl = nil)
  @queue = response_queue
  @correlation_id = correlation_id
  @message_ttl = message_ttl || 600_000
end

Class Method Details

.currentObject

Convenience method that returns the thread local responder. Only valid inside a block passed to #respond.



80
81
82
# File 'lib/torquebox/messaging/future_responder.rb', line 80

def self.current
  Thread.current[:future_responder]
end

.status=(status) ⇒ Object

Convenience method that allows you to send a status message via the current responder. Only valid inside a block passed to #respond.



87
88
89
# File 'lib/torquebox/messaging/future_responder.rb', line 87

def self.status=(status)
  current.status = status if current
end

Instance Method Details

#error=(error) ⇒ Object

Signal that an error occurred during processing.

Parameters:

  • The (Exception)

    error!



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

def error=(error)
  @error = error
  send_response :high 
end

#respondObject

Handles started/complete/error for you around the given block. The current responder is avaiable inside the block via current, which is useful for sending #status messages.



68
69
70
71
72
73
74
75
76
# File 'lib/torquebox/messaging/future_responder.rb', line 68

def respond
  started
  Thread.current[:future_responder] = self
  self.result = yield 
rescue Exception => e
  self.error = e
  $stderr.puts "FutureResponder#respond: An error occured: ", e
  $stderr.puts e.backtrace.join( "\n" )
end

#result=(result) ⇒ Object

Signal that processing has completed.

Parameters:

  • The

    result of the operation.



52
53
54
55
56
# File 'lib/torquebox/messaging/future_responder.rb', line 52

def result=(result)
  @result_set = true
  @result = result
  send_response :high 
end

#startedObject

Signal that processing has started.



38
39
40
# File 'lib/torquebox/messaging/future_responder.rb', line 38

def started
  send_response(:low)
end

#status=(status) ⇒ Object

Report the current status back to the client. The status value is application specific.



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

def status=(status)
  @status_set = true
  @status = status
  send_response
end