go get
is used as example. Run the following command:go get github.com/apache/rocketmq-clients/golang/v5
package mainimport ("context""fmt""log""os""strconv""time"rmq_client "github.com/apache/rocketmq-clients/golang/v5""github.com/apache/rocketmq-clients/golang/v5/credentials")const (Topic = "xxxxxx"// Set Endpoint to the access address provided by Tencent CloudEndpoint = "xxxxxx"// Add the configured ak to AccessKeyAccessKey = "xxxxxx"// Add the configured sk to SecretKeySecretKey = "xxxxxx")func main() {os.Setenv("mq.consoleAppender.enabled", "true")rmq_client.ResetLogger()// In most case, you don't need to create many producers, singleton pattern is more recommended.producer, err := rmq_client.NewProducer(&rmq_client.Config{Endpoint: Endpoint,Credentials: &credentials.SessionCredentials{AccessKey: AccessKey,AccessSecret: SecretKey,},},rmq_client.WithTopics(Topic),)if err != nil {log.Fatal(err)}// start producererr = producer.Start()if err != nil {log.Fatal(err)}// graceful stop producerdefer producer.GracefulStop()for i := 0; i < 10; i++ {// new a messagemsg := &rmq_client.Message{Topic,Body: []byte("this is a message : " + strconv.Itoa(i)),}// set keys and tagmsg.SetKeys("a", "b")msg.SetTag("ab")// send message in syncresp, 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])}// wait a momenttime.Sleep(time.Second * 1)}}
package mainimport ("context""fmt""log""os""time"rmq_client "github.com/apache/rocketmq-clients/golang/v5""github.com/apache/rocketmq-clients/golang/v5/credentials")const (Topic = "xxxxxx"ConsumerGroup = "xxxxxx"// Set Endpoint to the access address provided by Tencent CloudEndpoint = "xxxxxx"// Add the configured ak to AccessKeyAccessKey = "xxxxxx"// Add the configured sk to SecretKeySecretKey = "xxxxxx")var (// maximum waiting time for receive funcawaitDuration = time.Second * 5// maximum number of messages received at one timemaxMessageNum int32 = 16// invisibleDuration should > 20sinvisibleDuration = time.Second * 20// receive messages in a loop)func main() {// log to consoleos.Setenv("mq.consoleAppender.enabled", "true")rmq_client.ResetLogger()// In most case, you don't need to create many consumers, singleton pattern is more recommended.simpleConsumer, err := rmq_client.NewSimpleConsumer(&rmq_client.Config{Endpoint: Endpoint,ConsumerGroup: ConsumerGroup,Credentials: &credentials.SessionCredentials{AccessKey: AccessKey,AccessSecret: SecretKey,},},rmq_client.WithAwaitDuration(awaitDuration),rmq_client.WithSubscriptionExpressions(map[string]*rmq_client.FilterExpression{Topic: rmq_client.SUB_ALL,}),)if err != nil {log.Fatal(err)}// start simpleConsumererr = simpleConsumer.Start()if err != nil {log.Fatal(err)}// graceful stop simpleConsumerdefer simpleConsumer.GracefulStop()for {fmt.Println("start receive message")mvs, err := simpleConsumer.Receive(context.TODO(), maxMessageNum, invisibleDuration)if err != nil {fmt.Println(err)}// ack messagefor _, mv := range mvs {simpleConsumer.Ack(context.TODO(), mv)fmt.Println(mv)}fmt.Println("wait a moment")fmt.Println()time.Sleep(time.Second * 1)}}
Was this page helpful?