2023-08-28 08:50:59 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-12-12 04:05:54 +08:00
|
|
|
"flag"
|
2023-08-28 08:50:59 +08:00
|
|
|
"fmt"
|
2024-01-08 16:03:08 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
2023-09-01 15:36:51 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/mq/client/sub_client"
|
2024-02-06 15:14:25 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/mq/topic"
|
2024-01-16 12:42:46 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
2023-09-05 12:43:50 +08:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/credentials/insecure"
|
2023-12-12 04:05:54 +08:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
namespace = flag.String("ns", "test", "namespace")
|
2024-02-06 15:14:25 +08:00
|
|
|
t = flag.String("topic", "test", "topic")
|
2023-12-12 04:05:54 +08:00
|
|
|
seedBrokers = flag.String("brokers", "localhost:17777", "seed brokers")
|
2024-01-16 12:42:46 +08:00
|
|
|
|
|
|
|
clientId = flag.Uint("client_id", uint(util.RandomInt32()), "client id")
|
2023-08-28 08:50:59 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2023-12-12 04:05:54 +08:00
|
|
|
flag.Parse()
|
2023-08-28 08:50:59 +08:00
|
|
|
|
2023-09-05 12:43:50 +08:00
|
|
|
subscriberConfig := &sub_client.SubscriberConfiguration{
|
2024-01-16 12:42:46 +08:00
|
|
|
ClientId: fmt.Sprintf("client-%d", *clientId),
|
2023-12-29 03:56:37 +08:00
|
|
|
ConsumerGroup: "test",
|
2024-01-16 12:42:46 +08:00
|
|
|
ConsumerGroupInstanceId: fmt.Sprintf("client-%d", *clientId),
|
2023-12-29 03:56:37 +08:00
|
|
|
GrpcDialOption: grpc.WithTransportCredentials(insecure.NewCredentials()),
|
2023-09-05 12:43:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
contentConfig := &sub_client.ContentConfiguration{
|
2024-02-06 15:14:25 +08:00
|
|
|
Topic: topic.NewTopic(*namespace, *t),
|
2023-09-05 12:43:50 +08:00
|
|
|
Filter: "",
|
2024-01-04 07:57:36 +08:00
|
|
|
StartTime: time.Unix(1, 1),
|
2023-09-05 12:43:50 +08:00
|
|
|
}
|
|
|
|
|
2023-12-29 03:56:37 +08:00
|
|
|
processorConfig := sub_client.ProcessorConfiguration{
|
2024-01-16 12:42:46 +08:00
|
|
|
ConcurrentPartitionLimit: 3,
|
2023-12-29 03:56:37 +08:00
|
|
|
}
|
|
|
|
|
2023-12-12 04:05:54 +08:00
|
|
|
brokers := strings.Split(*seedBrokers, ",")
|
2023-12-29 03:56:37 +08:00
|
|
|
subscriber := sub_client.NewTopicSubscriber(brokers, subscriberConfig, contentConfig, processorConfig)
|
2023-08-28 08:50:59 +08:00
|
|
|
|
2024-01-08 16:03:08 +08:00
|
|
|
counter := 0
|
2024-01-01 05:25:33 +08:00
|
|
|
subscriber.SetEachMessageFunc(func(key, value []byte) (bool, error) {
|
2024-01-08 16:03:08 +08:00
|
|
|
counter++
|
|
|
|
println(string(key), "=>", string(value), counter)
|
2024-01-01 05:25:33 +08:00
|
|
|
return true, nil
|
2023-09-05 12:43:50 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
subscriber.SetCompletionFunc(func() {
|
2024-04-14 22:42:40 +08:00
|
|
|
glog.V(0).Infof("done received %d messages", counter)
|
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
|
|
|
}
|