module Qian::Event

Public Class Methods

avro_schema_name() click to toggle source

当前 Event 类型对应的 Avro Schema 全名

@return [String]

# File lib/qian/event.rb, line 34
def self.avro_schema_name
  "com.jianshu.event.#{Qian::Util.convert_class_name_to_package_name(self.to_s)}"
end
included(base) click to toggle source
# File lib/qian/event.rb, line 4
def self.included(base)
  base.class_eval do
    include Virtus.value_object(:strict => true)

    #
    # 设置当前 Event 的 Kafka Topic
    #
    # @param [<type>] topic_name <description>
    #
    # @return [<type>] <description>
    #
    def self.kafka_topic(topic_name)
      @kafka_topic_name = topic_name.to_s
    end

    #
    # 返回当前 Event 的 Kafka Topic
    #
    # @return [String]
    #
    def self.kafka_topic_name
      @kafka_topic_name
    end

    #
    # 当前 Event 类型对应的 Avro Schema 全名
    #
    #
    # @return [String]
    #
    def self.avro_schema_name
      "com.jianshu.event.#{Qian::Util.convert_class_name_to_package_name(self.to_s)}"
    end
  end
end
kafka_topic(topic_name) click to toggle source

设置当前 Event 的 Kafka Topic

@param [<type>] topic_name <description>

@return [<type>] <description>

# File lib/qian/event.rb, line 15
def self.kafka_topic(topic_name)
  @kafka_topic_name = topic_name.to_s
end
kafka_topic_name() click to toggle source

返回当前 Event 的 Kafka Topic

@return [String]

# File lib/qian/event.rb, line 24
def self.kafka_topic_name
  @kafka_topic_name
end

Public Instance Methods

attrs_with_string_key() click to toggle source

将自己 encode 为 avro binary data

  • Avro::Turf 只接受

@return [String]

# File lib/qian/event.rb, line 57
def attrs_with_string_key
  self.attributes.deep_stringify_keys
end
avro_encoded_data() click to toggle source

将自己 encode 为 avro binary data

@return [String]

# File lib/qian/event.rb, line 67
def avro_encoded_data
  Qian.avro.encode(self.attrs_with_string_key, :schema_name => self.class.avro_schema_name)
end
emit!() click to toggle source

将自己事件发送出去

@return [void]

# File lib/qian/event.rb, line 46
def emit!
  Qian.kafka_producer.produce(avro_encoded_data, :topic => self.class.kafka_topic_name)
end