Adding fanout publishing script for the picture upload example
This commit is contained in:
parent
6d5cc71aa4
commit
952604d431
43
sylvilagus/jruby/lib/sylvilagus/ch04/fanout_publisher.rb
Normal file
43
sylvilagus/jruby/lib/sylvilagus/ch04/fanout_publisher.rb
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
require 'sylvilagus/init'
|
||||||
|
require 'sylvilagus/ch04'
|
||||||
|
require 'json'
|
||||||
|
require 'rabbitmq-client.jar'
|
||||||
|
|
||||||
|
class Sylvilagus::Ch04::FanoutPublisher
|
||||||
|
def main(argv = ARGV.clone)
|
||||||
|
unless argv.length == 3
|
||||||
|
STDERR.puts "Usage: #{File.basename($0)} <image-id> <user-id> <image-path>"
|
||||||
|
return 1
|
||||||
|
end
|
||||||
|
message = {
|
||||||
|
'image_id' => Integer(argv.fetch(0)),
|
||||||
|
'user_id' => Integer(argv.fetch(1)),
|
||||||
|
'image_path' => argv.fetch(2)
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
)
|
||||||
|
|
||||||
|
props = Java::ComRabbitmqClient::AMQP::BasicProperties.new
|
||||||
|
props.content_type = 'application/json'
|
||||||
|
props.delivery_mode = 2
|
||||||
|
|
||||||
|
json_msg = JSON.dump({'message' => message})
|
||||||
|
channel.basic_publish('upload-pictures', '', props, json_msg.to_java_bytes)
|
||||||
|
|
||||||
|
puts "Sent message #{json_msg.inspect} tagged " <<
|
||||||
|
'exchange "upload-pictures" on vhost "/".'
|
||||||
|
return 0
|
||||||
|
ensure
|
||||||
|
@conn.close if @conn
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if $0 == __FILE__
|
||||||
|
exit Sylvilagus::Ch04::FanoutPublisher.new.main
|
||||||
|
end
|
Loading…
Reference in New Issue
Block a user