-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproducer.go
39 lines (30 loc) · 945 Bytes
/
producer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package main
import (
"fmt"
"github.com/confluentinc/confluent-kafka-go/kafka"
)
type ProducerImpl struct {
producer *kafka.Producer
topic string
}
func NewProducer(broker, topic string) (*ProducerImpl, error) {
p, err := kafka.NewProducer(&kafka.ConfigMap{"bootstrap.servers": broker})
if err != nil {
return nil, fmt.Errorf("failed to create producer: %s", err)
}
if topic == "" {
return nil, fmt.Errorf("you need to specify an valid topic")
}
return &ProducerImpl{producer: p, topic: topic}, nil
}
func (p *ProducerImpl) Send(key string, val []byte, headers ...kafka.Header) (chan kafka.Event, error) {
deliveryChan := make(chan kafka.Event)
if err := p.producer.Produce(&kafka.Message{
TopicPartition: kafka.TopicPartition{Topic: &p.topic, Partition: kafka.PartitionAny},
Value: []byte(val),
Headers: headers,
}, deliveryChan); err != nil {
return nil, err
}
return deliveryChan, nil
}