Sending and Receiving Ordered Messages
In DMS for RocketMQ, ordered messages are consumed in the exact order that they are produced.
Ordered messages are ordered globally or on the partition level.
- Globally ordered messages: There is only one queue in a specific topic. All messages in the queue will be published and subscribed to in the first in, first out (FIFO) order.
- Partition-level ordered message: Messages within a queue in a specific topic are published and subscribed to in the FIFO order. A producer specifies a message group for each message. Messages in the same group are allocated to the same queue.
The only difference between globally ordered messages and partition-level ordered messages is the number of queues. The code is the same.
Before sending and receiving ordered messages, collect RocketMQ connection information by referring to Collecting Connection Information.
- To receive and send orderly messages, ensure the topic message type is Orderly before connecting a client to a RocketMQ instance of version 5.x.
- When the gRPC protocol is used to connect to a RocketMQ instance, whether a consumer consumes messages in sequence depends not on the consumption code, but on whether ordered consumption is enabled for the consumer group. The code for ordered consumption is the same as that for normal consumption.
Preparing the Environment
- Run the following command to check whether Go has been installed:
go version
If the following information is displayed, Go has been installed:
go version go1.16.5 linux/amd64
If Go is not installed, download and install it.
- Add the following code to go.mod to add the dependency:
module rocketmq-example-go go 1.13 require ( github.com/apache/rocketmq-clients/golang/v5 )
Sending Ordered Messages
The following code is an example. Replace the information in bold with the actual values.
package main import ( "context" "fmt" "log" "os" "strconv" "time" "github.com/apache/rocketmq-clients/golang" "github.com/apache/rocketmq-clients/golang/credentials" ) const ( Topic = "topic01" Endpoint = "192.168.xx.xx:8080" AccessKey = os.Getenv("ROCKETMQ_AK") //Hard-coded or plaintext username and key are risky. You are advised to store them in ciphertext in a configuration file or an environment variable. SecretKey = os.Getenv("ROCKETMQ_SK") ) func main() { os.Setenv("mq.consoleAppender.enabled", "true") golang.ResetLogger() producer, err := golang.NewProducer(&golang.Config{ Endpoint: Endpoint, Credentials: &credentials.SessionCredentials{ AccessKey: AccessKey, AccessSecret: SecretKey, }, }, golang.WithTopics(Topic), ) if err != nil { log.Fatal(err) } err = producer.Start() if err != nil { log.Fatal(err) } defer producer.GracefulStop() for i := 0; i < 10; i++ { msg := &golang.Message{ Topic: Topic, Body: []byte("this is a message : " + strconv.Itoa(i)), } // Set the message key and tag. msg.SetKeys("a", "b") msg.SetTag("ab") msg.SetMessageGroup("yourMessageGroup0") resp, err := producer.Send(context.TODO(), msg) if err != nil { log.Fatal(err) } for i := 0; i < len(resp); i++ { fmt.Printf("%#v\n", resp[i]) } time.Sleep(time.Second * 1) } }
The parameters in the example code are described as follows. For details about how to obtain the parameter values, see Collecting Connection Information.
- Topic: Enter a topic name.
- Endpoint: Enter the gRPC address or gRPC public address.
- AccessKey: Enter the username if ACL was enabled during instance creation.
- SecretKey: Enter the user key if ACL was enabled during instance creation.
- SetKeys: Enter the message key.
- SetTag: Enter the message tag.
Subscribing to Ordered Messages
The code for subscribing to ordered messages is the same as that for subscribing to normal messages.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot