Class: JMS::MessageListenerImpl

Inherits:
Object
  • Object
show all
Includes:
MessageListener
Defined in:
lib/jms/message_listener_impl.rb

Overview

For internal use only by JMS::Connection

Instance Method Summary collapse

Constructor Details

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

Parameters:

:statistics Capture statistics on how many messages have been read
   true  : This method will 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 Destination::statistics is called. In this case MessageConsumer::statistics
           can be called several times during processing without affecting the end time.
           Also, the start time and message count is not reset until MessageConsumer::each
           is called again with :statistics => true

           The statistics gathered are returned when :statistics => true and :async => false

35
36
37
38
39
40
41
42
# File 'lib/jms/message_listener_impl.rb', line 35

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 JMS 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


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/jms/message_listener_impl.rb', line 48

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

#statisticsObject

Return Statistics gathered for this listener


71
72
73
74
75
76
77
# File 'lib/jms/message_listener_impl.rb', line 71

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