Class: WaterDrop::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/water_drop/message.rb

Overview

Message class which encapsulate single Kafka message logic and its delivery

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(topic, message) ⇒ WaterDrop::Message

Returns WaterDrop message instance.

Examples:

Creating a new message

WaterDrop::Message.new(topic, message)

Parameters:

  • topic (String, Symbol)

    a topic to which we want to send a message

  • message (Object)

    any object that can be serialized to a JSON string or that can be casted to a string



12
13
14
15
# File 'lib/water_drop/message.rb', line 12

def initialize(topic, message)
  @topic = topic.to_s
  @message = message
end

Instance Attribute Details

#messageObject (readonly)

Returns the value of attribute message.



4
5
6
# File 'lib/water_drop/message.rb', line 4

def message
  @message
end

#topicObject (readonly)

Returns the value of attribute topic.



4
5
6
# File 'lib/water_drop/message.rb', line 4

def topic
  @topic
end

Instance Method Details

#send!Object

Note:

Won’t send any messages if send_messages config flag is set to false

Sents a current message to Kafka

Examples:

Set a message

WaterDrop::Message.new(topic, message).send!


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/water_drop/message.rb', line 21

def send!
  return true unless ::WaterDrop.config.send_messages?

  Pool.with do |producer|
    producer.send_messages([
      Poseidon::MessageToSend.new(topic, message)
    ])
  end
  ::WaterDrop.logger.info("Message #{message} was sent to topic '#{topic}'")
rescue StandardError => e
  # Even if we dont reraise this exception, it should log that it happened
  ::WaterDrop.logger.error(e)
  # Reraise if we want to raise on failure
  # Ignore if we dont want to know that something went wrong
  return unless ::WaterDrop.config.raise_on_failure?
  raise(e)
end