Getting the alert producer JRuby implementation working

cat-town
Dan Buch 12 years ago
parent d42c036909
commit 4acbd0a911

@ -1,5 +1,7 @@
source :rubygems
gem 'json_pure'
group :development, :test do
gem 'rspec'
end

@ -2,6 +2,7 @@ GEM
remote: http://rubygems.org/
specs:
diff-lcs (1.1.3)
json_pure (1.7.5)
rspec (2.12.0)
rspec-core (~> 2.12.0)
rspec-expectations (~> 2.12.0)
@ -15,4 +16,5 @@ PLATFORMS
java
DEPENDENCIES
json_pure
rspec

@ -1,63 +1,49 @@
require 'sylvilagus/init'
require 'sylvilagus/ch04'
require 'optparse'
require 'json'
require 'rabbitmq-client.jar'
require 'gson.jar'
import com.rabbitmq.client.ConnectionFactory
import com.google.gson.Gson
class Sylvilagus::Ch04::AlertProducer
def main
opts = {}
options = {}
OptionParser.new do |opts|
opts.on('-r', '--routing-key=',
opts.on('-rROUTING_KEY', '--routing-key=ROUTING_KEY',
'Routing key for message (e.g. myalert.im)') do |r|
opts[:routing_key] = r
options[:routing_key] = r
end
opts.on('-m', '--message=', 'Message text for alert.') do |m|
opts[:message] = m
opts.on('-mMESSAGE', '--message=MESSAGE',
'Message text for alert.') do |m|
options[:message] = m
end
end.parse!
factory = ConnectionFactory.new
unless options[:message] && options[:routing_key]
STDERR.puts 'Need both message and routing_key!'
exit 1
end
factory = Java::ComRabbitmqClient::ConnectionFactory.new
factory.uri = ENV.fetch('SYLVILAGUS_ALERT_AMQP_URI')
@conn = factory.new_connection
puts "Made it! conn=#{@conn}"
channel = @conn.create_channel
channel.exchange_declare('alerts', 'topic', true, false, false, nil)
props = Java::ComRabbitmqClient::AMQP::BasicProperties.new
props.content_type = 'application/json'
gson = Gson.new
puts gson.to_json({'wat' => 'now?', 'huh' => [1, 2, 3]})
json_msg = JSON.dump({'message' => options[:message]})
channel.basic_publish('alerts', options[:routing_key],
true, true, props, json_msg.to_java_bytes)
puts "Sent message #{json_msg.inspect} tagged " <<
"with routing key #{options[:routing_key].inspect} to " <<
'exchange "alerts" on vhost "/".'
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

Loading…
Cancel
Save