Skip to content

Instantly share code, notes, and snippets.

@jinto
Last active November 17, 2019 08:11
Show Gist options
  • Save jinto/97c7a4558fb70d7f33c1 to your computer and use it in GitHub Desktop.
Save jinto/97c7a4558fb70d7f33c1 to your computer and use it in GitHub Desktop.
rabbitmq ruby ( for blogging http://tech.jinto.pe.kr/2014/10/16/RabbitMQ.html)
# encoding: utf-8
require "bunny"
def hi(name)
conn = Bunny.new(:automatically_recover => false)
conn.start
ch = conn.create_channel(nil, 8)
q = ch.queue(name)#, :exclusive => true)
@ii=0
begin
puts " [*] Waiting for messages for #{name}. To exit press CTRL+C"
q.subscribe(:block => false) do |delivery_info, properties, body|
@ii+=1
puts " [x] Received #{name} #{body} #{@ii}"
sleep 9
@ii-=1
puts " end #{@ii}"
end
rescue Interrupt => _
conn.close
exit(0)
end
return q
end
q = hi("backend1")
hi("backend2")
puts "wating messages"
loop do
if q.message_count > 0
msg = q.pop[:payload]
#puts "Found Message: #{msg}"
else
sleep 5
end
end
require "bunny"
conn = Bunny.new(:automatically_recover => false)
conn.start
ch = conn.create_channel
q = ch.queue("backend1")
ch.default_exchange.publish("백엔드 1번 메시지!", :routing_key => q.name)
puts " [x] Sent '백엔드 1번!'"
(1..3).each do |n|
q = ch.queue("backend2")
ch.default_exchange.publish("백엔드 2번 메시지!", :routing_key => q.name)
puts " [x] Sent '백엔드 2번!'"
end
conn.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment