module RabidMQ
Module to abstract the boilerplate of connecting to rabbitMQ This will also abstract how the credentials are supplied etc
Provides including class with ability to subscribe and react to events that arrive via RabidMQ
Example: class MyClass
include RabidMQ::Listener amqp 'queue_name', 'exchange.name', exclusive: false, routing_key: 'some.routing.*.key' subscribe do |info, meta, data| # code to do on message receipt end
end
Provides any including class with the ability to publish via RabidMQ
Example: class MyClass
include RabidMQ::Publisher
end
MyClass.amqp_broadcast('some_topic', “Class Hello”) instance = MyClass.new instance.amqp_broadcast('some_topic', “Instance Hello”)
Constants
- VERSION
Public Class Methods
channel()
click to toggle source
Get a channel with the Bunny::Session
# File lib/rabid_mq.rb, line 28 def channel return @channel if @channel && !@channel.closed? @channel = connect.create_channel rescue Bunny::ChannelAlreadyClosed => e reconnect end
connect()
click to toggle source
Start a new connection
# File lib/rabid_mq.rb, line 41 def connect connection.tap do |c| c.start end end
connection()
click to toggle source
Provide a new or existing Bunny::Session
# File lib/rabid_mq.rb, line 54 def connection @connection ||= Bunny.new RabidMQ::Config.load_config end
fanout_exchange(topic, **options)
click to toggle source
Provide fanout exchange
# File lib/rabid_mq.rb, line 23 def fanout_exchange(topic, **options) channel.fanout(name_with_env(topic), **options) end
name_with_env(name)
click to toggle source
# File lib/rabid_mq.rb, line 47 def name_with_env(name) return name unless defined?(::Rails) return name if name.match /\[(development|test|production|integration|pod)\]/ name + "[#{Rails.env}]" end
reconnect()
click to toggle source
# File lib/rabid_mq.rb, line 35 def reconnect @channel = nil channel end
topic_exchange(topic, **options)
click to toggle source
Provide a topic exchange on demand connected to the existing channel
# File lib/rabid_mq.rb, line 18 def topic_exchange(topic, **options) channel.topic(name_with_env(topic), **options) end