Class: KafkaEventHub::Producer
- Defined in:
- lib/kafka_event_hub/producer.rb
Constant Summary
Constants inherited from Config
Instance Method Summary collapse
-
#initialize(topic = nil) ⇒ Producer
constructor
A new instance of Producer.
-
#produce(topic: nil, key:, payload:, partition: nil) ⇒ Object
Produce any message to Kafka.
Methods inherited from Config
Constructor Details
#initialize(topic = nil) ⇒ Producer
Returns a new instance of Producer.
5 6 7 |
# File 'lib/kafka_event_hub/producer.rb', line 5 def initialize(topic = nil) super('producer' => true, 'topic' => topic) end |
Instance Method Details
#produce(topic: nil, key:, payload:, partition: nil) ⇒ Object
Produce any message to Kafka
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/kafka_event_hub/producer.rb', line 10 def produce(topic: nil, key:, payload:, partition: nil) real_topic = topic || @topic producer = @kafka.producer json = payload.is_a?(String) ? payload : payload.to_json producer.produce( topic: real_topic, key: key, payload: json, partition: partition ).wait ensure producer&.close end |