Overview
rabbit_mq is an opinionated RabbitMQ client to help you build balanced and consistent Consumers and Producers.
Documentation
The full documentation is published on hex.
The following modules are provided;
Example usage
- Define network topology
- Define Producers
- Define Consumers
- Supervise
- Start producing and consuming messages
Define network topology
defmodule Topology do
use RabbitMQ.Topology,
exchanges: [
{"customer", :topic,
[
{"customer.created", "customer/customer.created", durable: true},
{"customer.updated", "customer/customer.updated", durable: true},
{"#", "customer/#"}
], durable: true}
]
endDefine Producers
defmodule CustomerProducer do
use RabbitMQ.Producer, exchange: "customer", worker_count: 3
def customer_updated(updated_customer) when is_map(updated_customer) do
opts = [
content_type: "application/json",
correlation_id: UUID.uuid4(),
mandatory: true
]
payload = Jason.encode!(updated_customer)
publish(payload, "customer.updated", opts)
end
endDefine Consumers
defmodule CustomerConsumer do
use RabbitMQ.Consumer, queue: "customer/customer.updated", worker_count: 3
require Logger
def consume(payload, meta, channel) do
Logger.info(payload)
ack(channel, meta.delivery_tag)
end
endSupervise
Start as normal under your existing supervision tree.
⚠️ Please note that the Topology module will terminate gracefully as soon as the network is configured.
children = [
Topology,
CustomerConsumer,
CustomerProducer,
# ...and more
]
Supervisor.start_link(children, strategy: :one_for_one)Start producing and consuming messages
To publish, simply call your pre-defined methods.
CustomerProducer.customer_updated(updated_customer)
To consume, use the consume/3 implementation.
Balanced performance and reliability
The RabbitMQ modules are pre-configured with sensible defaults and follow design principles that improve and delicately balance both performance and reliability.
This has been possible through
- a) extensive experience of working with Elixir and RabbitMQ in production; and
- b) meticulous consultation of the below (and more) documents and guides.
⚠️ While most of the heavy-lifting is provided by the library itself, reading through the documents below before running any application in production is thoroughly recommended.