View Source WhiteRabbit.Producer behaviour (White Rabbit v0.2.0)

WhiteRabbit.Producer behaviour module that publishes messages using its callbacks.

using-the-module

Using The Module

using-the-use-macro

Using the use macro

defmodule AppOne.Producer.Json do
  use WhiteRabbit.Producer

  def test_publish do
    data = %{hello: "there", general: :kenobi}

    payload = Jason.encode!(data)

    # Use default publish/5 function
    publish(:appone_connection, "json_test_exchange", "test_json", payload,
      content_type: "aplication/json",
      persistent: true
    )
  end

end

overide-default-callback

Overide default callback

# or override it with custom callback
def publish(conn, exchange, queue, payload, options) do
  # custom logic

  # Send AMQP.Basic.publish/5 message

  # more custom logic
end

or-just-use-the-publish-5-function-directly

Or just use the publish/5 function directly

Default automatically emits :telemetry events with the names:

  • [:white_rabbit, :publish, :start]

  • [:white_rabbit, :publish, :stop]

iex> WhiteRabbit.Producer.publish(:appone_conn, "test_exchange", "test_route", "hello there", persistent: true)
:ok

Link to this section Summary

Types

Return on publish of message

Publish Options:

:mandatory - If set, returns an error if the broker can't route the message to a queue (default false)

Functions

Tries to publish message with a channel from a pool if a channel is found.

Link to this section Types

@type channel() :: AMQP.Channel.t()
@type on_publish() :: :ok

Return on publish of message

@type publish_options() :: Keyword.t()

publish-options

Publish Options:

:mandatory - If set, returns an error if the broker can't route the message to a queue (default false)

:immediate - If set, returns an error if the broker can't deliver the message to a consumer immediately (default false)

:content_type - MIME Content type

:content_encoding - MIME Content encoding

:headers - Message headers of type t:AMQP.arguments/0. Can be used with headers Exchanges

:persistent - If set, uses persistent delivery mode. Messages marked as persistent that are delivered to durable queues will be logged to disk

:correlation_id - application correlation identifier

:priority - message priority, ranging from 0 to 9

:reply_to - name of the reply queue

:expiration - how long the message is valid (in milliseconds)

:message_id - message identifier

:type - message type as a string

:user_id - creating user ID. RabbitMQ will validate this against the active connection user

:app_id - publishing application ID

Link to this section Callbacks

Link to this callback

publish(conn_pool, exchange, routing_key, message, options)

View Source
@callback publish(
  conn_pool :: atom(),
  exchange :: String.t(),
  routing_key :: String.t(),
  message :: any(),
  options :: publish_options()
) :: on_publish()

Link to this section Functions

Link to this function

publish(conn_tuple, exchange, routing_key, message, options)

View Source
@spec publish(
  conn_tuple :: tuple(),
  exchange :: String.t(),
  routing_key :: String.t(),
  message :: any(),
  options :: publish_options()
) :: on_publish()
@spec publish(
  channel :: AMQP.Channel.t(),
  exchange :: String.t(),
  routing_key :: String.t(),
  message :: term(),
  options :: Keyword.t()
) :: :ok

Tries to publish message with a channel from a pool if a channel is found.

Returns :ok if successful

Options: publish_options

attaches-some-telemetry-events-as-well

Attaches some :telemetry events as well:

  • [:white_rabbit, :publish, :start]

  • measurements:

    • time: :naive unix timestamp
    • count: 1
  • [:white_rabbit, :publish, :stop]

  • measurements:

    • duration: :naive unix timestamp
    • count: 1
  • metadata: %{ connpool: connpool, exchange: exchange, routing_key: routing_key, module: __MODULE }