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
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 ] )
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
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.handle_continue/2
.
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
@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
.
Callback implementation for GenServer.handle_continue/2
.
Callback implementation for GenServer.init/1
.
@spec start_link(consumer_opts_schema_t()) :: ExRocketmq.Typespecs.on_start()
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 ofString.t/0
keys andterm/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 isfalse
.: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 is0
.:post_subscription_when_pull
(boolean/0
) - Whether to post subscription when pull message The default value isfalse
.:pull_batch_size
(non_neg_integer/0
) - The batch size to pull message The default value is32
.:consume_batch_size
(non_neg_integer/0
) - The batch size to consume message The default value is16
.:max_reconsume_times
(non_neg_integer/0
) - The max times to reconsume message The default value is16
.:trace_enable
(boolean/0
) - Whether to enable trace collection The default value isfalse
.: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}
stop a RocketMQ consumer process.
@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
@spec unsubscribe(pid() | atom(), ExRocketmq.Typespecs.topic()) :: :ok
Unsubscribes from a RocketMQ topic.
Examples
iex> ExRocketmq.Consumer.unsubscribe(consumer, "my_topic")
:ok