Touching up the add-points
consumer
to really fake implement points-adding method and more cleanly stop consuming.
This commit is contained in:
parent
c4e8e39176
commit
b3f0f5c4ec
@ -7,9 +7,26 @@ class Sylvilagus::Ch04::AddPointsConsumer
|
|||||||
class Consumer < Java::ComRabbitmqClient::DefaultConsumer
|
class Consumer < Java::ComRabbitmqClient::DefaultConsumer
|
||||||
def handleDelivery(consumer_tag, envelope, properties, body)
|
def handleDelivery(consumer_tag, envelope, properties, body)
|
||||||
message = Java::OrgJruby::RubyString.bytes_to_string(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)
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -24,10 +41,12 @@ class Sylvilagus::Ch04::AddPointsConsumer
|
|||||||
channel.queue_declare('add-points', false, true, false, nil)
|
channel.queue_declare('add-points', false, true, false, nil)
|
||||||
channel.queue_bind('add-points', 'upload-pictures', '')
|
channel.queue_bind('add-points', 'upload-pictures', '')
|
||||||
|
|
||||||
|
consumer = Consumer.new(channel)
|
||||||
puts "Consuming from 'upload-pictures' exchange"
|
puts "Consuming from 'upload-pictures' exchange"
|
||||||
channel.basic_consume('add-points', false, 'add-points-consumer',
|
channel.basic_consume('add-points', false, 'add-points-consumer',
|
||||||
false, false, nil, Consumer.new(channel))
|
false, false, nil, consumer)
|
||||||
loop do
|
loop do
|
||||||
|
break if consumer.done?
|
||||||
sleep 1
|
sleep 1
|
||||||
end
|
end
|
||||||
return 0
|
return 0
|
||||||
|
Loading…
Reference in New Issue
Block a user