79 lines
1.9 KiB
Ruby
79 lines
1.9 KiB
Ruby
############
|
|
# USAGE
|
|
############
|
|
#
|
|
# jam-ruby$> bundle exec ruby scripts/simple_amqp_manager.rb
|
|
|
|
############
|
|
# OVERVIEW
|
|
############
|
|
#
|
|
# This is a simple user of AmqpConnectionManager (a jam-ruby class), which will continually
|
|
# send messages to rabbitmq, and, if it receives them, print them.
|
|
#
|
|
|
|
############
|
|
# TESTS
|
|
############
|
|
#
|
|
# Test 1: start with rabbitmq down
|
|
# ------
|
|
# * stop rabbitmq
|
|
# * run this file
|
|
# * start rabbitmq, and messages should be sent/received
|
|
#
|
|
# Test 2: restart rabbitmq at steady state
|
|
# ------
|
|
# * start rabbitmq
|
|
# * run this file
|
|
# * messages should be sent/received
|
|
# * restart rabbitmq
|
|
# * once rabbitmq is back up, messages should be sent/received
|
|
#
|
|
|
|
require 'amqp'
|
|
require 'active_record'
|
|
require 'jam_db'
|
|
|
|
# initialize ActiveRecord's db connection
|
|
ActiveRecord::Base.establish_connection(YAML::load(File.open('config/database.yml'))["test"])
|
|
|
|
require 'jam_ruby'
|
|
|
|
# initialize logging
|
|
Logging.logger.root.level = :debug
|
|
Logging.logger.root.appenders = Logging.appenders.stdout
|
|
|
|
|
|
log = Logging.logger['SimpleAmqpManager']
|
|
|
|
|
|
include JamRuby
|
|
|
|
users_exchange = nil
|
|
|
|
EventMachine.run do
|
|
|
|
manager = AmqpConnectionManager.new(true, 4, :host => '127.0.0.1', :port => 5672)
|
|
manager.connect do |channel|
|
|
log.debug "initializing channel with registration to dog topic"
|
|
|
|
users_exchange = channel.topic('dogs')
|
|
# create user messaging topic
|
|
user_topic = channel.queue("", :auto_delete => true)
|
|
user_topic.bind(users_exchange, :routing_key => "dog.#")
|
|
user_topic.purge
|
|
|
|
user_topic.subscribe(:ack => false) do |headers, msg|
|
|
log.debug("received message from dog queue: #{msg}")
|
|
end
|
|
end
|
|
|
|
EventMachine.add_periodic_timer(2) do
|
|
unless users_exchange.nil? # if we have not connected yet ever, this will be nil
|
|
log.debug "sending message: [super secret message]"
|
|
users_exchange.publish("[super secret message]", :routing_key => "dog.leg")
|
|
end
|
|
end
|
|
end
|