seaweedfs/weed/mq/client/cmd/weed_sub/subscriber.go

45 lines
971 B
Go
Raw Normal View History

2023-08-28 08:50:59 +08:00
package main
import (
"fmt"
2023-09-01 15:36:51 +08:00
"github.com/seaweedfs/seaweedfs/weed/mq/client/sub_client"
2023-09-05 12:43:50 +08:00
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
2023-08-28 08:50:59 +08:00
)
func main() {
2023-09-05 12:43:50 +08:00
subscriberConfig := &sub_client.SubscriberConfiguration{
ClientId: "testSubscriber",
GroupId: "test",
GroupInstanceId: "test",
GrpcDialOption: grpc.WithTransportCredentials(insecure.NewCredentials()),
}
contentConfig := &sub_client.ContentConfiguration{
Namespace: "test",
Topic: "test",
Filter: "",
}
subscriber := sub_client.NewTopicSubscriber(subscriberConfig, contentConfig)
2023-09-01 15:36:51 +08:00
if err := subscriber.Connect("localhost:17777"); err != nil {
fmt.Println(err)
return
}
2023-08-28 08:50:59 +08:00
2023-09-05 12:43:50 +08:00
subscriber.SetEachMessageFunc(func(key, value []byte) bool {
2023-09-01 15:36:51 +08:00
println(string(key), "=>", string(value))
return true
2023-09-05 12:43:50 +08:00
})
subscriber.SetCompletionFunc(func() {
2023-09-01 15:36:51 +08:00
println("done subscribing")
2023-09-05 12:43:50 +08:00
})
if err := subscriber.Subscribe(); err != nil {
2023-08-28 08:50:59 +08:00
fmt.Println(err)
}
2023-09-01 15:36:51 +08:00
2023-08-28 08:50:59 +08:00
}