Speak to RabbitMQ. This gem serves as a wrapper for publishing messages to RabbitMQ via the Bunny gem, and for consuming messages using the AMQP gem.
Initialization can be done inline in a daemon, or if used in Rails
an initializer should be made at config/initializers/lapine.rb
Register a connection. This connection should be given a name, and a hash of connection options that will be passed through to Bunny.
Lapine.add_connection'my-connection',{host: 'my-rabbitmq.mine.com',port: 5672,user: 'rabbit',password: 'meow'}Then register an exchange.
Lapine.add_exchange'efrafa',durable: true,connection: 'my-connection',# requiredtype: 'topic'# requiredDefine a class that configures which exchange is used. This class
must define #to_hash
require'lapine'classWorkerincludeLapine::Publisherexchange'efrafa'definitialize(action)@action=actionenddefto_hash{'action'=>@action}endendThis class can be used to publish messages onto its exchange:
Worker.new('dig').publishPublishing can take a routing key for topic exchanges:
Worker.new('dig').publish('rabbits.drones')Note that the #initialize method and the contents of #to_hash
are arbitrary.
Please see the Lapine wiki for documentation on defining and configuring consumers.
- This should be dead simple, but everything else was either too complex or assumed very specific configurations different from what we want.
Lapine comes with helpers to stub out calls to RabbitMQ. This allows you to write tests using Lapine, without having to actually run RabbitMQ in your test suite.
require'lapine/test/rspec_helper'RSpec.configuredo |config|
config.includeLapine::Test::RSpecHelper,fake_rabbit: trueconfig.before:each,:fake_rabbitdo |example|
Lapine::Test::RSpecHelper.setup(example)endconfig.after:each,:fake_rabbitdoLapine::Test::RSpecHelper.teardownendendAn example test would look something like this:
RSpec.describeMyPublisher,fake_rabbit: truedolet(:exchange){Lapine.find_exchange('my.topic')}let(:queue){exchange.channel.queue.bind(exchange)}describe'publishing'doit'adds a message to a queue'doMyPublisher.new.publish('my.things')expect(queue.message_count).toeq(1)endendend- Fork it ( https://github.com/[my-github-username]/lapine/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