rocketmq

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 17, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package rocketmq provides a RocketMQ backend for the ling-base/mq broker abstraction. It wraps the official apache/rocketmq-client-go/v2 client, mapping the broker-agnostic mq.Message / mq.Delivery / mq.Producer / mq.Consumer / mq.Broker interfaces onto RocketMQ topics, producer groups and push consumers.

RocketMQ has no concept of exchanges, queues or bindings in the AMQP sense: it routes messages purely by topic (and optionally tag/sharding key). The topology-management methods on mq.Broker (DeclareExchange, DeclareQueue, Bind, Unbind, DeleteQueue, DeleteExchange) are therefore implemented as no-ops that return nil, except that DeclareExchange records the topic name so that producers can publish to it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Broker

type Broker struct {
	// contains filtered or unexported fields
}

Broker implements mq.Broker for RocketMQ.

func New

func New(cfg Config) (*Broker, error)

New creates a new RocketMQ broker. Call Connect() to initialize the underlying RocketMQ client.

func (*Broker) Bind

func (b *Broker) Bind(queue, exchange, routingKey string) error

Bind is a no-op: RocketMQ routing is topic-based.

func (*Broker) Close

func (b *Broker) Close() error

Close shuts down the broker, closing all producers and consumers.

func (*Broker) Connect

func (b *Broker) Connect() error

Connect initializes the RocketMQ client. RocketMQ clients are lazy: producers and consumers are created on demand, so Connect simply marks the broker as ready. A lightweight name-server resolver is validated here so that an invalid configuration is reported early.

func (*Broker) Consumer

func (b *Broker) Consumer(topic string, opts mq.ConsumeOptions) (mq.Consumer, error)

Consumer creates a consumer for the given topic with the given options. The consumer is not started; call Start to begin consuming.

func (*Broker) DeclareExchange

func (b *Broker) DeclareExchange(name string, opts mq.ExchangeOptions) error

DeclareExchange records a topic name. RocketMQ auto-creates topics on first publish (when autoCreateTopicEnable is on at the broker), so this is effectively a no-op that simply remembers the name.

func (*Broker) DeclareQueue

func (b *Broker) DeclareQueue(name string, opts mq.QueueOptions) error

DeclareQueue is a no-op: RocketMQ has no AMQP-style queues.

func (*Broker) DeleteExchange

func (b *Broker) DeleteExchange(name string) error

DeleteExchange removes a recorded topic from the in-memory registry. It does not delete the topic from the broker.

func (*Broker) DeleteQueue

func (b *Broker) DeleteQueue(name string) error

DeleteQueue is a no-op: RocketMQ has no AMQP-style queues.

func (*Broker) IsConnected

func (b *Broker) IsConnected() bool

IsConnected reports whether the broker is currently connected.

func (*Broker) Metrics

func (b *Broker) Metrics() mq.Metrics

Metrics returns a snapshot of broker metrics.

func (*Broker) Producer

func (b *Broker) Producer(topic string, opts mq.PublishOptions) (mq.Producer, error)

Producer creates or returns a cached producer for the given topic.

func (*Broker) Unbind

func (b *Broker) Unbind(queue, exchange, routingKey string) error

Unbind is a no-op: RocketMQ routing is topic-based.

type Config

type Config struct {
	// NameServer is the list of name-server addresses
	// (e.g. []string{"127.0.0.1:9876"}). Required.
	NameServer []string

	// GroupName is the default producer/consumer group name.
	// Default: "DEFAULT_PRODUCER" / "DEFAULT_CONSUMER".
	GroupName string

	// InstanceName is the client instance name. If empty the
	// client uses the process host name.
	InstanceName string

	// Credentials are the optional ACL credentials.
	Credentials Credentials

	// RetryCount is the number of retries on send failure.
	// Default: 2.
	RetryCount int
}

Config configures a RocketMQ broker.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns a Config with sensible defaults for a local name server. The NameServer field is left empty and must be set before calling New.

type Consumer

type Consumer struct {
	// contains filtered or unexported fields
}

Consumer implements mq.Consumer for RocketMQ using a push consumer.

func (*Consumer) IsRunning

func (c *Consumer) IsRunning() bool

IsRunning reports whether the consumer is actively consuming.

func (*Consumer) Start

func (c *Consumer) Start(ctx context.Context) error

Start subscribes to the topic and begins consuming.

func (*Consumer) Stop

func (c *Consumer) Stop(timeout time.Duration) error

Stop gracefully stops consuming.

type Credentials

type Credentials struct {
	// AccessKey is the ACL access key.
	AccessKey string

	// SecretKey is the ACL secret key.
	SecretKey string

	// SecurityToken is an optional temporary security token.
	SecurityToken string
}

Credentials holds the ACL credentials for a RocketMQ cluster.

type Delivery

type Delivery struct {
	// contains filtered or unexported fields
}

Delivery adapts a RocketMQ primitive.MessageExt to the mq.Delivery interface.

RocketMQ push consumers acknowledge messages by returning a ConsumeResult from the subscribe callback rather than via an explicit ACK RPC. Therefore Ack/Nack/Reject here simply record the user's intent in the ackedManually/nacked flags; the consumer loop inspects these flags (together with the handler error) to decide the ConsumeResult. When AutoAck is enabled the framework ignores these flags entirely.

func (*Delivery) Ack

func (d *Delivery) Ack() error

Ack acknowledges successful processing. For RocketMQ this records the intent; the consumer loop returns ConsumeSuccess.

func (*Delivery) Body

func (d *Delivery) Body() []byte

Body returns the raw payload.

func (*Delivery) DeliveryTag

func (d *Delivery) DeliveryTag() uint64

DeliveryTag returns the queue offset as a stable delivery tag.

func (*Delivery) Exchange

func (d *Delivery) Exchange() string

Exchange returns the source topic.

func (*Delivery) Headers

func (d *Delivery) Headers() map[string]any

Headers returns the message headers (RocketMQ properties).

func (*Delivery) Message

func (d *Delivery) Message() *mq.Message

Message returns the underlying mq.Message.

func (*Delivery) Nack

func (d *Delivery) Nack(requeue bool) error

Nack negatively acknowledges the message. If requeue is true the consumer loop returns ConsumeRetryLater so the broker redelivers; otherwise the message is dropped (routed to the retry/dead-letter topic after exceeding the max reconsume count).

func (*Delivery) Redelivered

func (d *Delivery) Redelivered() bool

Redelivered reports whether this message has been delivered before.

func (*Delivery) Reject

func (d *Delivery) Reject(requeue bool) error

Reject rejects the message. Behaves like Nack.

func (*Delivery) RoutingKey

func (d *Delivery) RoutingKey() string

RoutingKey returns the message tag (RocketMQ's closest analogue to a routing key).

type Producer

type Producer struct {
	// contains filtered or unexported fields
}

Producer implements mq.Producer for RocketMQ.

func (*Producer) Close

func (p *Producer) Close() error

Close releases producer resources.

func (*Producer) Publish

func (p *Producer) Publish(ctx context.Context, msg *mq.Message) error

Publish sends a message to the producer's topic.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL