Last active
May 10, 2022 23:14
-
-
Save zaeemarshad/98718b891bbf4a6e287bba66dc5b0d8a to your computer and use it in GitHub Desktop.
Ruby RMQ publisher - supports publishing to multiple vhosts
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
#!/usr/bin/env ruby | |
# frozen_string_literal: true | |
# | |
require 'bunny' | |
require 'logger' | |
host = 'localhost' | |
queue = 'test1' | |
vhosts = ['/'] | |
exchange = 'amq.topic' | |
user = 'user' | |
password = 'password' | |
payload = 'test message' | |
msg_count = ARGV[0].nil? ? 1000 : ARGV[0].to_i | |
logger = Logger.new($stdout) | |
start = Process.clock_gettime(Process::CLOCK_MONOTONIC) | |
vhosts.each do |vhost| | |
begin | |
connection = Bunny.new(:host => host,:vhost => vhost, :user => user, :password => password) | |
connection.start | |
channel = connection.create_channel | |
x = channel.topic(exchange, :durable => true) | |
count = 0 | |
puts "Published:" | |
msg_count.times do | |
x.publish(payload, routing_key: queue) | |
count +=1 | |
print (count.to_s + "\r") | |
$stdout.flush | |
end | |
ending = Process.clock_gettime(Process::CLOCK_MONOTONIC) | |
puts | |
logger.info "queued #{msg_count} messages to #{queue}@#{vhost}" | |
connection.close | |
connection.clean_up_on_shutdown | |
logger.info "elapsed: #{ending - start}" | |
rescue SystemExit, Interrupt | |
puts | |
logger.info "queued #{count} messages of #{msg_count}" | |
logger.info "shutting down" | |
connection.close | |
connection.clean_up_on_shutdown | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment