From d3918bd79c0924be6247cb73aa325df046fa0515 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Thu, 22 Nov 2012 16:40:19 -0500 Subject: [PATCH] Goofing around with JRuby implementation of chapter 4 alert stuff --- sylvilagus/jruby/Gemfile | 5 ++ sylvilagus/jruby/Gemfile.lock | 18 ++++++ sylvilagus/jruby/lib/sylvilagus.rb | 2 + sylvilagus/jruby/lib/sylvilagus/ch04.rb | 4 ++ .../lib/sylvilagus/ch04/alert_producer.rb | 63 +++++++++++++++++++ 5 files changed, 92 insertions(+) create mode 100644 sylvilagus/jruby/Gemfile create mode 100644 sylvilagus/jruby/Gemfile.lock create mode 100644 sylvilagus/jruby/lib/sylvilagus.rb create mode 100644 sylvilagus/jruby/lib/sylvilagus/ch04.rb create mode 100644 sylvilagus/jruby/lib/sylvilagus/ch04/alert_producer.rb diff --git a/sylvilagus/jruby/Gemfile b/sylvilagus/jruby/Gemfile new file mode 100644 index 0000000..d2152b6 --- /dev/null +++ b/sylvilagus/jruby/Gemfile @@ -0,0 +1,5 @@ +source :rubygems + +group :development, :test do + gem 'rspec' +end diff --git a/sylvilagus/jruby/Gemfile.lock b/sylvilagus/jruby/Gemfile.lock new file mode 100644 index 0000000..b83e6c3 --- /dev/null +++ b/sylvilagus/jruby/Gemfile.lock @@ -0,0 +1,18 @@ +GEM + remote: http://rubygems.org/ + specs: + diff-lcs (1.1.3) + rspec (2.12.0) + rspec-core (~> 2.12.0) + rspec-expectations (~> 2.12.0) + rspec-mocks (~> 2.12.0) + rspec-core (2.12.0) + rspec-expectations (2.12.0) + diff-lcs (~> 1.1.3) + rspec-mocks (2.12.0) + +PLATFORMS + java + +DEPENDENCIES + rspec diff --git a/sylvilagus/jruby/lib/sylvilagus.rb b/sylvilagus/jruby/lib/sylvilagus.rb new file mode 100644 index 0000000..6174b76 --- /dev/null +++ b/sylvilagus/jruby/lib/sylvilagus.rb @@ -0,0 +1,2 @@ +module Sylvilagus +end diff --git a/sylvilagus/jruby/lib/sylvilagus/ch04.rb b/sylvilagus/jruby/lib/sylvilagus/ch04.rb new file mode 100644 index 0000000..eb9a071 --- /dev/null +++ b/sylvilagus/jruby/lib/sylvilagus/ch04.rb @@ -0,0 +1,4 @@ +module Sylvilagus + module Ch04 + end +end diff --git a/sylvilagus/jruby/lib/sylvilagus/ch04/alert_producer.rb b/sylvilagus/jruby/lib/sylvilagus/ch04/alert_producer.rb new file mode 100644 index 0000000..af72dc8 --- /dev/null +++ b/sylvilagus/jruby/lib/sylvilagus/ch04/alert_producer.rb @@ -0,0 +1,63 @@ +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