Menu Search

send.rb

require 'qpid_proton'
require 'optparse'

options = {}
messages = []

OptionParser.new do |opts|
  opts.banner = "Usage: send.rb [options] <msg1> ... <msgn>"
  opts.on("-a", "--address [addr]", "The receiver's address (def. 0.0.0.0)") do |f|
    options[:address] = f
  end

  opts.parse!

  messages = ARGV
end

options[:address] = "0.0.0.0" unless options[:address]
messages << "Hello world!" if messages.empty?

messenger = Qpid::Proton::Messenger.new
messenger.start
msg = Qpid::Proton::Message.new

messages.each do |message|
  msg.address = options[:address]
  msg.subject = "The time is #{Time.new}"
  msg.content = message
  begin
    messenger.put(msg)
  rescue Qpid::Proton::ProtonError => error
    puts "ERROR: #{error.message}"
    exit
  end
end

begin
  messenger.send
rescue Qpid::Proton::ProtonError => error
  puts "ERROR: #{error.message}"
  puts error.backtrace.join("\n")
  exit
end

puts "SENT: " + messages.join(",")

messenger.stop

Download this file