Created
May 17, 2009 00:16
-
-
Save danielsdeleo/112857 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "rubygems" | |
require "eventmachine" | |
require "mq" | |
require "amqp" | |
require "bunny" | |
#PUBLISH_MODE = :one_to_n | |
PUBLISH_MODE = :one_to_one_of_n | |
START_TIME = Time.new | |
def finish | |
EM.forks.each {|pid| Process.kill("KILL", pid)} | |
end | |
def gen_qname | |
if PUBLISH_MODE == :one_to_n | |
rand(2 ** 16).to_s(16) | |
else | |
"foobarbaz" | |
end | |
end | |
def timestamp | |
delta_t = Time.new - START_TIME | |
delta_t.to_s | |
end | |
EM.fork(3) do | |
mq = MQ.new | |
qname = gen_qname | |
puts "starting queue ``#{qname}'', PID: #{Process.pid.to_s}" | |
q = mq.queue(qname).bind(mq.topic("test"), :key => "test.key") | |
q.subscribe do |msg| | |
puts "[queue #{qname} (pid #{Process.pid.to_s})] received msg: #{msg} @ #{timestamp}" | |
end | |
end | |
connection = Bunny.new | |
connection.start | |
topic = connection.exchange("test", :type => :topic) | |
(1..10).each do |i| | |
topic.publish("[#{i}] " + timestamp, :key => "test.key") | |
sleep(0.1) | |
end | |
finish | |
connection.stop |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment