Getting the alert producer JRuby implementation working
This commit is contained in:
parent
d42c036909
commit
4acbd0a911
@ -1,5 +1,7 @@
|
|||||||
source :rubygems
|
source :rubygems
|
||||||
|
|
||||||
|
gem 'json_pure'
|
||||||
|
|
||||||
group :development, :test do
|
group :development, :test do
|
||||||
gem 'rspec'
|
gem 'rspec'
|
||||||
end
|
end
|
||||||
|
@ -2,6 +2,7 @@ GEM
|
|||||||
remote: http://rubygems.org/
|
remote: http://rubygems.org/
|
||||||
specs:
|
specs:
|
||||||
diff-lcs (1.1.3)
|
diff-lcs (1.1.3)
|
||||||
|
json_pure (1.7.5)
|
||||||
rspec (2.12.0)
|
rspec (2.12.0)
|
||||||
rspec-core (~> 2.12.0)
|
rspec-core (~> 2.12.0)
|
||||||
rspec-expectations (~> 2.12.0)
|
rspec-expectations (~> 2.12.0)
|
||||||
@ -15,4 +16,5 @@ PLATFORMS
|
|||||||
java
|
java
|
||||||
|
|
||||||
DEPENDENCIES
|
DEPENDENCIES
|
||||||
|
json_pure
|
||||||
rspec
|
rspec
|
||||||
|
@ -1,63 +1,49 @@
|
|||||||
require 'sylvilagus/init'
|
require 'sylvilagus/init'
|
||||||
require 'sylvilagus/ch04'
|
require 'sylvilagus/ch04'
|
||||||
require 'optparse'
|
require 'optparse'
|
||||||
|
require 'json'
|
||||||
require 'rabbitmq-client.jar'
|
require 'rabbitmq-client.jar'
|
||||||
require 'gson.jar'
|
|
||||||
import com.rabbitmq.client.ConnectionFactory
|
|
||||||
import com.google.gson.Gson
|
|
||||||
|
|
||||||
class Sylvilagus::Ch04::AlertProducer
|
class Sylvilagus::Ch04::AlertProducer
|
||||||
def main
|
def main
|
||||||
opts = {}
|
options = {}
|
||||||
OptionParser.new do |opts|
|
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|
|
'Routing key for message (e.g. myalert.im)') do |r|
|
||||||
opts[:routing_key] = r
|
options[:routing_key] = r
|
||||||
end
|
end
|
||||||
opts.on('-m', '--message=', 'Message text for alert.') do |m|
|
opts.on('-mMESSAGE', '--message=MESSAGE',
|
||||||
opts[:message] = m
|
'Message text for alert.') do |m|
|
||||||
|
options[:message] = m
|
||||||
end
|
end
|
||||||
end.parse!
|
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')
|
factory.uri = ENV.fetch('SYLVILAGUS_ALERT_AMQP_URI')
|
||||||
@conn = factory.new_connection
|
@conn = factory.new_connection
|
||||||
puts "Made it! conn=#{@conn}"
|
|
||||||
channel = @conn.create_channel
|
channel = @conn.create_channel
|
||||||
|
channel.exchange_declare('alerts', 'topic', true, false, false, nil)
|
||||||
|
|
||||||
gson = Gson.new
|
props = Java::ComRabbitmqClient::AMQP::BasicProperties.new
|
||||||
puts gson.to_json({'wat' => 'now?', 'huh' => [1, 2, 3]})
|
props.content_type = 'application/json'
|
||||||
|
|
||||||
|
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
|
ensure
|
||||||
@conn.close if @conn
|
@conn.close if @conn
|
||||||
end
|
end
|
||||||
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__
|
if $0 == __FILE__
|
||||||
Sylvilagus::Ch04::AlertProducer.new.main
|
Sylvilagus::Ch04::AlertProducer.new.main
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user