View Source ExRocketmq.Producer (lib_oss v0.1.0)

This module provides a GenServer implementation of a RocketMQ producer.

Example

  • First, write a producer task:

    defmodule DemoProducer do
    
    use Task
    
    alias ExRocketmq.Producer
    alias ExRocketmq.Models.Message
    
    @topic "POETRY"
    @msgs "风劲角弓鸣,将军猎渭城。
    草枯鹰眼疾,雪尽马蹄轻。
    忽过新丰市,还归细柳营。
    回看射雕处,千里暮云平。"
    
    def start_link(opts) do
      Task.start_link(__MODULE__, :run, [opts])
    end
    
    defp get_msg() do
      @msgs
      |> String.split("
    ")
    end
    
    def run(opts) do
      get_msg()
      |> Enum.each(fn msgs ->
        to_emit =
          msgs
          |> Enum.map(fn msg ->
            %Message{topic: @topic, body: msg}
          end)
    
        Producer.send_sync(:producer, to_emit)
      end)
    
      Process.sleep(5000)
      run(opts)
    end
    end
  • Second, add namesrvs and producer and your msg produce task to supervisor tree:

    children = [
    {Namesrvs,
     remotes: [
       [transport: Transport.Tcp.new(host: "test.rocket-mq.net", port: 31_120)]
     ],
     opts: [
       name: :namesrvs
     ]},
    {
      Producer,
      group_name: "GID_POETRY",
      namesrvs: :namesrvs,
      trace_enable: true,
      opts: [
        name: :producer
      ]
    },
    {
      DemoProducer,
      []
    }
    ]
    
    Supervisor.start_link(children, strategy: :one_for_one)

This module support both normal producer and transactional producer, if you want to use transactional producer, you must implement ExRocketmq.Producer.Transaction behaviour, see this example for more details.

Summary

Functions

Returns a specification to start this module under a supervisor.

Callback implementation for GenServer.init/1.

send msgs to broker, and don't wait broker response

send msgs to broker, and wait broker response

Sends a message to the broker as part of a transaction and waits for the broker's response.

start producer process

Types

Link to this type

producer_opts_schema_t()

View Source
@type producer_opts_schema_t() :: [
  group_name: binary(),
  namesrvs: pid() | atom(),
  namespace: binary(),
  selector: term(),
  compress: keyword(),
  transaction_listener: term(),
  trace_enable: boolean(),
  opts: keyword()
]

Functions

Returns a specification to start this module under a supervisor.

See Supervisor.

Link to this function

handle_continue(atom, state)

View Source

Callback implementation for GenServer.handle_continue/2.

Callback implementation for GenServer.init/1.

Link to this function

send_oneway(producer, msgs)

View Source
@spec send_oneway(pid() | atom(), [ExRocketmq.Models.Message.t()]) :: :ok

send msgs to broker, and don't wait broker response

Examples

iex> Producer.send_oneway(producer, [%Message{topic: topic, body: "Hello from elixir"}])
:ok
Link to this function

send_sync(producer, msgs)

View Source
@spec send_sync(pid() | atom(), [ExRocketmq.Models.Message.t()]) ::
  {:ok, ExRocketmq.Models.SendMsg.Response.t()} | ExRocketmq.Typespecs.error_t()

send msgs to broker, and wait broker response

Examples

iex> Producer.send_sync(producer, [%Message{topic: topic, body: "Hello from elixir"}])
{:ok,
 %ExRocketmq.Models.SendMsg.Response{
   status: 0,
   msg_id: "0A5804A1000051AF000000450648EF0E",
   queue: %ExRocketmq.Models.MessageQueue{
     topic: "SOME_TOPIC",
     broker_name: "broker-0",
     queue_id: 0
   },
   queue_offset: 689717,
   transaction_id: "",
   offset_msg_id: "0A5804A1000051AF000000450648EF0E",
   region_id: "DefaultRegion",
   trace_on: true
 }}
Link to this function

send_transaction_msg(producer, msgs)

View Source
@spec send_transaction_msg(pid() | atom(), ExRocketmq.Models.Message.t()) ::
  {:ok, ExRocketmq.Models.SendMsg.Response.t(),
   ExRocketmq.Typespecs.transaction_state()}
  | ExRocketmq.Typespecs.error_t()

Sends a message to the broker as part of a transaction and waits for the broker's response.

Examples

iex> Producer.send_transaction_msg(producer, %Message{topic: topic, body: "Hello from elixir"})
{:ok, response, transaction_state}

start producer process

Options

  • :group_name (String.t/0) - Required. The group name of the producer

  • :namesrvs - Required. The namesrvs process

  • :namespace (String.t/0) - The namespace of the producer The default value is "".

  • :selector (term/0) - The mq selector of the producer The default value is %ExRocketmq.Producer.MqSelector.Random{}.

  • :compress (keyword/0) - The compress opts of the producer

    • :compressor (atom/0) - The compress implemention of the producer The default value is ExRocketmq.Compress.Zlib.

    • :compress_over (integer/0) - Do compress when message bytesize over this value The default value is 4096.

    • :compress_level - The compress level of the compressor The default value is :best_compression.

  • :transaction_listener (term/0) - The transaction listener of the producer, must implement ExRocketmq.Producer.Transaction, see ExRocketmq.Producer.Transaction for more details

  • :trace_enable (boolean/0) - The trace enable of the producer The default value is false.

  • :opts (keyword/0) - The opts of the producer's GenServer The default value is [].

Examples

iex> ExRocketmq.Producer.start_link(
  group_name: "GID_POETRY",
  namesrvs: :namesrvs,
  trace_enable: true,
  opts: [
    name: :producer
  ]
)
@spec stop(pid() | atom()) :: :ok