Skip to content

Instantly share code, notes, and snippets.

@lardcanoe
Last active August 29, 2015 14:17
Show Gist options
  • Save lardcanoe/afc43247401154e4b0c8 to your computer and use it in GitHub Desktop.
Save lardcanoe/afc43247401154e4b0c8 to your computer and use it in GitHub Desktop.
Trying to get multiple threads working with March Hare
require 'march_hare'
connection = MarchHare.connect(:host => 'localhost', :thread_pool_size => 8)
channel = connection.create_channel
channel.prefetch = 10
exchange = channel.exchange('test', :type => :direct)
queue = channel.queue('hello.world')
queue.bind(exchange, :routing_key => 'xyz')
queue.purge
consumer = queue.subscribe(:ack => true, :blocking => false, :executor => ::MarchHare::ThreadPools.dynamically_growing) do |headers, msg|
puts msg
x = Random.rand * 3
puts " working for #{x}s"
sleep(x)
headers.ack
end
100.times do |i|
exchange.publish("hello world! #{i}", :routing_key => 'xyz')
end
# make sure all messages are processed before we cancel
# to avoid confusing exceptions from the [already shutdown] executor. MK.
sleep 100.0
consumer.cancel
puts "Disconnecting now..."
at_exit do
channel.close
connection.close
end
require 'march_hare'
connection = MarchHare.connect(:host => 'localhost')
10.times do
channel = connection.create_channel
channel.prefetch = 1
exchange = channel.exchange('test', :type => :direct)
queue = channel.queue('hello.world')
queue.bind(exchange, :routing_key => 'xyz')
queue.subscribe(:ack => true, :blocking => false) do |headers, msg|
puts msg
x = Random.rand * 3
puts " working for #{x}s"
sleep(x)
headers.ack
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment