Skip to content

Instantly share code, notes, and snippets.

@anewusername1
Created August 17, 2011 15:24
Show Gist options
  • Save anewusername1/1151767 to your computer and use it in GitHub Desktop.
Save anewusername1/1151767 to your computer and use it in GitHub Desktop.
Multicast pub/sub using rabbitmq, amqp, and bunny
require 'bunny'
b_con = Bunny.new(host: 'localhost', port: 5672, logging: true)
b_con.start
b_q = b_con.queue('events')
b_ex = b_con.exchange('events', type: :topic)
b_ex.publish('message!!', key: 'usermanager.user.login')
require 'amqp'
AMQP.start(host: 'localhost', port: 5672) do |connection|
channel = AMQP::Channel.new(connection)
exchange = channel.topic("events")
# the first subscriber will declare a different queue name than the second
# but will use the same routing key
channel.queue("service1.usermanager.user.login").bind(exchange, :routing_key => "usermanager.user.login").subscribe do |metadata, payload|
puts "Subscriber 1: An update for Service 1: #{payload}, routing key is #{metadata.routing_key}"
end
show_stopper = Proc.new { puts 'closing the rabbit hole'; connection.close { EventMachine.stop } }
['TERM'].each do |sig|
Signal.trap sig, show_stopper
end
end
require 'amqp'
AMQP.start(host: 'localhost', port: 5672) do |connection|
channel = AMQP::Channel.new(connection)
exchange = channel.topic("events")
# the second subscriber will declare a different queue name than the first
# but will use the same routing key
channel.queue("service2.usermanager.user.login").bind(exchange, :routing_key => "usermanager.user.login").subscribe do |metadata, payload|
puts "Subscriber 2: An update for Service 2: #{payload}, routing key is #{metadata.routing_key}"
end
show_stopper = Proc.new { puts 'closing the rabbit hole'; connection.close { EventMachine.stop } }
['TERM'].each do |sig|
Signal.trap sig, show_stopper
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment