Filling in the consumer side of the JRuby implementation

This commit is contained in:
Dan Buch 2012-11-16 21:15:29 -05:00
parent 43be53a224
commit ef2c0337c6
4 changed files with 66 additions and 22 deletions

View File

@ -0,0 +1,4 @@
module Sylvilagus
module Ch02
end
end

View File

@ -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

View File

@ -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

View File

@ -1,27 +1,14 @@
require 'sylvilagus/init' require 'sylvilagus/ch02/hello_world'
require 'rabbitmq-client.jar' class Sylvilagus::Ch02::HelloWorldProducer
import com.rabbitmq.client.ConnectionFactory
module Sylvilagus
module Ch02
class HelloWorldProducer
class << self class << self
include Sylvilagus::Ch02::HelloWorld::ClassMethods
def main def main
message = ARGV.first || 'snorg' message = ARGV.first || 'snorg'
with_hello_world_conn do |conn,channel|
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) channel.basicPublish('hello-exchange', 'hola', nil, message.to_java_bytes)
puts "Published #{message.inspect}" puts "Published #{message.inspect}"
ensure
conn.close
end
end end
end end
end end