Skip to content

Commit

Permalink
Port [Python] example for Chapter 1 to Ruby
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelklishin committed May 28, 2011
1 parent 2798673 commit 368d2a1
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
5 changes: 5 additions & 0 deletions ruby/.gitignore
@@ -0,0 +1,5 @@
*~
*#
*.class
*.rbc
Gemfile.lock
3 changes: 3 additions & 0 deletions ruby/Gemfile
@@ -0,0 +1,3 @@
source :rubygems

gem "amqp", "~> 0.8.0.RC12"
21 changes: 21 additions & 0 deletions ruby/chapter-1/hello_world_consumer.rb
@@ -0,0 +1,21 @@
if defined?(Bundler); Bundler.setup; else; require "rubygems"; end

require "amqp"

EventMachine.run do
AMQP.connect(:host => "localhost", :username => "guest", :password => "guest") do |connection|
puts "Connected. Waiting for messages... Hit Control + C to stop me."

channel = AMQP::Channel.new(connection)
exchange = channel.direct("hello-exchange", :durable => true, :auto_delete => false)
channel.queue("hello-queue", :durable => true, :auto_delete => false).bind(exchange, :routing_key => "hola").subscribe do |header, body|
puts "Received #{body}"

connection.close { EventMachine.stop }
end

# stop on Control + c or after 1 second
stop = Proc.new { connection.close { EventMachine.stop } }
Signal.trap("INT", &stop)
end
end
18 changes: 18 additions & 0 deletions ruby/chapter-1/hello_world_producer.rb
@@ -0,0 +1,18 @@
if defined?(Bundler); Bundler.setup; else; require "rubygems"; end

require "amqp"

EventMachine.run do
AMQP.connect(:host => "localhost", :username => "guest", :password => "guest") do |connection|
puts "Connected. Hit Control + C to stop me."

channel = AMQP::Channel.new(connection)
channel.direct("hello-exchange", :durable => true, :auto_delete => false).
publish(ARGV[0], :routing_key => "hola")

# stop on Control + c or after 1 second
stop = Proc.new { connection.close { EventMachine.stop } }
Signal.trap("INT", &stop)
EventMachine.add_timer(1, &stop)
end
end

0 comments on commit 368d2a1

Please sign in to comment.