Skip to content

Instantly share code, notes, and snippets.

@krdlab
Created December 4, 2012 04:56
Show Gist options
  • Save krdlab/4200685 to your computer and use it in GitHub Desktop.
Save krdlab/4200685 to your computer and use it in GitHub Desktop.
use RabbitMQ on ruby
#!/usr/bin/env ruby
# encoding: utf-8
require "rubygems"
require "amqp"
require "msgpack"
EventMachine.run do
AMQP.connect do |conn|
channel = AMQP::Channel.new(conn)
exchange = channel.direct("test.exchange", :durable => true)
channel.queue("test.quene", :durable => true) do |queue|
queue.bind(exchange, :routing_key => "test.routing_key").subscribe(:ack => true) do |headers, payload|
puts "headers=#{headers}"
puts "payload=#{MessagePack.unpack(payload)}"
channel.acknowledge(headers.delivery_tag, false)
end
end
EM.add_timer(1) do
exchange.publish([1,2,3].to_msgpack, :routing_key => "test.routing_key", :persistent => true)
end
EM.add_timer(5) {
conn.close { EventMachine.stop }
puts "stop"
}
end
puts "start..."
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment