Class: HornetQ::Client::MessageHandler

Inherits:
Object
  • Object
show all
Includes:
Java::org.hornetq.api.core.client::MessageHandler
Defined in:
lib/hornetq/client/message_handler.rb

Overview

For internal use only

Instance Method Summary collapse

Constructor Details

#initialize(params = {}, &proc) ⇒ MessageHandler

Parameters:

:statistics Capture statistics on how many messages have been read
   true  : Capture statistics on the number of messages received
           and the time it took to process them.
           The timer starts when the listener instance is created and finishes when either the last message was received,
           or when Consumer::on_message_statistics is called. In this case on_message_statistics::statistics
           can be called several times during processing without affecting the end time.
           The start time and message count is never reset for this instance


15
16
17
18
19
20
21
22
# File 'lib/hornetq/client/message_handler.rb', line 15

def initialize(params={}, &proc)
  @proc = proc

  if params[:statistics]
    @message_count = 0
    @start_time = Time.now
  end
end

Instance Method Details

#onMessage(message) ⇒ Object

Method called for every message received on the queue Per the specification, this method will be called sequentially for each message on the queue. This method will not be called again until its prior invocation has completed. Must be onMessage() since on_message() does not work for interface methods that must be implemented



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/hornetq/client/message_handler.rb', line 28

def onMessage(message)
  begin
    if @message_count
      @message_count += 1
      @last_time = Time.now
    end
    @proc.call message
  rescue SyntaxError, NameError => boom
    HornetQ::logger.error "Unhandled Exception processing Message. Doesn't compile: " + boom
    HornetQ::logger.error "Ignoring poison message:\n#{message.inspect}"
    HornetQ::logger.error boom.backtrace.join("\n")
  rescue StandardError => bang
    HornetQ::logger.error "Unhandled Exception processing Message. Doesn't compile: " + bang
    HornetQ::logger.error "Ignoring poison message:\n#{message.inspect}"
    HornetQ::logger.error boom.backtrace.join("\n")
  rescue => exc
    HornetQ::logger.error "Unhandled Exception processing Message. Exception occurred:\n#{exc}"
    HornetQ::logger.error "Ignoring poison message:\n#{message.inspect}"
    HornetQ::logger.error exc.backtrace.join("\n")
  end
end

#statisticsObject

Return Statistics gathered for this listener Note: These statistics are only useful if the queue is pre-loaded with messages

since the timer start immediately and stops on the last message received


53
54
55
56
57
58
59
60
61
# File 'lib/hornetq/client/message_handler.rb', line 53

def statistics
  raise "First call Consumer::on_message with :statistics=>true before calling MessageConsumer::statistics()" unless @message_count
  duration =(@last_time || Time.now) - @start_time
  { 
    :count => @message_count,
    :duration => duration,
    :messages_per_second => (@message_count/duration).to_i
  }
end