Touching up the add-points consumer

to really fake implement points-adding method and more cleanly stop
consuming.
This commit is contained in:
Dan Buch 2012-11-26 23:29:20 -05:00
parent c4e8e39176
commit b3f0f5c4ec

View File

@ -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