Created
June 23, 2011 02:38
-
-
Save michaelklishin/1041778 to your computer and use it in GitHub Desktop.
An example of how messages can be load balanced (in the round robin manner) with amqp gem 0.8.x using the default exchange and multiple channels to separate consumers.
This file contains hidden or 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
| #!/usr/bin/env ruby | |
| # encoding: utf-8 | |
| require "rubygems" | |
| require "amqp" | |
| EventMachine.run do | |
| AMQP.connect do |connection| | |
| channel1 = AMQP::Channel.new(connection) | |
| channel2 = AMQP::Channel.new(connection) | |
| exchange = channel1.default_exchange | |
| q1 = channel1.queue("amqpgem.examples.queues.shared", :auto_delete => true) | |
| q1.subscribe do |payload| | |
| puts "Queue #{q1.name} on channel 1 received #{payload}" | |
| end | |
| q2 = channel2.queue("amqpgem.examples.queues.shared", :auto_delete => true) | |
| q2.subscribe do |payload| | |
| puts "Queue #{q2.name} on channel 2 received #{payload}" | |
| end | |
| # Publish some test data in a bit, after queues are declared & bound | |
| EventMachine.add_timer(0.3) do | |
| 5.times { |i| exchange.publish("Hello #{i}, fanout exchanges world!", :routing_key => "amqpgem.examples.queues.shared") } | |
| end | |
| show_stopper = Proc.new { connection.close { EventMachine.stop } } | |
| Signal.trap "TERM", show_stopper | |
| EM.add_timer(3, show_stopper) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment