Class: ModernTimes::JMS::PublishHandle

Inherits:
Object
  • Object
show all
Defined in:
lib/modern_times/jms/publish_handle.rb

Defined Under Namespace

Classes: WorkerResponse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(publisher, jms_message_id, start) ⇒ PublishHandle

Returns a new instance of PublishHandle.



7
8
9
10
11
12
13
# File 'lib/modern_times/jms/publish_handle.rb', line 7

def initialize(publisher, jms_message_id, start)
  @publisher         = publisher
  @jms_message_id    = jms_message_id
  @start             = start
  # Dummy hash will store all the responses from the RequestWorker's matching our publishing destination.
  @dummy_hash        = {}
end

Class Method Details

.clear_dummy_handlingObject

For testing



82
83
84
85
86
87
# File 'lib/modern_times/jms/publish_handle.rb', line 82

def self.clear_dummy_handling
  alias_method :dummy_read_response, :read_response
  alias_method :read_response, :real_read_response
  alias_method :dummy_read_single_response, :read_single_response
  alias_method :read_single_response, :real_read_single_response
end

.setup_dummy_handlingObject



74
75
76
77
78
79
# File 'lib/modern_times/jms/publish_handle.rb', line 74

def self.setup_dummy_handling
  alias_method :real_read_response, :read_response
  alias_method :read_response, :dummy_read_response
  alias_method :real_read_single_response, :read_single_response
  alias_method :read_single_response, :dummy_read_single_response
end

Instance Method Details

#add_dummy_response(name, object) ⇒ Object



70
71
72
# File 'lib/modern_times/jms/publish_handle.rb', line 70

def add_dummy_response(name, object)
  @dummy_hash[name] = object
end

#dummy_read_response(timeout, &block) ⇒ Object



65
66
67
68
# File 'lib/modern_times/jms/publish_handle.rb', line 65

def dummy_read_response(timeout, &block)
  raise "Invalid call to read_response for #{@publisher}, not setup for responding" unless @publisher.response?
  do_read_response(nil, timeout, &block)
end

#read_response(timeout, &block) ⇒ Object

Waits the given timeout for a response message on the queue.

If called w/o a block:

Returns the message
Returns nil on timeout
Raises RemoteException on a remote exception

If called with a block, for instance:

handle.read_response(timeout) do |response|
  response.on_message 'CharCount' do |hash|
    puts "CharCount returned #{hash.inspect}"
  end
  response.on_message 'Length', 'Reverse' do |val|
    puts "#{response.name} returned #{val}"
  end
  response.on_message 'ExceptionRaiser' do |val|
    puts "#{response.name} didn't raise an exception but returned #{val}"
  end
  response.on_timeout 'Reverse' do
    puts "Reverse has it's own timeout handler"
  end
  response.on_timeout do
    puts "#{response.name} did not respond in time"
  end
  response.on_remote_exception 'ExceptionRaiser' do
    puts "It figures that ExceptionRaiser would raise an exception"
  end
  response.on_remote_exception do |e|
    puts "#{response.name} raised an exception #{e.message}\n\t#{e.backtrace.join("\n\t")}"
  end
end

The specified blocks will be called for each response. For instance, LengthWorker#request might return 4 and “Length returned 4” would be displayed. If it failed to respond within the timeout, then “Length did no respond in time” would be displayed. For Workers that raise an exception, they will either be handled by their specific handler if it exists or the default exception handler. If that doesn’t exist either, then the RemoteException will be raised for the whole read_response call. Timeouts will also be handled by the default timeout handler unless a specific one is specified. All messages must have a specific handler specified because the call won’t return until all specified handlers either return, timeout, or return an exception.



56
57
58
59
60
61
62
63
# File 'lib/modern_times/jms/publish_handle.rb', line 56

def read_response(timeout, &block)
  reply_queue = @publisher.reply_queue
  raise "Invalid call to read_response for #{@publisher}, not setup for responding" unless reply_queue
  options = { :destination => reply_queue, :selector => "JMSCorrelationID = '#{@jms_message_id}'" }
  ModernTimes::JMS::Connection.session_pool.consumer(options) do |session, consumer|
    do_read_response(consumer, timeout, &block)
  end
end