Conejo is an OTP application/library based on pma/amqp which will help you to define your AMQP/RabbitMQ publishers and consumers in an easier way.
I highly recommend to initiate your publishers/consumers under a Supervisor.
-
Add
conejo
to your list of dependencies inmix.exs
:def deps do [{:conejo, "~> 0.6"}] end
- Define your config files. Try to respect this configuration. It is based on the options that are needed by pma/amqp.
- If you whish to use a virtual host, you can specify an optional parameter called "vhost" containing your wanted vhost (eg. "dev").
config :my_application, :consumer,
exchange: "my_exchange",
exchange_type: "topic",
queue_name: "my_queue",
queue_declaration_options: [{:auto_delete, true}, {:exclusive, true}],
queue_bind_options: [routing_key: "example"],
consume_options: [no_ack: true]
config :conejo,
host: "my_host",
port: 5672,
vhost: "dev",
username: "user",
password: "pass"
Confex is supported.
- Define and run your Consumers. Code the function handle_consume(channel, tag, redelivered, payload) which will be executed when a message is received.
defmodule MyApplication.MyConsumer do
use Conejo.Consumer
def handle_consume(_channel, payload, _params) do
IO.puts "Received -> #{inspect payload}"
end
end
options = Application.get_all_env(:my_application)[:consumer]
{:ok, consumer} = MyApplication.MyConsumer.start_link(options, [name: :consumer])
- Define and run your Publisher.
defmodule MyApplication.MyPublisher do
use Conejo.Publisher
end
{:ok, publisher} = MyApplication.MyPublisher.start_link([], [name: :publisher])
#Synchronous
MyApplication.MyPublisher.sync_publish(:publisher, "my_exchange", "example", "Hola")
#Asynchronous
MyApplication.MyPublisher.async_publish(:publisher, "my_exchange", "example", "Adios")
- Run the tests. You have to have Docker installed in you computer.
mix test --no-start
Conejo
dependencies use lager
for logging, so you have to configure it in your configuration files:
config :lager,
handlers: [level: :critical]