Class: Kafka::Group

Inherits:
Object
  • Object
show all
Defined in:
lib/jruby-kafka/group.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Group

Create a Kafka client group

options: :zk_connect => “localhost:2181” - REQUIRED: The connection string for the

zookeeper connection in the form host:port. Multiple URLS can be given to allow fail-over.

:zk_connect_timeout => “6000” - (optional) The max time that the client waits while establishing a connection to zookeeper. :group_id => “group” - REQUIRED: The group id to consume on. :topic_id => “topic” - REQUIRED: The topic id to consume on. :reset_beginning => “from-beginning” - (optional) If the consumer does not already have an established offset

to consume from, start with the earliest message present in the log rather than the latest message.

:consumer_restart_on_error => “true” - (optional) Controls if consumer threads are to restart on caught exceptions.

exceptions are logged.


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/jruby-kafka/group.rb', line 33

def initialize(options={})
  validate_required_arguments(options)

  @zk_connect = options[:zk_connect]
  @group_id = options[:group_id]
  @topic = options[:topic_id]
  @zk_session_timeout = '6000'
  @zk_connect_timeout = '6000'
  @zk_sync_time = '2000'
  @auto_offset_reset = 'largest'
  @auto_commit_interval = '1000'
  @running = false
  @rebalance_max_retries = '4'
  @rebalance_backoff_ms = '2000'
  @socket_timeout_ms = "#{30 * 1000}"
  @socket_receive_buffer_bytes = "#{64 * 1024}"
  @fetch_message_max_bytes = "#{1024 * 1024}"
  @auto_commit_enable = "#{true}"
  @queued_max_message_chunks = '10'
  @fetch_min_bytes = '1'
  @fetch_wait_max_ms = '100'
  @refresh_leader_backoff_ms = '200'
  @consumer_timeout_ms = '-1'
  @consumer_restart_on_error = "#{true}"
  @consumer_restart_sleep_ms = '2000'

  if options[:zk_connect_timeout]
    @zk_connect_timeout = options[:zk_connect_timeout]
  end
  if options[:zk_session_timeout]
    @zk_session_timeout = options[:zk_session_timeout]
  end
  if options[:zk_sync_time]
    @zk_sync_time = options[:zk_sync_time]
  end
  if options[:auto_commit_interval]
    @auto_commit_interval = options[:auto_commit_interval]
  end

  if options[:rebalance_max_retries]
    @rebalance_max_retries = options[:rebalance_max_retries]
  end

  if options[:rebalance_backoff_ms]
    @rebalance_backoff_ms = options[:rebalance_backoff_ms]
  end

  if options[:socket_timeout_ms]
    @socket_timeout_ms = options[:socket_timeout_ms]
  end

  if options[:socket_receive_buffer_bytes]
    @socket_receive_buffer_bytes = options[:socket_receive_buffer_bytes]
  end

  if options[:fetch_message_max_bytes]
    @fetch_message_max_bytes = options[:fetch_message_max_bytes]
  end

  if options[:auto_commit_enable]
    @auto_commit_enable = "#{options[:auto_commit_enable]}"
  end

  if options[:queued_max_message_chunks]
    @queued_max_message_chunks = options[:queued_max_message_chunks]
  end

  if options[:fetch_min_bytes]
    @fetch_min_bytes = options[:fetch_min_bytes]
  end

  if options[:fetch_wait_max_ms]
    @fetch_wait_max_ms = options[:fetch_wait_max_ms]
  end

  if options[:refresh_leader_backoff_ms]
    @refresh_leader_backoff_ms = options[:refresh_leader_backoff_ms]
  end

  if options[:consumer_timeout_ms]
    @consumer_timeout_ms = options[:consumer_timeout_ms]
  end

  if options[:consumer_restart_on_error]
    @consumer_restart_on_error = options[:consumer_restart_on_error]
  end

  if options[:consumer_restart_sleep_ms]
    @consumer_restart_sleep_ms = options[:consumer_restart_sleep_ms]
  end


  if options[:reset_beginning]
    if options[:reset_beginning] == 'from-beginning'
      @auto_offset_reset = 'smallest'
    else
      @auto_offset_reset = 'largest'
    end
  end
end

Instance Method Details

#run(a_numThreads, a_queue) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/jruby-kafka/group.rb', line 153

def run(a_numThreads, a_queue)
  begin
    if @auto_offset_reset == 'smallest'
      Java::kafka::utils::ZkUtils.maybeDeletePath(@zk_connect, "/consumers/#{@group_id}")
    end

    @consumer = Java::kafka::consumer::Consumer.createJavaConsumerConnector(createConsumerConfig())
  rescue ZkException => e
    raise KafkaError.new(e), "Got ZkException: #{e}"
  end
  topicCountMap = java.util.HashMap.new()
  thread_value = a_numThreads.to_java Java::int
  topicCountMap.put(@topic, thread_value)
  consumerMap = @consumer.createMessageStreams(topicCountMap)
  streams = Array.new(consumerMap[@topic])

  @executor = Executors.newFixedThreadPool(a_numThreads)
  @executor_submit = @executor.java_method(:submit, [Java::JavaLang::Runnable.java_class])

  threadNumber = 0
  for stream in streams
    @executor_submit.call(Kafka::Consumer.new(stream, threadNumber, a_queue, @consumer_restart_on_error, @consumer_restart_sleep_ms))
    threadNumber += 1
  end
  @running = true
end

#running?Boolean

Returns:

  • (Boolean)


181
182
183
# File 'lib/jruby-kafka/group.rb', line 181

def running?
  @running
end

#shutdownObject



142
143
144
145
146
147
148
149
150
# File 'lib/jruby-kafka/group.rb', line 142

def shutdown()
  if @consumer
    @consumer.shutdown()
  end
  if @executor
    @executor.shutdown()
  end
  @running = false
end