Created
September 10, 2013 09:12
-
-
Save oisin/6506907 to your computer and use it in GitHub Desktop.
Simple AMQP clients using Bunny and CloudAMQP
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
# Consumer - subscribes to the queue and gets a callback for each message | |
require 'bunny' | |
AMQP_URL = "your amqp broker URL" | |
conn = Bunny.new(AMQP_URL) | |
conn.start | |
ch = conn.create_channel | |
q = ch.queue("converser.triggers", durable: true) | |
q.subscribe do |delivery_info, metadata, payload| | |
puts "Message Payload: #{payload}" | |
end | |
conn.close |
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
# Producer - publishes 10 testing messages, each 2 seconds apart | |
# | |
require 'bunny' | |
require 'json' | |
AMQP_URL = "your amqp broker URL" | |
conn = Bunny.new(AMQP_URL) | |
conn.start | |
ch = conn.create_channel | |
q = ch.queue("converser.triggers", durable: true) | |
10.times { | |
ch.default_exchange.publish({text: "Testing, testing"}.to_json, persistent: true, routing_key: q.name) | |
sleep 2 | |
} | |
conn.close |
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
source 'https://rubygems.org' | |
gem 'bunny' | |
gem 'json' |
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
$ bundle install | |
Fetching gem metadata from https://rubygems.org/... | |
Resolving dependencies... | |
Using amq-protocol (1.7.0) | |
Using bunny (0.10.6) | |
Using json (1.8.0) | |
Using bundler (1.3.5) | |
Your bundle is complete! | |
Use `bundle show [gemname]` to see where a bundled gem is installed. | |
$ bundle exec ruby bunnypub.rb | |
# Wait for 20 seconds or so! | |
$ bundle exec ruby bunnycon.rb | |
Message Payload: {"text":"Testing, testing"} | |
Message Payload: {"text":"Testing, testing"} | |
Message Payload: {"text":"Testing, testing"} | |
Message Payload: {"text":"Testing, testing"} | |
# etc etc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment