Skip to content

Instantly share code, notes, and snippets.

@yosiat
Created March 23, 2020 13:16
Show Gist options
  • Save yosiat/97460fb4dee41c5d0d0f6873a31c77d9 to your computer and use it in GitHub Desktop.
Save yosiat/97460fb4dee41c5d0d0f6873a31c77d9 to your computer and use it in GitHub Desktop.
package main
import (
"context"
"fmt"
"time"
"github.com/apache/pulsar-client-go/pulsar"
)
func main() {
client, err := pulsar.NewClient(pulsar.ClientOptions{
URL: "pulsar://localhost:6650",
})
defer client.Close()
producer, err := client.CreateProducer(pulsar.ProducerOptions{
Topic: "my-topic",
})
defer producer.Close()
ticker := time.NewTicker(1 * time.Second)
done := make(chan bool)
go func() {
for {
select {
case <-done:
return
case t := <-ticker.C:
fmt.Println("got tick, publishing a message", t)
_, err = producer.Send(context.Background(), &pulsar.ProducerMessage{
Payload: []byte("hello"),
})
if err != nil {
fmt.Println("Failed to publish message", err)
}
}
}
}()
fmt.Println("Publishing messages ~")
fmt.Println("After 10 minutes we stop")
time.Sleep(10 * time.Minute)
ticker.Stop()
done <- true
fmt.Println("stopping")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment