Basic Configuration - messagebus/lapine GitHub Wiki

Lapine decouples Publisher from Consumer configuration. When using Lapine, messages will be produced synchronously, using the bunny gem. A consumer process uses the amqp gem to pull messages out of exchange and dispatch them to your own worker classes.

Publisher Configuration

Before sending messages using a producer class, connections and exchanges must be registered with Lapine. Your producer class will use an exchange, which in turn will use a registered connection.

Connections are lazily instantiated, so they will not actually connect to your RabbitMQ broker until needed.

Lapine.add_connection 'my-connection', {
  host: 'my-rabbitmq.mine.com',
  port: 5672,
  user: 'rabbit',
  password: 'meow',
  heartbeat: 30
}

The second argument is a hash that is passed to the Bunny gem when a connection to RabbitMQ is lazily instantiated.

The first argument is an arbitrary name, which is used when registering an exchange:

Lapine.add_exchange 'efrafa.topic', 
  durable: true, 
  connection: 'my-connection',
  type: 'topic'

This can be added in a Rails initializer, for instance config/initializers/lapine.rb.

See Creating a Publisher for information on creating publisher classes.

Consumer Configuration

The Lapine consumer process is a stand-alone process that registers queues, receives messages pushed to those queues and then dispatches the messages to your own classes. This consumer is configured using a YAML configuration file as well as through command-line options.

connection:
  host: '127.0.0.1'
  port: 5672
  ssl: false
  vhost: '/'
  username: 'guest'
  password: 'guest'

# require this file on daemon startup
require:
 - lib/my_handler

# list of available topic exchanges
topics:
 - efrafa.topic

# register these queues and create bindings
queues:
 - q: my-handler
   topic: efrafa.topic
   routing_key: *
   handlers:
     - MyHandler

# remove bindings, drain messages then delete these queues
delete_queues:
 - q: my-old-queue
   topic: efrafa.topic
   routing_key: *
   handlers:
     - MyHandler

Connection properties all have basic defaults, and are overridden by options passed on the command line.

If Rails is present in the application bundle, Lapine will attempt to load Rails. If not, you may use the optional require key in your configuration file to require necessary files.

See Creating a Consumer for information on how to create consumer handlers.

delete_queues

When refactoring consumer code, queues and bindings may change for a variety of reasons. Lapine allows you to delete old queues as a part of a new deployment.

delete_queues:
 - q: old-queue-name
   topic: my.topic
   routing_key: *
   handlers:
     - MyHandler

Note that this configuration includes the details for how to consumer messages. Lapine does the following when deleting queues:

  1. Subscribe to all messages on the queue
  2. Unbind the queue from the exchange, to stop new messages from being added to it
  3. Drain the queue by dispatching messages to registered Handlers
  4. Delete the queue when all messages have been drained

Unbinding a queue from a topic exchange requires that the original routing_key be used. Otherwise AMQP/RabbitMQ will not know which binding to deletes. Draining the queue requires that handlers be defined.