{"id":19405655,"url":"https://github.com/johnament/kafka-cdi","last_synced_at":"2025-08-01T08:34:02.695Z","repository":{"id":68090585,"uuid":"104817058","full_name":"johnament/kafka-cdi","owner":"johnament","description":"A simple CDI extension for Apache Kafka","archived":false,"fork":false,"pushed_at":"2017-08-23T20:01:33.000Z","size":112,"stargazers_count":1,"open_issues_count":0,"forks_count":25,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-04-03T01:51:12.981Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/johnament.png","metadata":{"files":{"readme":"README.adoc","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-09-26T00:46:50.000Z","updated_at":"2023-10-11T03:42:32.000Z","dependencies_parsed_at":null,"dependency_job_id":"e14c9035-b39c-475c-8840-99d9a7944474","html_url":"https://github.com/johnament/kafka-cdi","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/johnament%2Fkafka-cdi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/johnament%2Fkafka-cdi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/johnament%2Fkafka-cdi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/johnament%2Fkafka-cdi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/johnament","download_url":"https://codeload.github.com/johnament/kafka-cdi/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250600585,"owners_count":21456988,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-10T11:39:00.195Z","updated_at":"2025-04-24T09:30:58.318Z","avatar_url":"https://github.com/johnament.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"= Kafka-CDI - A simple CDI extension for Apache Kafka\n\nimage:https://img.shields.io/travis/matzew/kafka-cdi/master.svg[Build Status (Travis CI), link=https://travis-ci.org/matzew/kafka-cdi]\nimage:https://img.shields.io/:license-Apache2-blue.svg[License, link=http://www.apache.org/licenses/LICENSE-2.0]\nimage:https://img.shields.io/maven-central/v/net.wessendorf.kafka/kafka-cdi-extension.svg[Maven Central, link=http://search.maven.org/#search%7Cga%7C1%7Ckafka-cdi-extension]\nimage:http://www.javadoc.io/badge/net.wessendorf.kafka/kafka-cdi-extension.svg[Javadocs, link=http://www.javadoc.io/doc/net.wessendorf.kafka/kafka-cdi-extension]\n\nMaking it easy to use Apache Kafka's Java client API in a CDI managed environment!\n\n== Getting started\n\nThis section gives an overview on how to use the CafDI library!\n\n=== Maven config\n\nIn a Maven managed project simply the following to your `pom.xml`:\n\n[source,xml]\n----\n...\n  \u003cdependency\u003e\n    \u003cgroupId\u003enet.wessendorf.kafka\u003c/groupId\u003e\n    \u003cartifactId\u003ekafka-cdi-extension\u003c/artifactId\u003e\n    \u003cversion\u003e0.0.9\u003c/version\u003e\n  \u003c/dependency\u003e\n...\n----\n\n=== Injecting a Kafka Producer\n\nThe `@Producer` annotation is used to configure and inject an instance of the `SimpleKafkaProducer` class, which is a simple extension of the original `KafkaProducer` class:\n\n[source,java]\n----\n...\npublic class MyPublisherService {\n\n  private Logger logger = LoggerFactory.getLogger(MyPublisherService.class);\n\n  @Producer\n  SimpleKafkaProducer\u003cInteger, String\u003e producer;\n\n  /**\n   * A simple service method, that sends payload over the wire\n   */\n  public void hello() {\n    producer.send(\"myTopic\", \"My Message\");\n  }\n}\n----\n\n=== Annotating a bean method to declare it as a Kafka Consumer\n\nThe `@Consumer` annotation is used to configure and declare an annotated method as a _callback_ for the internal `DelegationKafkaConsumer`, which internally uses the vanilla `KafkaConsumer`:\n\n[source,java]\n----\npublic class MyListenerService {\n\n  private Logger logger = LoggerFactory.getLogger(MyListenerService.class);\n  \n  /**\n   * Simple listener that receives messages from the Kafka broker\n    */\n  @Consumer(topic = \"myTopic\", groupId = \"myGroupID\", keyType = Integer.class)\n  public void receiver(String message) {\n    logger.info(\"That's what I got: \" + message);\n  }\n]\n----\n\nReceiving the key and the value is also possible:\n\n[source,java]\n----\npublic class MyListenerService {\n\n  private Logger logger = LoggerFactory.getLogger(MyListenerService.class);\n\n  /**\n   * Simple listener that receives messages from the Kafka broker\n    */\n  @Consumer(topic = \"myTopic\", groupId = \"myGroupID\")\n  public void receiver(final String key, final String value) {\n    logger.info(\"That's what I got: (key: \" + key + \" , value:\" +  value + \")\");\n  }\n]\n----\n\n\n\n=== Global Configuration of the Kafka cluster\n\nA minimal of configuration is currently needed. For that there is a `@KafkaConfig` annotation. The first occurrence is used:\n\n[source,java]\n----\n@KafkaConfig(bootstrapServers = \"#{SOME_HOST}:#{SOME_PORT}\")\npublic class MyService {\n   ...\n}\n----\n\n=== JsonObject Serialization\n\nApache Kafka uses a binary message format, and comes with a handful of handy Serializers and Deserializers, available through the `Serdes` class. The CafDI extension adds a Serde for the `JsonObject`:\n\n==== JsonObject Serializer\n\nTo send serialize a JsonObject, simply specify the type, like:\n\n[source,java]\n----\n...\n@Producer\nSimpleKafkaProducer\u003cInteger, JsonObject\u003e producer;\n...\n\nproducer.send(\"myTopic\", myJsonObj);\n----\n\n==== JsonObject Deserializer\n\nFor deserialization the argument on the annotation `@Consumer` method is used to setup the actual `Deserializer`\n\n[source,java]\n----\n@Consumer(topic = \"myTopic\", groupId = \"myGroupID\", keyType = Integer.class)\npublic void receiveJsonObject(JsonObject message) {\n  logger.info(\"That's what I got: \" + message);\n}\n----\n\n== Running Apache Kafka \n\nTo setup Apache Kafka there are different ways to get started. This section quickly discusses pure Docker and Openshift.\n\n=== Running via Docker images\n\nStarting a Zookeeper cluster:\n\n[source,bash]\n----\ndocker run -d --name zookeeper jplock/zookeeper:3.4.6\n----\n\nNext, we need to start Kafka and link the Zookeeper Linux container to it:\n\n[source,bash]\n----\ndocker run -d --name kafka --link zookeeper:zookeeper ches/kafka\n----\n\nNow, that the broker is running, we need to figure out the IP address of it:\n\n[source,bash]\n----\ndocker inspect --format '{{ .NetworkSettings.IPAddress }}' kafka  \n----\n\nWe use this IP address when inside our `@KafkaConfig` annotation that our _Producers_ and _Consumers_ can speak to Apache Kafka.\n\n=== Running on Openshift \n\nFor Apache Kafka on Openshift please check this repository \n\nhttps://github.com/EnMasseProject/barnabas\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjohnament%2Fkafka-cdi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjohnament%2Fkafka-cdi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjohnament%2Fkafka-cdi/lists"}