-
-
Save yosiat/97460fb4dee41c5d0d0f6873a31c77d9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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