Class: TorqueBox::Messaging::FutureResponder

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

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (FutureResponder) initialize(response_queue, correlation_id, message_ttl = nil)

Returns a new instance of FutureResponder



31
32
33
34
35
# File 'messaging/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

+ (Object) current



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

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

+ (Object) status=(status)



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

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

Instance Method Details

- (Object) error=(error)



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

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

- (Object) respond



68
69
70
71
72
73
74
75
76
# File 'messaging/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

- (Object) result=(result)



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

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

- (Object) send_response(priority = :normal)



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'messaging/lib/torquebox/messaging/future_responder.rb', line 92

def send_response(priority = :normal)
  message = {}
  message[:status] = @status if @status_set
  message[:error] = @error if @error
  message[:result] = @result if @result_set
  @queue.publish( message,
                  :correlation_id => @correlation_id,
                  :ttl => @message_ttl,
                  :priority => priority,
                  :encoding => :marshal,
                  :tx => false # can't be a part of the task's tx!
                  )
rescue TypeError => ex
  $stderr.puts "FutureResponder#send_response: Warning: unable to marshal #{@result.inspect}"
end

- (Object) started



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

def started
  send_response(:low)
end

- (Object) status=(status)



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

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