Created
March 5, 2019 09:53
-
-
Save mufti1/cf7bdf2c7bd3f3910855448fef69ffbe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package producer | |
import ( | |
"github.com/Shopify/sarama" | |
"github.com/sirupsen/logrus" | |
) | |
// KafkaProducer hold kafka producer session | |
type KafkaProducer struct { | |
Producer sarama.SyncProducer | |
} | |
// SendMessage function to send message into kafka | |
func (p *KafkaProducer) SendMessage(topic, msg string) error { | |
kafkaMsg := &sarama.ProducerMessage{ | |
Topic: topic, | |
Value: sarama.StringEncoder(msg), | |
} | |
partition, offset, err := p.Producer.SendMessage(kafkaMsg) | |
if err != nil { | |
logrus.Errorf("Send message error: %v", err) | |
return err | |
} | |
logrus.Infof("Send message success, Topic %v, Partition %v, Offset %d", topic, partition, offset) | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment