-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #296 from FundingCircle/louiseklodt/fix-consumer-d…
…ocs-for-poll Fix Client API docs to use poll instead of log and add sample app for Client API
- Loading branch information
Showing
9 changed files
with
235 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,4 @@ pom.xml.asc | |
.lein-* | ||
.cpcache | ||
.nrepl-port | ||
/examples/*/target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Roll dice | ||
|
||
This repo contains a simple example app to demonstrate usage of Jackdaw's Client API (Consumer and Producer API). The app rolls a dice `n` number of times where `n` is provided by the user. A Jackdaw Producer writes the numbers to the input topic `rolldice`. A Jackdaw consumer continuously reads from the topic, adds up the numbers and prints out the result. To exit the consumer loop, press Ctr+c. | ||
|
||
## Installation | ||
|
||
Clone this repo. | ||
|
||
## Usage | ||
|
||
1. Bring up the Kafka services by running `docker-compose up -d`. Alternatively, run the Kafka services (broker and zookeeper) locally, following instructions in the [Apache Kafka Quickstart](https://kafka.apache.org/quickstart). | ||
|
||
2. From the root directory of the repo, run: `lein run` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
version: '2' | ||
services: | ||
zookeeper: | ||
image: confluentinc/cp-zookeeper:latest | ||
hostname: zookeeper | ||
container_name: zookeeper | ||
ports: | ||
- "2181:2181" | ||
environment: | ||
ZOOKEEPER_CLIENT_PORT: 2181 | ||
ZOOKEEPER_TICK_TIME: 2000 | ||
kafka: | ||
image: confluentinc/cp-enterprise-kafka:latest | ||
hostname: broker | ||
container_name: broker | ||
depends_on: | ||
- zookeeper | ||
ports: | ||
- "9092:9092" | ||
environment: | ||
KAFKA_BROKER_ID: 1 | ||
KAFKA_ZOOKEEPER_CONNECT: 'zookeeper:2181' | ||
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT | ||
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://broker:29092,PLAINTEXT_HOST://localhost:9092 | ||
KAFKA_METRIC_REPORTERS: io.confluent.metrics.reporter.ConfluentMetricsReporter | ||
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1 | ||
KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 0 | ||
CONFLUENT_METRICS_REPORTER_BOOTSTRAP_SERVERS: broker:29092 | ||
CONFLUENT_METRICS_REPORTER_ZOOKEEPER_CONNECT: zookeeper:2181 | ||
CONFLUENT_METRICS_REPORTER_TOPIC_REPLICAS: 1 | ||
CONFLUENT_METRICS_ENABLE: 'true' | ||
CONFLUENT_SUPPORT_CUSTOMER_ID: 'anonymous' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
(defproject rolldice "0.1.0-SNAPSHOT" | ||
:description "FIXME: write description" | ||
:url "http://example.com/FIXME" | ||
:license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0" | ||
:url "https://www.eclipse.org/legal/epl-2.0/"} | ||
:dependencies [[org.clojure/clojure "1.10.1"] | ||
[fundingcircle/jackdaw "0.7.8"] | ||
[proto-repl "0.3.1"] | ||
[pjstadig/humane-test-output "0.11.0"] | ||
[com.taoensso/timbre "4.2.0"] | ||
[org.slf4j/slf4j-nop "1.7.30"]] | ||
:main ^:skip-aot roll-dice.core | ||
:target-path "target/%s" | ||
:profiles {:uberjar {:aot :all | ||
:jvm-opts ["-Dclojure.compiler.direct-linking=true"]} | ||
:dev {:plugins [[com.jakemccrary/lein-test-refresh "0.24.1"]]}}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
(ns roll-dice.core | ||
"This sample app is a simple Kafka application to demonstrate how to use | ||
Jackdaw's Client API (Producer and Consumer). | ||
It rolls a dice `n` number of times (input by the user). | ||
A Jackdaw Producer then writes the numbers to the input topic `rolldice`. | ||
A Jackdaw Consumer reads from the topic and adds up the numbers | ||
and prints out the result." | ||
(:require | ||
[roll-dice.kafka :as kafka])) | ||
|
||
(defn roll-dice [n] | ||
(repeatedly n | ||
#(let [rnd-side (fn [] (inc (rand-int 6)))] | ||
[(rnd-side) (rnd-side)]))) | ||
|
||
(defn process-records [records] | ||
(let [numbers (:value (first records)) | ||
sums (mapv #(apply + %) numbers)] | ||
(println "numbers: " numbers) | ||
(println "sums: " sums))) | ||
|
||
(defn start-consumer-thread! [topic group-id] | ||
(-> (Thread. #(kafka/process-messages! topic group-id process-records)) | ||
(.start))) | ||
|
||
(defn -main [] | ||
(let [topic "rolldice" | ||
group-id "rolldice-consumer"] | ||
(start-consumer-thread! topic group-id) | ||
(loop [] | ||
(println "How many times would you like to roll the dice? \nPlease enter a positive integer:") | ||
(let [n (Integer/parseInt (read-line)) | ||
roll-n-times! #(roll-dice n)] | ||
(kafka/produce-message! topic roll-n-times!) | ||
(Thread/sleep 2000) | ||
(println "\nPress Ctr+c to exit or keep rolling.\n") | ||
(recur))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
(ns roll-dice.kafka | ||
(:require | ||
[jackdaw.client :as jc] | ||
[jackdaw.serdes :refer [string-serde edn-serde]] | ||
[taoensso.timbre :refer [error info]]) | ||
(:import [org.apache.kafka.common.errors WakeupException])) | ||
|
||
(def kafka-config {"bootstrap.servers" "localhost:9092"}) | ||
|
||
(defn producer-config [topic] | ||
(merge kafka-config | ||
{"acks" "all" | ||
"client.id" (str "producer-" (name topic))})) | ||
|
||
(defn consumer-config [topic group-id] | ||
(merge kafka-config | ||
{"group.id" group-id | ||
"client.id" (str "consumer-" (name topic)) | ||
"auto.offset.reset" "earliest"})) | ||
|
||
(defn topic-config [topic] | ||
{:topic-name topic | ||
:key-serde (string-serde) | ||
:value-serde (edn-serde)}) | ||
|
||
(defn poll-and-loop! | ||
"Continuously fetches records every `poll-ms`, processes them and commits offset after each poll." | ||
[consumer processing-fn continue?] | ||
(let [poll-ms 5000] | ||
(loop [] | ||
(if @continue? | ||
(let [records (jc/poll consumer poll-ms)] | ||
(when (seq records) | ||
(processing-fn records) | ||
(info "commit sync at offset" (-> records last :offset inc)) | ||
(.commitSync consumer)) | ||
(recur)))))) | ||
|
||
(defn stop-and-close-consumer! | ||
"Stops the consumer polling loop and closes the consumer." | ||
[consumer continue?] | ||
(reset! continue? false) | ||
(.close consumer) | ||
(info "Closed Kafka Consumer")) | ||
|
||
(defn start-consumer! | ||
"Starts consumer loop to process events read from `topic`" | ||
[consumer processing-fn continue?] | ||
(try | ||
(poll-and-loop! consumer processing-fn continue?) | ||
(catch WakeupException e) ;; ignore for shutdown | ||
(finally | ||
(stop-and-close-consumer! consumer continue?)))) | ||
|
||
(defn add-shutdown-hook-consumer! | ||
"Registers a shutdown hook to exit the consumer cleanly" | ||
[consumer continue?] | ||
(.addShutdownHook (Runtime/getRuntime) | ||
(Thread. (fn [] | ||
(info "Stopping Kafka Consumer...") | ||
(reset! continue? false) | ||
(.wakeup consumer))))) ; wakeup causes consumer to break out of polling | ||
|
||
(defn process-messages! | ||
"Creates Kafka Consumer and shutdown hook, and starts the consumer" | ||
[topic group-id processing-fn] | ||
(let [topic-config (topic-config topic) | ||
consumer-config (consumer-config topic group-id) | ||
continue? (atom true) | ||
consumer (jc/subscribed-consumer consumer-config [topic-config])] | ||
(add-shutdown-hook-consumer! consumer continue?) | ||
(start-consumer! consumer processing-fn continue?))) | ||
|
||
(defn produce-message! | ||
"Creates a Kafka Producer and writes message to `topic` by calling `producer-fn`" | ||
[topic producer-fn] | ||
(let [topic-config (topic-config topic) | ||
producer-config (producer-config topic)] | ||
(with-open [producer (jc/producer producer-config topic-config)] | ||
(let [values (producer-fn)] | ||
@(jc/produce! producer topic-config values))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
(ns roll-dice.core-test | ||
(:require | ||
[clojure.test :refer [deftest is testing]] | ||
[roll-dice.core :as core])) |