This gem provides a way to create and keep RabbitMQ exchanges using Bunny, and keep them to be used.
Add this line to your application's Gemfile:
gem 'bunny_exchanges_manager'
And then execute:
$ bundle
Or install it yourself as:
$ gem install bunny_exchanges_manager
To use BunnyExchanges
you must define the configuration of each exchange in the
YAML config file.
By default the path to the configuration file is config/exchanges.yml
, but you
can provide a specific path with:
BunnyExchanges.configure do |config|
config.exchanges_path = "my/custom/path"
end
In the configuration file you have to name and define all your exchanges:
an_action:
name: the.exchange.name
type: fanout
durable: false
auto_delete: true
arguments:
one_argument: 1
another_argument: "2"
another_action:
name: another.exchange.name
type: topic
To get a configured exchange:
BunnyExchanges.get(:an_action) # => #<Bunny::Exchange:...>
Optionally, if you configured many connections, you can specify it like this:
BunnyExchanges.get(:an_action, connection_name: :another) # => #<Bunny::Exchange:...>
The default connection config file path is config/rabbitmq.yml
, although you
can provide as much connections as needed with:
BunnyExchanges.configure do |config|
config.connections = {
:default => "config/rabbitmq/default.yml",
:another => "config/rabbitmq/another.yml",
}
end
The connection file needs to have the same parameters Bunny.new
can accept,
for example:
:host: localhost
:port: 5672
:user: guest
:pass: guest
:vhost: /
:threaded: true
:heartbeat: 2
Or it can also have a connection definition for each environment of your app:
development:
:host: localhost
:port: 5672
:user: guest
:pass: guest
:vhost: /
:threaded: true
:heartbeat: 2
test:
:host: localhost
:port: 5672
:user: guest
:pass: guest
:vhost: /
:threaded: true
:heartbeat: 2
In case you want to use this feature, you must provide the environment in an initializer:
BunnyExchanges.configure do |config|
config.env = Rails.env
end
There are situations when you need to reconnect to your AMQP server.
A common case is when you use BunnyExchanges with pre-forking servers like
PhussionPassenger or Unicorn.
For those situations a BunnyExchanges.reset!
method is provided.
The following example shows how to reset your connection after a fork on PhussionPassenger.
if defined?(PhusionPassenger)
PhusionPassenger.on_event(:starting_worker_process) do |forked|
BunnyExchanges.reset! if forked
end
end
- Fork it ( https://github.com/[my-github-username]/bunny_exchanges_manager/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request