Adding the alert consumer
plus a tiny bit of touchup to fanout producer.
This commit is contained in:
parent
952604d431
commit
c4e8e39176
41
sylvilagus/jruby/lib/sylvilagus/ch04/add_points_consumer.rb
Normal file
41
sylvilagus/jruby/lib/sylvilagus/ch04/add_points_consumer.rb
Normal 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
|
@ -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 " <<
|
||||
|
Loading…
Reference in New Issue
Block a user