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