Sha256: dc07b61ee8ae6e77368396fec56e5f62cf0b72c8a5eb9b0e2e4a59721f8a52bc

Contents?: true

Size: 1.98 KB

Versions: 5

Compression:

Stored size: 1.98 KB

Contents

#
# HornetQ Consumer:
#          Use Connection::on_message to consume all messages in separate
#          threads so as not to block the main thread
#          Displays a '.' for every message received
#          Used for performance measurements of consuming messages
#

# Allow examples to be run in-place without requiring a gem install
$LOAD_PATH.unshift File.dirname(__FILE__) + '/../../lib'

require 'yaml'
require 'rubygems'
require 'hornetq'

sleep_time = (ARGV[0] || 60000).to_i
session_count = (ARGV[1] || 1).to_i

config = YAML.load_file(File.dirname(__FILE__) + '/hornetq.yml')['development']

# Using Connect.start since a session must be started in order to consume messages
HornetQ::Client::Connection.connection(config[:connection]) do |connection|
  
  # Create a non-durable TestQueue to receive messages sent to the TestAddress
  connection.session(config[:session]) do |session|
    session.create_queue_ignore_exists('TestAddress', 'TestQueue', false)
  end
  
  # Consume All messages from the queue and gather statistics
  # on_message will call the supplied block for every message received in another
  # thread. As a result, the on_message call returns immediately!
  # Other work can be performed on this thread, or just a sleep as in this example
  #
  # :session_count can be used to spawn multiple consumers simultaneously, each
  # receiving messages simultaneously on their own threads
  connection.on_message(:queue_name    => 'TestQueue', 
                        :session_count => session_count,
                        :statistics    => true) do |message|
    print '.'
    message.acknowledge
  end
  
  puts "Started #{session_count} consumers, will wait for #{sleep_time/1000} seconds before shutting down"
  # Wait for sleep_time before shutting down the server
  sleep(sleep_time/1000)
  
  connection.on_message_statistics.each do |stats|
    puts "Received #{stats[:count]} messages in #{stats[:duration]} seconds at #{stats[:messages_per_second]} messages per second"
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
jruby-hornetq-0.4.0 examples/advanced/consume_on_message.rb
jruby-hornetq-0.3.3 examples/advanced/consume_on_message.rb
jruby-hornetq-0.3.2 examples/advanced/consume_on_message.rb
jruby-hornetq-0.3.1 examples/advanced/consume_on_message.rb
jruby-hornetq-0.3.0.alpha examples/advanced/consume_on_message.rb