From ef2c0337c6ad1a5ebf571d34a7ebdb8d97905eca Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Fri, 16 Nov 2012 21:15:29 -0500 Subject: [PATCH] Filling in the consumer side of the JRuby implementation --- sylvilagus/jruby/lib/sylvilagus/ch02.rb | 4 +++ .../jruby/lib/sylvilagus/ch02/hello_world.rb | 22 +++++++++++++ .../sylvilagus/ch02/hello_world_consumer.rb | 31 +++++++++++++++++++ .../sylvilagus/ch02/hello_world_producer.rb | 31 ++++++------------- 4 files changed, 66 insertions(+), 22 deletions(-) create mode 100644 sylvilagus/jruby/lib/sylvilagus/ch02.rb create mode 100644 sylvilagus/jruby/lib/sylvilagus/ch02/hello_world.rb create mode 100644 sylvilagus/jruby/lib/sylvilagus/ch02/hello_world_consumer.rb diff --git a/sylvilagus/jruby/lib/sylvilagus/ch02.rb b/sylvilagus/jruby/lib/sylvilagus/ch02.rb new file mode 100644 index 0000000..4f169ac --- /dev/null +++ b/sylvilagus/jruby/lib/sylvilagus/ch02.rb @@ -0,0 +1,4 @@ +module Sylvilagus + module Ch02 + end +end diff --git a/sylvilagus/jruby/lib/sylvilagus/ch02/hello_world.rb b/sylvilagus/jruby/lib/sylvilagus/ch02/hello_world.rb new file mode 100644 index 0000000..d706c35 --- /dev/null +++ b/sylvilagus/jruby/lib/sylvilagus/ch02/hello_world.rb @@ -0,0 +1,22 @@ +require 'sylvilagus/init' +require 'sylvilagus/ch02' + +require 'rabbitmq-client.jar' +import com.rabbitmq.client.ConnectionFactory + +module Sylvilagus::Ch02::HelloWorld + module ClassMethods + def with_hello_world_conn(&block) + factory = ConnectionFactory.new + factory.setUri('amqp://guest:guest@localhost:5672') + conn = factory.newConnection + channel = conn.createChannel + channel.exchangeDeclare('hello-exchange', 'direct', true) + channel.queueDeclare('hello-queue', false, false, false, nil) + + block.call(conn, channel) + ensure + conn.close + end + end +end diff --git a/sylvilagus/jruby/lib/sylvilagus/ch02/hello_world_consumer.rb b/sylvilagus/jruby/lib/sylvilagus/ch02/hello_world_consumer.rb new file mode 100644 index 0000000..a30d8d5 --- /dev/null +++ b/sylvilagus/jruby/lib/sylvilagus/ch02/hello_world_consumer.rb @@ -0,0 +1,31 @@ +require 'sylvilagus/ch02/hello_world' + +import com.rabbitmq.client.DefaultConsumer +import org.jruby.RubyString + +class Sylvilagus::Ch02::HelloWorldConsumer < DefaultConsumer + class << self + include Sylvilagus::Ch02::HelloWorld::ClassMethods + + def main + with_hello_world_conn do |conn,channel| + channel.basicConsume('hello-queue', false, new(channel)) + loop do + sleep 3 + puts "Still waiting..." + end + end + end + end + + def handleDelivery(consumer_tag, envelope, properties, body) + delivery_tag = envelope.getDeliveryTag + body_string = RubyString.bytes_to_string(body) + puts "Consumed #{body_string.inspect}" + getChannel.basicAck(delivery_tag, false) + end +end + +if $0 == __FILE__ + Sylvilagus::Ch02::HelloWorldConsumer.main +end diff --git a/sylvilagus/jruby/lib/sylvilagus/ch02/hello_world_producer.rb b/sylvilagus/jruby/lib/sylvilagus/ch02/hello_world_producer.rb index 7b1ad81..ad2bc3b 100644 --- a/sylvilagus/jruby/lib/sylvilagus/ch02/hello_world_producer.rb +++ b/sylvilagus/jruby/lib/sylvilagus/ch02/hello_world_producer.rb @@ -1,27 +1,14 @@ -require 'sylvilagus/init' +require 'sylvilagus/ch02/hello_world' -require 'rabbitmq-client.jar' -import com.rabbitmq.client.ConnectionFactory +class Sylvilagus::Ch02::HelloWorldProducer + class << self + include Sylvilagus::Ch02::HelloWorld::ClassMethods -module Sylvilagus - module Ch02 - class HelloWorldProducer - class << self - def main - message = ARGV.first || 'snorg' - - factory = ConnectionFactory.new - factory.setUri('amqp://guest:guest@localhost:5672') - conn = factory.newConnection - channel = conn.createChannel - channel.exchangeDeclare('hello-exchange', 'direct', true) - channel.queueDeclare('hello-queue', false, false, false, nil) - - channel.basicPublish('hello-exchange', 'hola', nil, message.to_java_bytes) - puts "Published #{message.inspect}" - ensure - conn.close - end + def main + message = ARGV.first || 'snorg' + with_hello_world_conn do |conn,channel| + channel.basicPublish('hello-exchange', 'hola', nil, message.to_java_bytes) + puts "Published #{message.inspect}" end end end