64 lines
1.8 KiB
Ruby
64 lines
1.8 KiB
Ruby
require 'sylvilagus/init'
|
|
require 'sylvilagus/ch04'
|
|
require 'optparse'
|
|
require 'rabbitmq-client.jar'
|
|
require 'gson.jar'
|
|
import com.rabbitmq.client.ConnectionFactory
|
|
import com.google.gson.Gson
|
|
|
|
class Sylvilagus::Ch04::AlertProducer
|
|
def main
|
|
opts = {}
|
|
OptionParser.new do |opts|
|
|
opts.on('-r', '--routing-key=',
|
|
'Routing key for message (e.g. myalert.im)') do |r|
|
|
opts[:routing_key] = r
|
|
end
|
|
opts.on('-m', '--message=', 'Message text for alert.') do |m|
|
|
opts[:message] = m
|
|
end
|
|
end.parse!
|
|
|
|
factory = ConnectionFactory.new
|
|
factory.uri = ENV.fetch('SYLVILAGUS_ALERT_AMQP_URI')
|
|
@conn = factory.new_connection
|
|
puts "Made it! conn=#{@conn}"
|
|
channel = @conn.create_channel
|
|
|
|
gson = Gson.new
|
|
puts gson.to_json({'wat' => 'now?', 'huh' => [1, 2, 3]})
|
|
|
|
ensure
|
|
@conn.close if @conn
|
|
end
|
|
end
|
|
|
|
# creds_broker = pika.PlainCredentials('alert_user', 'alertme')
|
|
# conn_params = pika.ConnectionParameters('localhost',
|
|
# virtual_host='/',
|
|
# credentials=creds_broker)
|
|
# conn_broker = pika.BlockingConnection(conn_params)
|
|
#
|
|
# channel = conn_broker.channel()
|
|
#
|
|
# msg = json.dumps({'message': args.message})
|
|
# msg_props = pika.BasicProperties()
|
|
# msg_props.content_type = 'application/json'
|
|
# msg_props.durable = False
|
|
#
|
|
# channel.basic_publish(body=msg,
|
|
# exchange='alerts',
|
|
# properties=msg_props,
|
|
# routing_key=args.routing_key)
|
|
#
|
|
# print(
|
|
# ('Sent message {} tagged with routing key {!r} to ' +
|
|
# 'exchange "alerts" on vhost "/".').format(msg, args.routing_key)
|
|
# )
|
|
#
|
|
# conn_broker.close()
|
|
|
|
if $0 == __FILE__
|
|
Sylvilagus::Ch04::AlertProducer.new.main
|
|
end
|