package kafka import ( "context" "github.com/ThreeDotsLabs/watermill" "github.com/ThreeDotsLabs/watermill/message" wmkafka "github.com/ThreeDotsLabs/watermill-kafka/v2/pkg/kafka" ) // Producer wraps a Watermill Kafka publisher for publishing messages to Kafka topics. type Producer struct { publisher message.Publisher } // NewProducer creates a Producer connected to the given brokers. func NewProducer(brokers []string) (*Producer, error) { publisher, createError := wmkafka.NewPublisher( wmkafka.PublisherConfig{ Brokers: brokers, Marshaler: wmkafka.DefaultMarshaler{}, }, watermill.NopLogger{}, ) if createError != nil { return nil, createError } return &Producer{publisher: publisher}, nil } // Publish writes a single message to the named topic. // The context parameter is accepted for interface compatibility but is not forwarded // to the Watermill publisher, which does not accept a context. func (producer *Producer) Publish(_ context.Context, topic, jobID string) error { msg := message.NewMessage(watermill.NewUUID(), []byte(jobID)) return producer.publisher.Publish(topic, msg) } // Close shuts down the underlying Kafka publisher. func (producer *Producer) Close() { _ = producer.publisher.Close() }