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, options = {}) ⇒ 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

  • options (Hash) (defaults to: {})

    (optional) additonal options to pass to the Kafka producer



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

def initialize(topic, message, options = {})
  @topic = topic.to_s
  @message = message
  @options = options
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

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
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!


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

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

  Pool.with { |producer| producer.send_message(self) }

  ::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