Adding the alert consumer

plus a tiny bit of touchup to fanout producer.
This commit is contained in:
Dan Buch 2012-11-26 22:47:57 -05:00
parent 952604d431
commit c4e8e39176
2 changed files with 42 additions and 1 deletions

View File

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

View File

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