RMQ v0.4.0 RMQ.RPC behaviour View Source
RPC via RabbitMQ.
In short, it's a GenServer
which implements a publisher and a consumer at once.
You can read more about how this works in the tutorial.
Configuration
:connection
- the connection module which implementsRMQ.Connection
behaviour;:queue
- the queue name to which the module will be subscribed for consuming responses. Also can be a tuple{queue, options}
. See the options forAMQP.Queue.declare/3
. Defaults to""
which means the broker will assign a name to the newly created queue by itself;:exchange
- the exchange name to which:queue
will be bound. Please make sure the exchange exist. Defaults to""
- the default exchange;:consumer_tag
- a consumer tag for:queue
. Defaults to the current module name;:publishing_options
- any valid options forAMQP.Basic.publish/5
exceptreply_to
,correlation_id
- these will be set automatically and cannot be changed. Defaults to[]
;:reconnect_interval
- a reconnect interval in milliseconds. It can be also a function that accepts the current connection attempt as a number and returns a new interval. Defaults to5000
;:filter_parameters
- a list of parameters that may contain sensitive data and have to be filtered out when logging. Defaults to["password"]
.
Example
Application 1:
defmodule MyApp.RemoteResource do
use RMQ.RPC, publishing_options: [app_id: "MyApp"]
def find_by_id(id) do
call("remote-resource-finder", %{id: id})
end
end
Application 2:
defmodule MyOtherApp.Consumer do
use RMQ.Consumer, queue: "remote-resource-finder"
@impl RMQ.Consumer
def consume(chan, payload, meta) do
response =
payload
|> Jason.decode!()
|> Map.fetch!("id")
|> MyOtherApp.Resource.get()
|> Jason.encode!()
reply(chan, meta, response)
ack(chan, meta)
end
end
Link to this section Summary
Functions
Performs a call against the given module.
The default implementation for setup_queue/2
callback.
Link to this section Functions
call(module, queue, payload \\ %{}, options \\ [], timeout \\ 5000)
View SourceSpecs
call( module :: module(), queue :: binary(), payload :: any(), options :: keyword(), timeout :: integer() ) :: {:ok, any()} | {:error, :not_connected} | {:error, :timeout} | {:error, any()}
Performs a call against the given module.
Here options
is a keyword list which will be merged into :publishing_options
during the call
and timeout
is the timeout in milliseconds for the inner GenServer call.
The payload will be encoded by using Jason.encode!/1
.
Specs
setup_queue(chan :: AMQP.Channel.t(), config :: keyword()) :: binary()
The default implementation for setup_queue/2
callback.
Link to this section Callbacks
Specs
config() :: keyword()
A callback for dynamic configuration.
Will be called before setup_queue/2
.
Specs
setup_queue(chan :: AMQP.Channel.t(), config :: keyword()) :: binary()
Does all the job on preparing the queue.
Whenever you need full control over configuring the queue you can implement this callback and
use AMQP
library directly.
See setup_queue/2
for the default implementation.