Skip to content

Commit 5af862f

Browse files
authored
Merge pull request #57 from q2wdqea/feat-mq-kpush
feat(mq): add key field
2 parents 59d5072 + 2c75b16 commit 5af862f

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

example/kq/producer/produce.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,13 @@ func main() {
4141
log.Fatal(err)
4242
}
4343

44-
fmt.Println(string(body))
4544
if err := pusher.Push(context.Background(), string(body)); err != nil {
4645
log.Fatal(err)
4746
}
47+
48+
if err := pusher.KPush(context.Background(), "test", string(body)); err != nil {
49+
log.Fatal(err)
50+
}
4851
}
4952

5053
cmdline.EnterToContinue()

kq/pusher.go

+13
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,19 @@ func (p *Pusher) Name() string {
9797
return p.topic
9898
}
9999

100+
// KPush sends a message to the Kafka topic.
101+
func (p *Pusher) KPush(ctx context.Context, k, v string) error {
102+
msg := kafka.Message{
103+
Key: []byte(k), // current timestamp
104+
Value: []byte(v),
105+
}
106+
if p.executor != nil {
107+
return p.executor.Add(msg, len(v))
108+
} else {
109+
return p.producer.WriteMessages(ctx, msg)
110+
}
111+
}
112+
100113
// Push sends a message to the Kafka topic.
101114
func (p *Pusher) Push(ctx context.Context, v string) error {
102115
msg := kafka.Message{

0 commit comments

Comments
 (0)