View Source Lepus behaviour (Lepus v0.1.5)
Defines a RabbitMQ client.
Example usage
defmodule MyApp.RabbitMQ do
use Lepus, client: Lepus.BasicClient
end
Add Lepus.BasicClient
to your supervision tree (see the Lepus.BasicClient
documentation).
Use it to publish a message to Rabbit MQ:
MyApp.RabbitMQ.publish_json("my-exchange", "my-routing-key", %{a: 1, b: 2})
Testing
For testing purposes you can use another :client
.
It could be Mox mock.
defmodule MyApp.RabbitMQ do
use Lepus, client: Application.get_env(:my_app, __MODULE__, []) |> Keyword.fetch!(:client)
end
In the prod.exs
or dev.exs
:
config :my_app, MyApp.RabbitMQ, client: Lepus.BasicClient
In the test.exs
:
config :my_app, MyApp.RabbitMQ, client: MyApp.RabbitMQClientMock
In the tests:
test "uses Mox for Lepus" do
MyApp.RabbitMQClientMock
|> expect(:publish_json, fn _, "my-exchange", "my-routing-key", payload, _opts ->
assert %{my_key: "My Value"} = payload
end)
MyApp.RabbitMQ.publish_json("my-exchange", "my-routing-key", %{my_key: "My Value"})
end
Summary
Callbacks
@callback publish( Lepus.Client.exchange(), Lepus.Client.routing_key(), Lepus.Client.binary_payload(), Lepus.Client.opts() ) :: Lepus.Client.response()
Publishes message to RabbitMQ exchange.
Options
amqp_opts
-AMQP.Basic.publish/5
options.rpc
- Defines if the message uses RPC pattern. The default value isfalse
.timeout
- number of milliseconds or:infinity
. Valid for RPC. The default value is5000
.
Returns
Without rpc: true
:
:ok
- in case of success{:error, any}
in case of error
With rpc: true
:
{:ok, any}
- in case of success{:error, :timeout}
- in case of timeout error{:error, any}
- in case of error
@callback publish_json( Lepus.Client.exchange(), Lepus.Client.routing_key(), Lepus.Client.payload(), Lepus.Client.opts() ) :: Lepus.Client.response()
The same as publish/4
but payload
can be any type that is convertable to JSON (map, list, etc.).
Sends JSON string to RabbitMQ exchange.