golang/example/consumer/simple_consumer/main.go (64 lines of code) (raw):

/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package main import ( "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" Endpoint = "xxxxxx" AccessKey = "xxxxxx" SecretKey = "xxxxxx" ) var ( // maximum waiting time for receive func awaitDuration = time.Second * 5 // maximum number of messages received at one time maxMessageNum int32 = 16 // invisibleDuration should > 20s invisibleDuration = time.Second * 20 // receive messages in a loop ) func main() { // log to console os.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 simpleConsumer err = simpleConsumer.Start() if err != nil { log.Fatal(err) } // graceful stop simpleConsumer defer simpleConsumer.GracefulStop() go func() { for { fmt.Println("start receive message") mvs, err := simpleConsumer.Receive(context.TODO(), maxMessageNum, invisibleDuration) if err != nil { fmt.Println(err) } // ack message for _, mv := range mvs { simpleConsumer.Ack(context.TODO(), mv) fmt.Println(mv) } fmt.Println("wait a moment") fmt.Println() time.Sleep(time.Second * 3) } }() // run for a while time.Sleep(time.Minute) }