Archiving a bunch of old stuff
This commit is contained in:
23
oldstuff/sylvilagus/jruby/lib/sylvilagus/ch02/hello_world.rb
Normal file
23
oldstuff/sylvilagus/jruby/lib/sylvilagus/ch02/hello_world.rb
Normal file
@@ -0,0 +1,23 @@
|
||||
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_channel(&block)
|
||||
factory = ConnectionFactory.new
|
||||
factory.uri = ENV.fetch('SYLVILAGUS_AMQP_URI')
|
||||
conn = factory.new_connection
|
||||
|
||||
channel = conn.create_channel
|
||||
channel.exchange_declare('hello-exchange', 'direct', true)
|
||||
channel.queue_declare('hello-queue', false, false, false, nil)
|
||||
|
||||
block.call(channel)
|
||||
ensure
|
||||
conn.close
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,41 @@
|
||||
require 'sylvilagus/ch02/hello_world'
|
||||
|
||||
class Sylvilagus::Ch02::HelloWorldConsumer < Java::ComRabbitmqClient::DefaultConsumer
|
||||
class << self
|
||||
include Sylvilagus::Ch02::HelloWorld::ClassMethods
|
||||
|
||||
def main
|
||||
with_hello_world_channel do |channel|
|
||||
consumer = new(channel)
|
||||
|
||||
STDERR.puts 'Starting consume loop...'
|
||||
channel.basic_consume('hello-queue', false, consumer)
|
||||
|
||||
loop do
|
||||
sleep 1
|
||||
break if consumer.done?
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def done?
|
||||
@done ||= false
|
||||
end
|
||||
|
||||
def handleDelivery(consumer_tag, envelope, properties, body)
|
||||
body_string = Java::OrgJruby::RubyString.bytes_to_string(body)
|
||||
|
||||
puts "Consumed #{body_string.inspect}"
|
||||
channel.basic_ack(envelope.delivery_tag, false)
|
||||
|
||||
if body_string == 'quit'
|
||||
STDERR.puts 'Quitting...'
|
||||
@done = true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if $0 == __FILE__
|
||||
Sylvilagus::Ch02::HelloWorldConsumer.main
|
||||
end
|
||||
@@ -0,0 +1,20 @@
|
||||
require 'sylvilagus/ch02/hello_world'
|
||||
|
||||
class Sylvilagus::Ch02::HelloWorldProducer
|
||||
class << self
|
||||
include Sylvilagus::Ch02::HelloWorld::ClassMethods
|
||||
|
||||
def main(args = ARGV.clone)
|
||||
raise 'Missing message arg!' if args.empty?
|
||||
message = args.fetch(0)
|
||||
with_hello_world_channel do |channel|
|
||||
channel.basic_publish('hello-exchange', 'hola', nil, message.to_java_bytes)
|
||||
puts "Published #{message.inspect}"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if $0 == __FILE__
|
||||
Sylvilagus::Ch02::HelloWorldProducer.main(ARGV)
|
||||
end
|
||||
Reference in New Issue
Block a user