From c4e8e391763b1852d4570ea79b53f2ceb2f3131e Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Mon, 26 Nov 2012 22:47:57 -0500 Subject: [PATCH] Adding the alert consumer plus a tiny bit of touchup to fanout producer. --- .../sylvilagus/ch04/add_points_consumer.rb | 41 +++++++++++++++++++ .../lib/sylvilagus/ch04/fanout_publisher.rb | 2 +- 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 sylvilagus/jruby/lib/sylvilagus/ch04/add_points_consumer.rb diff --git a/sylvilagus/jruby/lib/sylvilagus/ch04/add_points_consumer.rb b/sylvilagus/jruby/lib/sylvilagus/ch04/add_points_consumer.rb new file mode 100644 index 0000000..cbf5e78 --- /dev/null +++ b/sylvilagus/jruby/lib/sylvilagus/ch04/add_points_consumer.rb @@ -0,0 +1,41 @@ +require 'sylvilagus/init' +require 'sylvilagus/ch04' +require 'json' +require 'rabbitmq-client.jar' + +class Sylvilagus::Ch04::AddPointsConsumer + class Consumer < Java::ComRabbitmqClient::DefaultConsumer + def handleDelivery(consumer_tag, envelope, properties, body) + message = Java::OrgJruby::RubyString.bytes_to_string(body) + puts "Got me a message! #{message.inspect}" + + channel.basic_ack(envelope.delivery_tag, false) + end + end + + def main + factory = Java::ComRabbitmqClient::ConnectionFactory.new + factory.uri = ENV.fetch('SYLVILAGUS_AMQP_URI') + @conn = factory.new_connection + channel = @conn.create_channel + channel.exchange_declare( + 'upload-pictures', 'fanout', false, true, false, nil + ) + channel.queue_declare('add-points', false, true, false, nil) + channel.queue_bind('add-points', 'upload-pictures', '') + + puts "Consuming from 'upload-pictures' exchange" + channel.basic_consume('add-points', false, 'add-points-consumer', + false, false, nil, Consumer.new(channel)) + loop do + sleep 1 + end + return 0 + ensure + @conn.close if @conn + end +end + +if $0 == __FILE__ + exit Sylvilagus::Ch04::AddPointsConsumer.new.main +end diff --git a/sylvilagus/jruby/lib/sylvilagus/ch04/fanout_publisher.rb b/sylvilagus/jruby/lib/sylvilagus/ch04/fanout_publisher.rb index 9bcc6a3..f0aca69 100644 --- a/sylvilagus/jruby/lib/sylvilagus/ch04/fanout_publisher.rb +++ b/sylvilagus/jruby/lib/sylvilagus/ch04/fanout_publisher.rb @@ -27,7 +27,7 @@ class Sylvilagus::Ch04::FanoutPublisher props.content_type = 'application/json' props.delivery_mode = 2 - json_msg = JSON.dump({'message' => message}) + json_msg = JSON.dump(message) channel.basic_publish('upload-pictures', '', props, json_msg.to_java_bytes) puts "Sent message #{json_msg.inspect} tagged " <<