From b3f0f5c4ec7cd0d8e3c003ebbfd545fefffbf8eb Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Mon, 26 Nov 2012 23:29:20 -0500 Subject: [PATCH] Touching up the `add-points` consumer to really fake implement points-adding method and more cleanly stop consuming. --- .../sylvilagus/ch04/add_points_consumer.rb | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/sylvilagus/jruby/lib/sylvilagus/ch04/add_points_consumer.rb b/sylvilagus/jruby/lib/sylvilagus/ch04/add_points_consumer.rb index cbf5e78..3831a89 100644 --- a/sylvilagus/jruby/lib/sylvilagus/ch04/add_points_consumer.rb +++ b/sylvilagus/jruby/lib/sylvilagus/ch04/add_points_consumer.rb @@ -7,9 +7,26 @@ 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}" + + if message == 'quit' + channel.basic_cancel(consumer_tag) + @done = true + return + end + + add_points_to_user(JSON.parse(message).fetch('user_id')) channel.basic_ack(envelope.delivery_tag, false) + rescue StandardError + channel.basic_nack(envelope.delivery_tag, false) + end + + def add_points_to_user(user_id) + puts "Adding points to user: #{user_id}" + end + + def done? + !!@done end end @@ -24,10 +41,12 @@ class Sylvilagus::Ch04::AddPointsConsumer channel.queue_declare('add-points', false, true, false, nil) channel.queue_bind('add-points', 'upload-pictures', '') + consumer = Consumer.new(channel) puts "Consuming from 'upload-pictures' exchange" channel.basic_consume('add-points', false, 'add-points-consumer', - false, false, nil, Consumer.new(channel)) + false, false, nil, consumer) loop do + break if consumer.done? sleep 1 end return 0