2014-10-06 15:08:30 +00:00
|
|
|
require 'hermann'
|
|
|
|
require 'concurrent'
|
2014-10-08 22:15:10 +00:00
|
|
|
require 'json'
|
2014-10-06 15:08:30 +00:00
|
|
|
|
|
|
|
module Hermann
|
|
|
|
module Provider
|
2014-10-06 20:46:00 +00:00
|
|
|
# This class simulates the kafka producer class within a java environment.
|
|
|
|
# If the producer throw an exception within the Promise a call to +.value!+
|
|
|
|
# will raise the exception and the rejected flag will be set to true
|
|
|
|
#
|
2014-10-06 15:08:30 +00:00
|
|
|
class JavaProducer
|
2014-10-13 21:05:58 +00:00
|
|
|
attr_accessor :producer
|
2014-10-06 15:08:30 +00:00
|
|
|
|
2014-10-23 00:12:44 +00:00
|
|
|
#default kafka Producer options
|
|
|
|
DEFAULTS = {
|
|
|
|
'serializer.class' => 'kafka.serializer.StringEncoder',
|
|
|
|
'partitioner.class' => 'kafka.producer.DefaultPartitioner',
|
|
|
|
'request.required.acks' => '1',
|
|
|
|
'message.send.max.retries' => '0'
|
|
|
|
}.freeze
|
2014-10-14 17:26:02 +00:00
|
|
|
|
|
|
|
# Instantiate JavaProducer
|
|
|
|
#
|
|
|
|
# @params [String] list of brokers
|
|
|
|
#
|
|
|
|
# @params [Hash] hash of kafka attributes, overrides defaults
|
|
|
|
#
|
|
|
|
# @raises [RuntimeError] if brokers string is nil/empty
|
|
|
|
#
|
|
|
|
# ==== Examples
|
|
|
|
#
|
|
|
|
# JavaProducer.new('0:9092', {'request.required.acks' => '1'})
|
|
|
|
#
|
|
|
|
def initialize(brokers, opts={})
|
2014-10-23 00:12:44 +00:00
|
|
|
config = create_config(brokers, opts)
|
2014-10-08 22:15:10 +00:00
|
|
|
@producer = JavaApiUtil::Producer.new(config)
|
2014-10-06 15:08:30 +00:00
|
|
|
end
|
|
|
|
|
2014-10-06 20:46:00 +00:00
|
|
|
# Push a value onto the Kafka topic passed to this +Producer+
|
|
|
|
#
|
|
|
|
# @param [Object] value A single object to push
|
2014-10-13 21:05:58 +00:00
|
|
|
# @param [String] topic to push message to
|
2014-10-06 20:46:00 +00:00
|
|
|
#
|
|
|
|
# @return +Concurrent::Promise+ Representa a promise to send the
|
|
|
|
# data to the kafka broker. Upon execution the Promise's status
|
|
|
|
# will be set
|
2014-10-14 17:54:52 +00:00
|
|
|
def push_single(msg, topic, unused)
|
2014-10-07 22:27:36 +00:00
|
|
|
Concurrent::Promise.execute {
|
2014-10-24 18:47:51 +00:00
|
|
|
begin
|
2014-10-31 00:59:34 +00:00
|
|
|
data = ProducerUtil::KeyedMessage.new(topic, msg)
|
|
|
|
# begin
|
2014-10-24 18:47:51 +00:00
|
|
|
@producer.send(data)
|
2014-10-31 00:59:34 +00:00
|
|
|
rescue Exception => e
|
|
|
|
# rescue Java::KafkaCommon::FailedToSendMessageException => jexc
|
|
|
|
# raise Hermann::Errors::ConnectivityError.new(jexc.message,
|
|
|
|
# :java_exception => jexc)
|
|
|
|
puts "............#{e.message}"
|
|
|
|
puts e.backtrace.join("\n")
|
2014-10-24 18:47:51 +00:00
|
|
|
end
|
2014-10-06 18:23:32 +00:00
|
|
|
}
|
2014-10-06 15:08:30 +00:00
|
|
|
end
|
|
|
|
|
2014-10-14 17:54:52 +00:00
|
|
|
# No-op for now
|
|
|
|
def connected?
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
# No-op for now
|
|
|
|
def errored?
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
# No-op for now
|
|
|
|
def connect(timeout=0)
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2014-10-06 15:08:30 +00:00
|
|
|
private
|
2014-10-14 17:26:02 +00:00
|
|
|
# Creates a ProducerConfig object
|
|
|
|
#
|
2014-10-23 00:12:44 +00:00
|
|
|
# @param [String] comma separated list of brokers
|
2014-10-14 17:26:02 +00:00
|
|
|
#
|
|
|
|
# @param [Hash] brokers passed into this function
|
|
|
|
# @option args [String] :brokers - string of brokers
|
|
|
|
#
|
2014-10-23 00:12:44 +00:00
|
|
|
# @return [ProducerConfig] - packaged config for +Producer+
|
2014-10-14 17:26:02 +00:00
|
|
|
#
|
|
|
|
# @raises [RuntimeError] if options does not contain key value strings
|
2014-10-23 00:12:44 +00:00
|
|
|
def create_config(brokers, opts={})
|
|
|
|
brokers = { 'metadata.broker.list' => brokers }
|
|
|
|
options = DEFAULTS.merge(brokers).merge(opts)
|
|
|
|
properties = Hermann.package_properties(options)
|
|
|
|
ProducerUtil::ProducerConfig.new(properties)
|
2014-10-14 17:26:02 +00:00
|
|
|
end
|
2014-10-06 15:08:30 +00:00
|
|
|
end
|
|
|
|
end
|
2014-10-08 16:45:07 +00:00
|
|
|
end
|