Commit e4c2d68
Changed files (2)
lib
5-topic-exchange
lib/5-topic-exchange/publish.rb
@@ -0,0 +1,17 @@
+#!/usr/bin/env ruby
+## encoding: utf-8
+
+require "bunny"
+
+connection = Bunny.new
+connection.start
+
+channel = connection.create_channel
+exchange = channel.topic("topic_logs")
+severity = ARGV.shift || "anonymous.info"
+message = ARGV.empty? ? "Hello World!" : ARGV.join(" ")
+
+exchange.publish(message, routing_key: severity)
+puts " [x] Sent #{severity}:#{message}"
+
+connection.close
lib/5-topic-exchange/subscribe.rb
@@ -0,0 +1,30 @@
+#!/usr/bin/env ruby
+## encoding: utf-8
+
+require "bunny"
+
+if ARGV.empty?
+ abort "Usage: #{$0} [binding key]"
+end
+
+connection = Bunny.new
+connection.start
+
+channel = connection.create_channel
+exchange = channel.topic("topic_logs")
+queue = channel.queue("", exclusive: true)
+
+ARGV.each do |severity|
+ queue.bind(exchange, routing_key: severity)
+end
+
+puts " [*] Waiting for logs. To exit press CTRL+C"
+
+begin
+ queue.subscribe(block: true) do |delivery_info, properties, body|
+ puts " [x] #{delivery_info.routing_key}:#{body}"
+ end
+rescue Interrupt => _
+ channel.close
+ connection.close
+end