Class: Kafka::Group

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

Overview

noinspection JRubyStringImportInspection

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) reset the consumer group to start at the

earliest message present in the log by clearing any offsets for the group stored in Zookeeper.

:auto_offset_reset => “smallest” or “largest” - (optional, default ‘largest’) If the consumer does not already

have an established offset to consume from, start with the earliest message present in the log (smallest) or 
after the last message in the log (largest).

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

exceptions are logged.


27
28
29
30
31
32
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/jruby-kafka/group.rb', line 27

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

  @zk_connect = options[:zk_connect]
  @group_id = options[:group_id]
  @topic = options[:topic_id]
  @topics_allowed = options[:allow_topics]
  @topics_filtered = options[:filter_topics]
  @zk_session_timeout = '6000'
  @zk_connect_timeout = '6000'
  @zk_sync_time = '2000'
  @reset_beginning = nil
  @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 = "#{false}"
  @consumer_restart_sleep_ms = '0'
  @consumer_id = nil
  @key_decoder_class = "kafka.serializer.DefaultDecoder"
  @value_decoder_class = "kafka.serializer.DefaultDecoder"

  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[:auto_offset_reset]
    @auto_offset_reset = "#{options[:auto_offset_reset]}"
  end

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

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

  if options[:reset_beginning]
    if not options[:auto_offset_reset] || options[:auto_offset_reset] != 'smallest'
      raise KafkaError.new('reset_beginning => from-beginning must be used with auto_offset_reset => smallest')
    end
    @reset_beginning = "#{options[:reset_beginning]}"
  end

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

Instance Method Details

#run(a_num_threads, a_queue) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/jruby-kafka/group.rb', line 160

def run(a_num_threads, a_queue)
  begin
    if @reset_beginning == 'from-beginning'
      Java::kafka::utils::ZkUtils.maybeDeletePath(@zk_connect, "/consumers/#{@group_id}")
    end

    @consumer = Java::kafka::consumer::Consumer.createJavaConsumerConnector(create_consumer_config)
  rescue ZkException => e
    raise KafkaError.new(e), "Got ZkException: #{e}"
  end

  thread_value = a_num_threads.to_java Java::int
  streams = get_streams(thread_value)

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

  thread_number = 0
  streams.each do |stream|
    @executor_submit.call(Kafka::Consumer.new(stream, thread_number, a_queue, @consumer_restart_on_error, @consumer_restart_sleep_ms))
    thread_number += 1
  end
  @running = true
end

#running?Boolean

Returns:

  • (Boolean)


185
186
187
# File 'lib/jruby-kafka/group.rb', line 185

def running?
  @running
end

#shutdownObject



150
151
152
153
154
155
156
157
158
# File 'lib/jruby-kafka/group.rb', line 150

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