RMQ v0.2.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 implements RMQ.Connection behaviour;
  • :queue - the queue name to which the module will be subscribed for consuming responses. Defaults to "" which means the broker will assign a name to a 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 for AMQP.Basic.publish/5 except reply_to, correlation_id, content_type - these will be set automatically and cannot be overridden. 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 to 5000.

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!()

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

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

Link to this section Summary

Functions

The default implementation for setup_queue/2 callback.

Callbacks

A callback for dynamic configuration.

Does all the job on preparing the queue.

Link to this section Functions

Link to this function

call(module, queue, payload \\ %{}, options \\ [], timeout \\ 5000)

View Source

Specs

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.

Link to this callback

setup_queue(chan, config)

View Source

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.