├── .gitignore ├── LICENSE ├── Makefile ├── README.md ├── deps.edn ├── dockerfiles ├── Dockerfile.clj-kafka-builder └── docker-compose.dev.yml ├── graalvm ├── jni-config.json ├── proxy-config.json ├── reflect-config.json └── resource-config.json ├── resources └── logback.xml └── src └── core.clj /.gitignore: -------------------------------------------------------------------------------- 1 | pom.xml.asc 2 | *.jar 3 | *.class 4 | /lib/ 5 | /classes/ 6 | /target/ 7 | /checkouts/ 8 | .lein-deps-sum 9 | .lein-repl-history 10 | .lein-plugins/ 11 | .lein-failures 12 | .nrepl-port 13 | .cpcache/ 14 | .idea/ 15 | **.iml 16 | release.properties 17 | pom.xml.releaseBackup 18 | .env 19 | clj-kafka 20 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2020 Dainius Jocas 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | run-dev-env: 2 | @docker-compose \ 3 | -p clj-kafka \ 4 | -f dockerfiles/docker-compose.dev.yml \ 5 | down 6 | @docker-compose \ 7 | -p clj-kafka \ 8 | -f dockerfiles/docker-compose.dev.yml \ 9 | up 10 | 11 | build-clj-kafka: 12 | docker build -f dockerfiles/Dockerfile.clj-kafka-builder -t clj-kafka-native-image . 13 | docker rm clj-kafka-native-image-build || true 14 | docker create --name clj-kafka-native-image-build clj-kafka-native-image 15 | docker cp clj-kafka-native-image-build:/usr/src/app/clj-kafka clj-kafka 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # clojure-kafka-graavm-native-image 2 | 3 | It turns out that it is possible to compile Kafka Producer and Consumer with GraalVM native-image and the demo is all in Clojure. 4 | 5 | ## TL;DR 6 | 7 | ```shell script 8 | make build-clj-kafka && ./clj-kafka 9 | ``` 10 | 11 | ## Demo 12 | 13 | It will start a background thread with a Kafka Producer that every second sends a message to the Kafka. 14 | And in the main thread it'll launch a Kafka Consumer that polls for messages from the same topic as is used by the producer. 15 | Process doesn't stop. 16 | 17 | Cool thing is that the binary starts instantly and use ~28 mb of RAM. 18 | ``` 19 | ./clj-kafka 0.01s user 0.02s system 0% cpu 4.168 total 20 | avg shared (code): 0 KB 21 | avg unshared (data/stack): 0 KB 22 | total (sum): 0 KB 23 | max memory: 28 MB 24 | page faults from disk: 0 25 | other page faults: 1488 26 | ``` 27 | 28 | ## Params 29 | 30 | Environment variables are used: 31 | - `BOOTSTRAP_SERVERS_CONFIG`: Kafka brokers, default value `"127.0.0.1:9092"` 32 | - `TOPIC`: kafka topic name, default value `"test"` 33 | 34 | ## Optional 35 | 36 | Start Kafka cluster in docker-compose in another terminal: 37 | ```shell script 38 | make run-dev-env 39 | ``` 40 | 41 | ## Requirements 42 | 43 | Linux, Docker, docker-compose, clojure. 44 | 45 | For MacOS download GraalVM, install native-image, set `JAVA_HOME`, add native-image to `PATH` and run: 46 | ```shell script 47 | clojure -A:native-image 48 | ``` 49 | Or in one step: 50 | ```shell script 51 | (JAVA_HOME=/home/user/graalvm-ce-java11-20.0.0/ && PATH=/home/user/graalvm-ce-java11-20.0.0/bin:$PATH && clojure -A:native-image) 52 | ``` 53 | 54 | ## License 55 | 56 | Copyright © 2020 [Dainius Jocas](https://www.jocas.lt). 57 | 58 | Distributed under the The Apache License, Version 2.0. 59 | -------------------------------------------------------------------------------- /deps.edn: -------------------------------------------------------------------------------- 1 | {:paths 2 | ["src" "resources"] 3 | :deps 4 | {org.clojure/clojure {:mvn/version "1.10.2-alpha1"} 5 | org.clojure/tools.logging {:mvn/version "1.0.0"} 6 | ch.qos.logback/logback-core {:mvn/version "1.2.3"} 7 | ch.qos.logback/logback-classic {:mvn/version "1.2.3"} 8 | io.quarkus/quarkus-kafka-client {:mvn/version "1.3.1.Final" 9 | :exclusions [org.jboss.slf4j/slf4j-jboss-logging 10 | org.jboss.logging/jboss-logging]}} 11 | :aliases 12 | {:native-image 13 | {:main-opts ["-m clj.native-image core" 14 | "--enable-https" 15 | "--no-fallback" 16 | "--allow-incomplete-classpath" 17 | "--initialize-at-build-time" 18 | "--enable-all-security-services" 19 | "-H:ReflectionConfigurationFiles=graalvm/reflect-config.json" 20 | "-H:+ReportExceptionStackTraces" 21 | "--initialize-at-run-time=com.fasterxml.jackson.dataformat.cbor.CBORFactory" 22 | ;; optional native image name override 23 | "-H:Name=clj-kafka"] 24 | :jvm-opts ["-Dclojure.compiler.direct-linking=true"] 25 | :extra-deps {clj.native-image 26 | {:git/url "https://github.com/taylorwood/clj.native-image.git" 27 | :exclusions [commons-logging/commons-logging 28 | org.slf4j/slf4j-nop] 29 | :sha "7708e7fd4572459c81f6a6b8e44c96f41cdd92d4"}}}}} 30 | -------------------------------------------------------------------------------- /dockerfiles/Dockerfile.clj-kafka-builder: -------------------------------------------------------------------------------- 1 | FROM oracle/graalvm-ce:20.0.0-java11 as BUILDER 2 | 3 | ENV GRAALVM_HOME=$JAVA_HOME 4 | 5 | RUN gu install native-image \ 6 | && curl -O https://download.clojure.org/install/linux-install-1.10.1.510.sh \ 7 | && chmod +x linux-install-1.10.1.510.sh \ 8 | && ./linux-install-1.10.1.510.sh \ 9 | && rm linux-install-1.10.1.510.sh 10 | 11 | RUN mkdir -p /usr/src/app 12 | WORKDIR /usr/src/app 13 | 14 | COPY deps.edn /usr/src/app/ 15 | RUN clojure -R:native-image 16 | COPY resources/ /usr/src/app/resources 17 | COPY graalvm/ /usr/src/app/graalvm/ 18 | COPY src/ /usr/src/app/src 19 | 20 | RUN clojure -A:native-image 21 | -------------------------------------------------------------------------------- /dockerfiles/docker-compose.dev.yml: -------------------------------------------------------------------------------- 1 | version: '3' 2 | 3 | services: 4 | zookeeper: 5 | image: wurstmeister/zookeeper:3.4.6 6 | ports: ["2181:2181"] 7 | 8 | broker: 9 | image: wurstmeister/kafka:2.11-1.1.1 10 | depends_on: 11 | - zookeeper 12 | ports: ["9092:9092"] 13 | environment: 14 | KAFKA_ADVERTISED_HOST_NAME: localhost 15 | KAFKA_LISTENERS: PLAINTEXT://broker:29092,PLAINTEXT_HOST://:9092 16 | KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://broker:29092,PLAINTEXT_HOST://localhost:9092 17 | KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT 18 | KAFKA_CREATE_TOPICS: "test:1:1" 19 | KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181 20 | volumes: 21 | - /var/run/docker.sock:/var/run/docker.sock 22 | -------------------------------------------------------------------------------- /graalvm/jni-config.json: -------------------------------------------------------------------------------- 1 | [ 2 | ] 3 | -------------------------------------------------------------------------------- /graalvm/proxy-config.json: -------------------------------------------------------------------------------- 1 | [ 2 | ] 3 | -------------------------------------------------------------------------------- /graalvm/reflect-config.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "name":"org.apache.kafka.clients.consumer.ConsumerConfig", 4 | "allPublicFields":true 5 | }, 6 | { 7 | "name":"org.apache.kafka.clients.consumer.KafkaConsumer", 8 | "allPublicMethods":true, 9 | "allPublicConstructors":true 10 | }, 11 | { 12 | "name":"org.apache.kafka.clients.consumer.RangeAssignor", 13 | "methods":[{"name":"","parameterTypes":[] }] 14 | }, 15 | { 16 | "name":"org.apache.kafka.clients.producer.KafkaProducer", 17 | "allPublicMethods":true, 18 | "allPublicConstructors":true 19 | }, 20 | { 21 | "name":"org.apache.kafka.clients.producer.ProducerConfig", 22 | "allPublicFields":true 23 | }, 24 | { 25 | "name":"org.apache.kafka.clients.producer.ProducerRecord", 26 | "allPublicConstructors":true 27 | }, 28 | { 29 | "name":"org.apache.kafka.clients.producer.internals.DefaultPartitioner", 30 | "methods":[{"name":"","parameterTypes":[] }] 31 | }, 32 | { 33 | "name":"org.apache.kafka.common.serialization.LongDeserializer", 34 | "methods":[{"name":"","parameterTypes":[] }] 35 | }, 36 | { 37 | "name":"org.apache.kafka.common.serialization.LongSerializer", 38 | "methods":[{"name":"","parameterTypes":[] }] 39 | }, 40 | { 41 | "name":"org.apache.kafka.common.serialization.StringDeserializer", 42 | "methods":[{"name":"","parameterTypes":[] }] 43 | }, 44 | { 45 | "name":"org.apache.kafka.common.serialization.StringSerializer", 46 | "methods":[{"name":"","parameterTypes":[] }] 47 | } 48 | ] 49 | -------------------------------------------------------------------------------- /graalvm/resource-config.json: -------------------------------------------------------------------------------- 1 | { 2 | "resources":[ 3 | {"pattern":"\\Qclojure/core.clj\\E"}, 4 | {"pattern":"\\Qclojure/core/async.clj\\E"}, 5 | {"pattern":"\\Qclojure/core/async/impl/buffers.clj\\E"}, 6 | {"pattern":"\\Qclojure/core/async/impl/channels.clj\\E"}, 7 | {"pattern":"\\Qclojure/core/async/impl/concurrent.clj\\E"}, 8 | {"pattern":"\\Qclojure/core/async/impl/dispatch.clj\\E"}, 9 | {"pattern":"\\Qclojure/core/async/impl/exec/threadpool.clj\\E"}, 10 | {"pattern":"\\Qclojure/core/async/impl/ioc_macros.clj\\E"}, 11 | {"pattern":"\\Qclojure/core/async/impl/mutex.clj\\E"}, 12 | {"pattern":"\\Qclojure/core/async/impl/protocols.clj\\E"}, 13 | {"pattern":"\\Qclojure/core/async/impl/timers.clj\\E"}, 14 | {"pattern":"\\Qclojure/core/cache.clj\\E"}, 15 | {"pattern":"\\Qclojure/core/memoize.clj\\E"}, 16 | {"pattern":"\\Qclojure/core/server.clj\\E"}, 17 | {"pattern":"\\Qclojure/core/server__init.class\\E"}, 18 | {"pattern":"\\Qclojure/core/specs/alpha.clj\\E"}, 19 | {"pattern":"\\Qclojure/core__init.class\\E"}, 20 | {"pattern":"\\Qclojure/data/priority_map.clj\\E"}, 21 | {"pattern":"\\Qclojure/pprint.clj\\E"}, 22 | {"pattern":"\\Qclojure/pprint__init.class\\E"}, 23 | {"pattern":"\\Qclojure/reflect.clj\\E"}, 24 | {"pattern":"\\Qclojure/reflect__init.class\\E"}, 25 | {"pattern":"\\Qclojure/spec/alpha.clj\\E"}, 26 | {"pattern":"\\Qclojure/spec/alpha__init.class\\E"}, 27 | {"pattern":"\\Qclojure/tools/analyzer.clj\\E"}, 28 | {"pattern":"\\Qclojure/tools/analyzer/ast.clj\\E"}, 29 | {"pattern":"\\Qclojure/tools/analyzer/env.clj\\E"}, 30 | {"pattern":"\\Qclojure/tools/analyzer/jvm.clj\\E"}, 31 | {"pattern":"\\Qclojure/tools/analyzer/jvm/utils.clj\\E"}, 32 | {"pattern":"\\Qclojure/tools/analyzer/passes.clj\\E"}, 33 | {"pattern":"\\Qclojure/tools/analyzer/passes/add_binding_atom.clj\\E"}, 34 | {"pattern":"\\Qclojure/tools/analyzer/passes/cleanup.clj\\E"}, 35 | {"pattern":"\\Qclojure/tools/analyzer/passes/constant_lifter.clj\\E"}, 36 | {"pattern":"\\Qclojure/tools/analyzer/passes/elide_meta.clj\\E"}, 37 | {"pattern":"\\Qclojure/tools/analyzer/passes/emit_form.clj\\E"}, 38 | {"pattern":"\\Qclojure/tools/analyzer/passes/jvm/analyze_host_expr.clj\\E"}, 39 | {"pattern":"\\Qclojure/tools/analyzer/passes/jvm/annotate_host_info.clj\\E"}, 40 | {"pattern":"\\Qclojure/tools/analyzer/passes/jvm/annotate_loops.clj\\E"}, 41 | {"pattern":"\\Qclojure/tools/analyzer/passes/jvm/annotate_tag.clj\\E"}, 42 | {"pattern":"\\Qclojure/tools/analyzer/passes/jvm/box.clj\\E"}, 43 | {"pattern":"\\Qclojure/tools/analyzer/passes/jvm/classify_invoke.clj\\E"}, 44 | {"pattern":"\\Qclojure/tools/analyzer/passes/jvm/constant_lifter.clj\\E"}, 45 | {"pattern":"\\Qclojure/tools/analyzer/passes/jvm/emit_form.clj\\E"}, 46 | {"pattern":"\\Qclojure/tools/analyzer/passes/jvm/fix_case_test.clj\\E"}, 47 | {"pattern":"\\Qclojure/tools/analyzer/passes/jvm/infer_tag.clj\\E"}, 48 | {"pattern":"\\Qclojure/tools/analyzer/passes/jvm/validate.clj\\E"}, 49 | {"pattern":"\\Qclojure/tools/analyzer/passes/jvm/validate_loop_locals.clj\\E"}, 50 | {"pattern":"\\Qclojure/tools/analyzer/passes/jvm/validate_recur.clj\\E"}, 51 | {"pattern":"\\Qclojure/tools/analyzer/passes/jvm/warn_on_reflection.clj\\E"}, 52 | {"pattern":"\\Qclojure/tools/analyzer/passes/source_info.clj\\E"}, 53 | {"pattern":"\\Qclojure/tools/analyzer/passes/trim.clj\\E"}, 54 | {"pattern":"\\Qclojure/tools/analyzer/passes/uniquify.clj\\E"}, 55 | {"pattern":"\\Qclojure/tools/analyzer/passes/warn_earmuff.clj\\E"}, 56 | {"pattern":"\\Qclojure/tools/analyzer/utils.clj\\E"}, 57 | {"pattern":"\\Qclojure/tools/cli.cljc\\E"}, 58 | {"pattern":"\\Qclojure/tools/logging.clj\\E"}, 59 | {"pattern":"\\Qclojure/tools/logging/impl.clj\\E"}, 60 | {"pattern":"\\Qclojure/tools/reader.clj\\E"}, 61 | {"pattern":"\\Qclojure/tools/reader/default_data_readers.clj\\E"}, 62 | {"pattern":"\\Qclojure/tools/reader/impl/commons.clj\\E"}, 63 | {"pattern":"\\Qclojure/tools/reader/impl/errors.clj\\E"}, 64 | {"pattern":"\\Qclojure/tools/reader/impl/inspect.clj\\E"}, 65 | {"pattern":"\\Qclojure/tools/reader/impl/utils.clj\\E"}, 66 | {"pattern":"\\Qclojure/tools/reader/reader_types.clj\\E"}, 67 | {"pattern":"\\Qcore.clj\\E"}, 68 | {"pattern":"\\Qcore/async.clj\\E"}, 69 | {"pattern":"\\Qcore/cartesian.clj\\E"}, 70 | {"pattern":"\\Qcore/http.clj\\E"}, 71 | {"pattern":"\\Qcore/ilm.clj\\E"}, 72 | {"pattern":"\\Qcore/index.clj\\E"}, 73 | {"pattern":"\\Qcore/json.clj\\E"}, 74 | {"pattern":"\\Qcore/records.clj\\E"}, 75 | {"pattern":"\\Qjsonista/core.clj\\E"}, 76 | {"pattern":"\\Qkafka/kafka-version.properties\\E"}, 77 | {"pattern":"\\Qorg/httpkit/client.clj\\E"}, 78 | {"pattern":"\\Qorg/httpkit/encode.clj\\E"}, 79 | {"pattern":"\\Qorg/slf4j/impl/StaticLoggerBinder.class\\E"}, 80 | {"pattern":"\\Qprofile_slow_queries.clj\\E"}, 81 | {"pattern":"\\Qreindex.clj\\E"}, 82 | {"pattern":"\\Qreplay.clj\\E"}, 83 | {"pattern":"\\Qscroll.clj\\E"} 84 | ], 85 | "bundles":[] 86 | } 87 | -------------------------------------------------------------------------------- /resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | ${LOGGER_TARGET:-System.out} 5 | 6 | %date{ISO8601} %-5p %c{1} - %m%n 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /src/core.clj: -------------------------------------------------------------------------------- 1 | (ns core 2 | (:gen-class) 3 | (:require 4 | [clojure.tools.logging :as log]) 5 | (:import (org.apache.kafka.clients.consumer ConsumerConfig KafkaConsumer) 6 | (org.apache.kafka.clients.producer ProducerConfig KafkaProducer ProducerRecord RecordMetadata) 7 | (org.apache.kafka.common.serialization StringSerializer StringDeserializer) 8 | (java.time Duration) 9 | (java.util Properties))) 10 | 11 | (defn brokers [] 12 | (or (System/getenv "BOOTSTRAP_SERVERS_CONFIG") "127.0.0.1:9092")) 13 | 14 | (defn topic [] 15 | (or (System/getenv "TOPIC") "test")) 16 | 17 | (defn kafka-producer [{:keys [bootstrap-servers-config]}] 18 | (KafkaProducer. 19 | (doto (Properties.) 20 | (.put ProducerConfig/BOOTSTRAP_SERVERS_CONFIG bootstrap-servers-config) 21 | (.put ProducerConfig/CLIENT_ID_CONFIG "KafkaExampleProducer") 22 | (.put ProducerConfig/KEY_SERIALIZER_CLASS_CONFIG (.getName StringSerializer)) 23 | (.put ProducerConfig/VALUE_SERIALIZER_CLASS_CONFIG (.getName StringSerializer))))) 24 | 25 | (defn kafka-consumer [{:keys [bootstrap-servers-config]}] 26 | (KafkaConsumer. 27 | (doto (Properties.) 28 | (.put ConsumerConfig/BOOTSTRAP_SERVERS_CONFIG bootstrap-servers-config) 29 | (.put ConsumerConfig/GROUP_ID_CONFIG "my-group-id") 30 | (.put ConsumerConfig/KEY_DESERIALIZER_CLASS_CONFIG (.getName StringDeserializer)) 31 | (.put ConsumerConfig/VALUE_DESERIALIZER_CLASS_CONFIG (.getName StringDeserializer)) 32 | (.put ConsumerConfig/MAX_POLL_RECORDS_CONFIG (Integer/parseInt "1")) 33 | (.put ConsumerConfig/ENABLE_AUTO_COMMIT_CONFIG "false") 34 | (.put ConsumerConfig/AUTO_OFFSET_RESET_CONFIG "latest")))) 35 | 36 | (defn start-producer [opts] 37 | (let [^KafkaProducer producer (kafka-producer opts) 38 | topic (:topic opts) 39 | thread (Thread. 40 | ^Runnable 41 | (fn [] 42 | (loop [counter 0] 43 | (let [producer-record (ProducerRecord. topic (str counter) (str "test-" counter)) 44 | ^RecordMetadata record-metadata (.get (.send producer producer-record))] 45 | (log/infof "Produced record with offset: %s" (.offset record-metadata))) 46 | (Thread/sleep 1000) 47 | (recur (inc counter)))))] 48 | (.start thread) 49 | thread)) 50 | 51 | (defn start-consumer [opts] 52 | (let [^KafkaConsumer consumer (kafka-consumer opts) 53 | duration (Duration/ofSeconds 2) 54 | ^String topic (:topic opts)] 55 | (.subscribe consumer (re-pattern topic)) 56 | (loop [count 0] 57 | (doseq [r (.poll consumer duration)] 58 | (log/infof "Iteration=%s record='%s'" count r)) 59 | (.commitAsync consumer) 60 | (recur (inc count))))) 61 | 62 | (defn -main [& args] 63 | (let [opts {:bootstrap-servers-config (brokers) 64 | :topic (topic)}] 65 | (log/infof "Starting Clojure Kafka native-image demo with: %s" opts) 66 | (start-producer opts) 67 | (start-consumer opts))) 68 | --------------------------------------------------------------------------------