wolfstudy commented on pull request #366:
URL: https://github.com/apache/pulsar-client-go/pull/366#issuecomment-693119010
```
package main
import (
"context"
"fmt"
"github.com/apache/pulsar-client-go/pulsar"
"log"
"math/rand"
"time"
)
func main() {
// create client
client, err := pulsar.NewClient(pulsar.ClientOptions{
URL: "pulsar://localhost:6650",
MaxConnectionsPerBroker: 1,
})
if err != nil {
log.Fatal(err)
}
defer client.Close()
topic := randomName("topic-name")
// create consumer
consumer, err := client.Subscribe(pulsar.ConsumerOptions{
Name: randomName("consumer-name"),
SubscriptionName: randomName("subscription-name"),
Topic: topic,
Type: pulsar.KeyShared,
SubscriptionInitialPosition:
pulsar.SubscriptionPositionEarliest,
})
if err != nil {
log.Fatal(err)
}
defer consumer.Close()
// create producer
producer, err := client.CreateProducer(pulsar.ProducerOptions{
Topic: topic,
Name: randomName("producer-name"),
})
if err != nil {
log.Fatal(err)
}
defer producer.Close()
// Publishing a lot of messages to target topic
countMsg := 10000
log.Printf("Publishing %d messages", countMsg)
// publish messages
for i := 0; i < countMsg; i++ {
_, err := producer.Send(context.Background(),
&pulsar.ProducerMessage{
Payload: []byte(fmt.Sprintf("payload-%d", i)),
Key: fmt.Sprintf("msg-key-%d", i),
EventTime: time.Now(),
})
if err != nil {
log.Fatal(err)
}
}
// receive message
log.Println("Starting consumer routine")
for i := 1; i <= countMsg; i++ {
msg, err := consumer.Receive(context.Background())
if err != nil {
log.Fatal(err)
}
log.Printf("Got message %d", i)
consumer.Ack(msg)
}
}
func randomName(prefix string) string {
return fmt.Sprintf("%s-%x", prefix, rand.Int63())
}
```
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]