diff --git a/sylvilagus/jruby/lib/sylvilagus/ch04/fanout_publisher.rb b/sylvilagus/jruby/lib/sylvilagus/ch04/fanout_publisher.rb new file mode 100644 index 0000000..9bcc6a3 --- /dev/null +++ b/sylvilagus/jruby/lib/sylvilagus/ch04/fanout_publisher.rb @@ -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)} " + 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