Switching to Ruby-style methods and setters

cat-town
Dan Buch 12 years ago
parent ef2c0337c6
commit 2e7de65c2d

@ -8,11 +8,11 @@ module Sylvilagus::Ch02::HelloWorld
module ClassMethods module ClassMethods
def with_hello_world_conn(&block) def with_hello_world_conn(&block)
factory = ConnectionFactory.new factory = ConnectionFactory.new
factory.setUri('amqp://guest:guest@localhost:5672') factory.uri = 'amqp://guest:guest@localhost:5672'
conn = factory.newConnection conn = factory.new_connection
channel = conn.createChannel channel = conn.create_channel
channel.exchangeDeclare('hello-exchange', 'direct', true) channel.exchange_declare('hello-exchange', 'direct', true)
channel.queueDeclare('hello-queue', false, false, false, nil) channel.queue_declare('hello-queue', false, false, false, nil)
block.call(conn, channel) block.call(conn, channel)
ensure ensure

@ -9,7 +9,7 @@ class Sylvilagus::Ch02::HelloWorldConsumer < DefaultConsumer
def main def main
with_hello_world_conn do |conn,channel| with_hello_world_conn do |conn,channel|
channel.basicConsume('hello-queue', false, new(channel)) channel.basic_consume('hello-queue', false, new(channel))
loop do loop do
sleep 3 sleep 3
puts "Still waiting..." puts "Still waiting..."
@ -19,10 +19,10 @@ class Sylvilagus::Ch02::HelloWorldConsumer < DefaultConsumer
end end
def handleDelivery(consumer_tag, envelope, properties, body) def handleDelivery(consumer_tag, envelope, properties, body)
delivery_tag = envelope.getDeliveryTag delivery_tag = envelope.get_delivery_tag
body_string = RubyString.bytes_to_string(body) body_string = RubyString.bytes_to_string(body)
puts "Consumed #{body_string.inspect}" puts "Consumed #{body_string.inspect}"
getChannel.basicAck(delivery_tag, false) get_channel.basic_ack(delivery_tag, false)
end end
end end

@ -7,7 +7,7 @@ class Sylvilagus::Ch02::HelloWorldProducer
def main def main
message = ARGV.first || 'snorg' message = ARGV.first || 'snorg'
with_hello_world_conn do |conn,channel| with_hello_world_conn do |conn,channel|
channel.basicPublish('hello-exchange', 'hola', nil, message.to_java_bytes) channel.basic_publish('hello-exchange', 'hola', nil, message.to_java_bytes)
puts "Published #{message.inspect}" puts "Published #{message.inspect}"
end end
end end

Loading…
Cancel
Save