Filling in the consumer side of the JRuby implementation
This commit is contained in:
parent
43be53a224
commit
ef2c0337c6
4
sylvilagus/jruby/lib/sylvilagus/ch02.rb
Normal file
4
sylvilagus/jruby/lib/sylvilagus/ch02.rb
Normal file
@ -0,0 +1,4 @@
|
||||
module Sylvilagus
|
||||
module Ch02
|
||||
end
|
||||
end
|
22
sylvilagus/jruby/lib/sylvilagus/ch02/hello_world.rb
Normal file
22
sylvilagus/jruby/lib/sylvilagus/ch02/hello_world.rb
Normal 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
|
31
sylvilagus/jruby/lib/sylvilagus/ch02/hello_world_consumer.rb
Normal file
31
sylvilagus/jruby/lib/sylvilagus/ch02/hello_world_consumer.rb
Normal 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
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user