12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- package sarama
- import "sync"
- type SimpleProducer struct {
- producer *Producer
- topic string
- m sync.Mutex
- }
- func NewSimpleProducer(client *Client, topic string, partitioner PartitionerConstructor) (*SimpleProducer, error) {
- if topic == "" {
- return nil, ConfigurationError("Empty topic")
- }
- config := NewProducerConfig()
- config.AckSuccesses = true
- if partitioner != nil {
- config.Partitioner = partitioner
- }
- prod, err := NewProducer(client, config)
- if err != nil {
- return nil, err
- }
- return &SimpleProducer{producer: prod, topic: topic}, nil
- }
- func (sp *SimpleProducer) SendMessage(key, value Encoder) error {
- sp.m.Lock()
- defer sp.m.Unlock()
- sp.producer.Input() <- &MessageToSend{Topic: sp.topic, Key: key, Value: value}
-
- select {
- case err := <-sp.producer.Errors():
- return err.Err
- case <-sp.producer.Successes():
- return nil
- }
- }
- func (sp *SimpleProducer) Close() error {
- return sp.producer.Close()
- }
|