Last active
November 17, 2019 08:11
rabbitmq ruby ( for blogging http://tech.jinto.pe.kr/2014/10/16/RabbitMQ.html)
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
# 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 |
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
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