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 class << self
include Sylvilagus::Ch02::HelloWorld::ClassMethods
module Sylvilagus def main
module Ch02 message = ARGV.first || 'snorg'
class HelloWorldProducer with_hello_world_conn do |conn,channel|
class << self channel.basicPublish('hello-exchange', 'hola', nil, message.to_java_bytes)
def main puts "Published #{message.inspect}"
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
end end
end end
end end