RMQ v0.1.0-beta.1 RMQ.RPC behaviour View Source

RPC via RabbitMQ.

In short, it's a GenServer which Implements a publisher and a consumer in one place.

A module which will implement this behaviour will be able to publish messages via call/4 and wait for a response.

You can read more about how this works in the tutorial.

Configuration

  • :connection - the connection module which implements RMQ.Connection behaviour;
  • :exchange - the name of the exchange to which RPC consuming queue is bound. Please make sure the exchange exist. Defaults to "".
  • :timeout - default timeout for call/4. Will be passed directly to the underlying call of GenServer.call/3 Defaults to 5000.
  • :consumer_tag - consumer tag for the callback queue. Defaults to a current module name;
  • :restart_delay - Defaults to 5000;
  • :publishing_options - any valid options for AMQP.Basic.publish/5 except :reply_to, :correlation_id, :content_type - these will be set automatically and cannot be overridden. Defaults to [].

Example usage with RMQ.Consumer

Application 1:

defmodule MyOtherApp.Consumer do
  use RMQ.Consumer,
    connection: MyOtherApp.RabbitConnection,
    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!()

    AMQP.Basic.publish(chan, meta.exchange, meta.reply_to, response,
      correlation_id: meta.correlation_id
    )

    AMQP.Basic.ack(chan, meta.delivery_tag)
  end
end

Application 2:

defmodule MyApp.RemoteResource do
  use RMQ.RPC,
    connection: MyApp.RabbitConnection,
    publishing_options: [app_id: "MyApp"]

  def find_by_id(id) do
    call("remote-resource-finder", %{id: id})
  end
end

Link to this section Summary

Callbacks

Performs remote procedure call.

Starts a GenServer process linked to the current process.

Link to this section Callbacks

Link to this callback

call(queue, payload, options, timeout)

View Source
call(queue :: String.t(), payload :: any(), options :: Keyword.t(), timeout()) ::
  any()

Performs remote procedure call.

  • options - same as publishing_options but have precedence over them. Can be omitted.
  • timeout - same as timeout in configuration. Can be omitted.
Link to this callback

start_link(options)

View Source
start_link(options :: [GenServer.option()]) :: GenServer.on_start()

Starts a GenServer process linked to the current process.