|
2 | 2 | For complete JavaDocs, see:
|
3 | 3 | http://kafka.apache.org/0100/javadoc/index.html?org/apache/kafka/clients/consumer/package-summary.html"}
|
4 | 4 | clj-kafka-x.consumers.simple
|
5 |
| - (:require [clj-kafka-x.data :refer :all]) |
6 |
| - (:import java.util.List |
7 |
| - java.util.regex.Pattern |
8 |
| - [org.apache.kafka.clients.consumer ConsumerRebalanceListener Consumer KafkaConsumer OffsetAndMetadata OffsetCommitCallback] |
| 5 | + (:require [clj-kafka-x.data :refer :all] |
| 6 | + [clj-kafka-x.impl.helpers :refer :all]) |
| 7 | + (:import java.util.regex.Pattern |
| 8 | + [org.apache.kafka.clients.consumer ConsumerRebalanceListener Consumer KafkaConsumer OffsetCommitCallback] |
9 | 9 | [org.apache.kafka.common.serialization ByteArrayDeserializer Deserializer StringDeserializer]
|
10 | 10 | org.apache.kafka.common.TopicPartition
|
11 |
| - (java.util Map) |
12 |
| - (clojure.lang IMapEntry MapEntry IRecord))) |
| 11 | + (java.util Map Collection) |
| 12 | + (java.time Duration))) |
13 | 13 |
|
14 | 14 |
|
15 | 15 | (defn string-deserializer [] (StringDeserializer.))
|
16 | 16 | (defn byte-array-deserializer [] (ByteArrayDeserializer.))
|
17 | 17 |
|
18 |
| -(defn- walk |
19 |
| - [inner outer form] |
20 |
| - (cond |
21 |
| - (list? form) (outer (apply list (map inner form))) |
22 |
| - (instance? IMapEntry form) |
23 |
| - (outer (MapEntry/create (inner (key form)) (inner (val form)))) |
24 |
| - (seq? form) (outer (doall (map inner form))) |
25 |
| - (instance? IRecord form) |
26 |
| - (outer (reduce (fn [r x] (conj r (inner x))) form form)) |
27 |
| - (coll? form) (outer (into (empty form) (map inner form))) |
28 |
| - :else (outer form))) |
29 |
| - |
30 |
| -(defn- postwalk |
31 |
| - [f form] |
32 |
| - (walk (partial postwalk f) f form)) |
33 |
| - |
34 |
| -(defn- stringify-keys |
35 |
| - [m] |
36 |
| - (let [f (fn [[k v]] (if (keyword? k) [(name k) v] [k v]))] |
37 |
| - (postwalk (fn [x] (if (map? x) (into {} (map f x)) x)) m))) |
38 |
| - |
39 |
| -(defn update-in-when |
40 |
| - [m k f & args] |
41 |
| - (if (not= ::not-found (get-in m k ::not-found)) |
42 |
| - (apply update-in m k f args) |
43 |
| - m)) |
44 |
| - |
45 |
| -(defn- int! [v] |
46 |
| - (if (number? v) |
47 |
| - (int v) |
48 |
| - (condp instance? v |
49 |
| - String (try |
50 |
| - (-> v str Integer/parseInt) |
51 |
| - (catch NumberFormatException _ nil)) |
52 |
| - nil))) |
53 |
| - |
54 |
| -(defn- safe-config ^Map [^Map config] |
55 |
| - (if (map? config) |
56 |
| - (-> config |
57 |
| - stringify-keys |
58 |
| - (update-in-when ["fetch.min.bytes"] int!) |
59 |
| - (update-in-when ["fetch.max.bytes"] int!) |
60 |
| - (update-in-when ["fetch.max.wait.ms"] int!) |
61 |
| - (update-in-when ["heartbeat.interval.ms"] int!) |
62 |
| - (update-in-when ["max.partition.fetch.bytes"] int!) |
63 |
| - (update-in-when ["session.timeout.ms"] int!) |
64 |
| - (update-in-when ["max.poll.interval.ms"] int!) |
65 |
| - (update-in-when ["max.poll.records"] int!) |
66 |
| - (update-in-when ["receive.buffer.bytes"] int!) |
67 |
| - (update-in-when ["request.timeout.ms"] int!) |
68 |
| - (update-in-when ["send.buffer.bytes"] int!) |
69 |
| - (update-in-when ["auto.commit.interval.ms"] int!) |
70 |
| - (update-in-when ["fetch.max.wait.ms"] int!) |
71 |
| - (update-in-when ["metrics.num.samples"] int!)) |
72 |
| - {})) |
73 |
| - |
74 | 18 | (defn consumer
|
75 | 19 | "Takes a map of config options and returns a `KafkaConsumer` for consuming records from Kafka.
|
76 | 20 |
|
|
157 | 101 | (let [listener (reify ConsumerRebalanceListener
|
158 | 102 | (onPartitionsAssigned [_ partitions] (assigned-callback (mapv to-clojure partitions)))
|
159 | 103 | (onPartitionsRevoked [_ partitions] (revoked-callback (mapv to-clojure partitions))))
|
160 |
| - topics (cond |
161 |
| - (string? topics) (vector topics) |
162 |
| - (and (sequential? topics) (string? (first topics))) topics |
163 |
| - (= Pattern (type topics)) topics |
164 |
| - (and (sequential? topics) (map? (first topics))) topics |
165 |
| - :else (throw |
166 |
| - (ex-info "Topic should be a string, sequence (of strings or maps) or pattern" |
167 |
| - {:topic topics})))] |
| 104 | + topics ^Collection (cond |
| 105 | + (string? topics) (vector topics) |
| 106 | + (and (sequential? topics) (string? (first topics))) topics |
| 107 | + (= Pattern (type topics)) topics |
| 108 | + (and (sequential? topics) (map? (first topics))) topics |
| 109 | + :else (throw |
| 110 | + (ex-info "Topic should be a string, sequence (of strings or maps) or pattern" |
| 111 | + {:topic topics})))] |
168 | 112 |
|
169 | 113 | (if (and (sequential? topics) (map? (first topics)))
|
170 | 114 | (do
|
|
288 | 232 | "
|
289 | 233 | [^Consumer consumer & {:keys [timeout] :or {timeout 1000}}]
|
290 | 234 |
|
291 |
| - (let [consumer-records (.poll consumer timeout)] |
| 235 | + (let [duration (Duration/ofMillis timeout) |
| 236 | + consumer-records (.poll consumer duration)] |
292 | 237 | (to-clojure consumer-records)))
|
293 | 238 |
|
294 | 239 |
|
|
365 | 310 | "
|
366 | 311 | ([^Consumer consumer] (.commitSync consumer))
|
367 | 312 | ([^Consumer consumer topic-partitions-offsets-metadata]
|
368 |
| - (let [tp-om-map (map->tp-om-map topic-partitions-offsets-metadata)] |
| 313 | + (let [tp-om-map ^Map (map->tp-om-map topic-partitions-offsets-metadata)] |
369 | 314 | (.commitSync consumer tp-om-map))))
|
370 | 315 |
|
371 | 316 |
|
|
0 commit comments