diff --git a/sylvilagus/jruby/Gemfile b/sylvilagus/jruby/Gemfile index d2152b6..5d130ae 100644 --- a/sylvilagus/jruby/Gemfile +++ b/sylvilagus/jruby/Gemfile @@ -1,5 +1,7 @@ source :rubygems +gem 'json_pure' + group :development, :test do gem 'rspec' end diff --git a/sylvilagus/jruby/Gemfile.lock b/sylvilagus/jruby/Gemfile.lock index b83e6c3..55a40f8 100644 --- a/sylvilagus/jruby/Gemfile.lock +++ b/sylvilagus/jruby/Gemfile.lock @@ -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 diff --git a/sylvilagus/jruby/lib/sylvilagus/ch04/alert_producer.rb b/sylvilagus/jruby/lib/sylvilagus/ch04/alert_producer.rb index af72dc8..6c3dd3b 100644 --- a/sylvilagus/jruby/lib/sylvilagus/ch04/alert_producer.rb +++ b/sylvilagus/jruby/lib/sylvilagus/ch04/alert_producer.rb @@ -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