subscriber.go 971 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/seaweedfs/seaweedfs/weed/mq/client/sub_client"
  5. "google.golang.org/grpc"
  6. "google.golang.org/grpc/credentials/insecure"
  7. )
  8. func main() {
  9. subscriberConfig := &sub_client.SubscriberConfiguration{
  10. ClientId: "testSubscriber",
  11. GroupId: "test",
  12. GroupInstanceId: "test",
  13. GrpcDialOption: grpc.WithTransportCredentials(insecure.NewCredentials()),
  14. }
  15. contentConfig := &sub_client.ContentConfiguration{
  16. Namespace: "test",
  17. Topic: "test",
  18. Filter: "",
  19. }
  20. subscriber := sub_client.NewTopicSubscriber(subscriberConfig, contentConfig)
  21. if err := subscriber.Connect("localhost:17777"); err != nil {
  22. fmt.Println(err)
  23. return
  24. }
  25. subscriber.SetEachMessageFunc(func(key, value []byte) bool {
  26. println(string(key), "=>", string(value))
  27. return true
  28. })
  29. subscriber.SetCompletionFunc(func() {
  30. println("done subscribing")
  31. })
  32. if err := subscriber.Subscribe(); err != nil {
  33. fmt.Println(err)
  34. }
  35. }