View Source ExRocketmq.Consumer (lib_oss v0.1.0)

RocketMQ consumer

This module provides functionality for consuming messages from RocketMQ.

It defines a State struct that holds the state of the consumer and its configuration options.

Example

  1. Start a namesrvrs:

    {:ok, namesrvs_pid} = Namesrvs.start_link(
     remotes: [
      [transport: Transport.Tcp.new(host: "test.rocket-mq.net", port: 31_120)]
    ],
    opts: [
      name: :namesrvs
    ]
    )
  2. Define a processor:

    defmodule MyProcessor do
    
     alias ExRocketmq.{
       Consumer.Processor,
       Models.MessageExt,
       Typespecs
     }
    
     @behaviour Processor
    
     defstruct []
    
     @type t :: %__MODULE__{}
    
     @spec new() :: t()
     def new, do: %__MODULE__{}
    
     @spec process(t(), Typespecs.topic(), [MessageExt.t()]) ::
             Processor.consume_result() | {:error, term()}
     def process(_, topic, msgs) do
       msgs
       |> Enum.each(fn msg ->
         IO.inspect(msg)
       end)
    
       :success
     end
    end
  3. Start consumer process

    Consumer.start_link(
      consumer_group: "GID_POETRY",
      namesrvs: :namesrvs,
      processor: MyProcessor.new(),
      subscriptions: %{"POETRY" => MsgSelector.new(:tag, "*")},
      trace_enable: true,
      opts: [
        name: :consumer
      ]}
    )

Summary

Functions

Returns a specification to start this module under a supervisor.

Callback implementation for GenServer.init/1.

Starts a RocketMQ consumer process.

stop a RocketMQ consumer process.

Subscribes to a RocketMQ topic with an optional message selector.

Unsubscribes from a RocketMQ topic.

Types

Link to this type

consumer_opts_schema_t()

View Source
@type consumer_opts_schema_t() :: [
  consumer_group: binary(),
  namespace: binary(),
  namesrvs: pid() | atom(),
  model: term(),
  subscriptions: %{optional(binary()) => term()},
  consume_orderly: boolean(),
  consume_from_where: term(),
  balance_strategy: term(),
  processor: term(),
  consume_timestamp: non_neg_integer(),
  post_subscription_when_pull: boolean(),
  pull_batch_size: non_neg_integer(),
  consume_batch_size: non_neg_integer(),
  max_reconsume_times: non_neg_integer(),
  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.

Starts a RocketMQ consumer process.

Options

  • :consumer_group (String.t/0) - Required. Consumer group name

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

  • :namesrvs - Required. The namesrvs process

  • :model - Consumer model, :cluster or :broadcast The default value is :cluster.

  • :subscriptions (map of String.t/0 keys and term/0 values) - The subscriptions of the consumer, such as %{"SomeTopic" => %ExRocketmq.Models.MsgSelector{}} The default value is %{}.

  • :consume_orderly (boolean/0) - Whether to consume orderly The default value is false.

  • :consume_from_where - Where to start consuming, :last_offset or :first_offset The default value is :last_offset.

  • :balance_strategy (term/0) - The implemention of ExRocketmq.Consumer.BalanceStrategy The default value is %ExRocketmq.Consumer.BalanceStrategy.Average{}.

  • :processor (term/0) - Required. Message consume processor, must implement ExRocketmq.Consumer.Processor

  • :consume_timestamp (non_neg_integer/0) - The timestamp(ms) to consume from, only used when consume_from_where is :timestamp The default value is 0.

  • :post_subscription_when_pull (boolean/0) - Whether to post subscription when pull message The default value is false.

  • :pull_batch_size (non_neg_integer/0) - The batch size to pull message The default value is 32.

  • :consume_batch_size (non_neg_integer/0) - The batch size to consume message The default value is 16.

  • :max_reconsume_times (non_neg_integer/0) - The max times to reconsume message The default value is 16.

  • :trace_enable (boolean/0) - Whether to enable trace collection The default value is false.

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

Examples

iex> ExRocketmq.Consumer.start_link(%{group_name: "my_group", namesrvs: "localhost:9876"})
{:ok, pid}
@spec stop(pid() | atom()) :: :ok

stop a RocketMQ consumer process.

Link to this function

subscribe(consumer, topic, msg_selector \\ %MsgSelector{})

View Source
@spec subscribe(
  pid() | atom(),
  ExRocketmq.Typespecs.topic(),
  ExRocketmq.Models.MsgSelector.t()
) :: :ok

Subscribes to a RocketMQ topic with an optional message selector.

After subscribing, the consumer will start to pull messages from the topic. And if the broker address changes, consumer update the connection to the broker.

Examples

iex> ExRocketmq.Consumer.subscribe(consumer, "my_topic")
:ok
Link to this function

unsubscribe(consumer, topic)

View Source
@spec unsubscribe(pid() | atom(), ExRocketmq.Typespecs.topic()) :: :ok

Unsubscribes from a RocketMQ topic.

Examples

iex> ExRocketmq.Consumer.unsubscribe(consumer, "my_topic")
:ok