├── README.md
├── build.gradle.kts
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── settings.gradle.kts
└── src
├── main
└── java
│ ├── KafkaMessageConsumer.java
│ ├── KafkaMessageProducer.java
│ └── Main.java
└── test
└── java
└── KafkaMessageIntegrationTest.java
/README.md:
--------------------------------------------------------------------------------
1 |
2 | # Streaming Application Using Kafka with Gradle Build Tool
3 |
4 | This is a simple Proof of Concept (POC) that demonstrates a Kafka producer and consumer application with unit and integration tests. This project is a Java application using Apache Kafka with the Gradle build tool.
5 |
6 | ## Table of Contents
7 | - [Requirements](#requirements)
8 | - [Setup](#setup)
9 | - [Running the Application](#running-the-application)
10 | - [Testing](#testing)
11 | - [Contributing](#contributing)
12 | - [License](#license)
13 |
14 | ## Requirements
15 | - [Kafka](https://kafka.apache.org/quickstart)
16 | - [Java 8+](https://www.oracle.com/java/technologies/javase-downloads.html)
17 | - [Gradle](https://gradle.org/install/)
18 |
19 | ## Setup
20 | 1. **Install Kafka**: Follow the [Kafka quickstart](https://kafka.apache.org/quickstart) guide to set up Kafka on your machine.
21 | 2. **Create Kafka Topic**: Create a Kafka topic called `demo_java` with 3 partitions:
22 | ```sh
23 | kafka-topics.sh --bootstrap-server localhost:9092 --topic demo_java --create --partitions 3 --replication-factor 1
24 |
25 | Running the Application
26 | To observe the output of the Java producer application, open the Kafka consumer CLI using the command:
27 |
28 | kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic demo_java
29 |
30 | To run the application:
31 |
32 | Testing
33 | Run the unit and integration tests using Gradle:
34 |
35 | ./gradlew clean test
36 |
37 |
38 |
39 |
40 |
41 |
42 | Build time was 6sec
43 |
44 |
45 | Contributing
46 | We welcome contributions to the Kafka Java Gradle POC project. Here are the steps to get started:
47 | 1. Fork the Repository:
48 | * Navigate to the repository kafka_Java_Gradle_POC and click on the "Fork" button in the top-right corner to create a copy of the repository in your GitHub account.
49 | 2. Clone the Forked Repository:shgit clone https://github.com/your-username/kafka_Java_Gradle_POC.git
50 | 3. cd kafka_Java_Gradle_POCgit checkout -b my-feature
51 | 4. Make Your Changes:
52 | * Implement your changes or additions to the repository code. Ensure your code follows the existing code style and includes necessary documentation.
53 | 5. Commit and Push Your Changes:git add .
54 | 6. git commit -m "Description of the changes"
55 | 7. git push origin my-feature
56 | 8. Create a Pull Request:
57 | * Go to your forked repository on GitHub, and you should see a "Compare & pull request" button. Click on it to create a pull request to the original repository.
58 | 9. Address Review Comments:
59 | * Engage with the maintainers, address any review comments, and make necessary changes until your pull request is approved and merged.
60 |
61 |
62 |
63 |
64 |
65 |
--------------------------------------------------------------------------------
/build.gradle.kts:
--------------------------------------------------------------------------------
1 | //import java.util.regex.Pattern.compile
2 |
3 | //import com.sun.imageio.plugins.jpeg.JPEG.version
4 |
5 | plugins {
6 | id("java")
7 | kotlin("jvm")
8 | }
9 |
10 | group = "kafka-demos-java"
11 | version = "1.0-SNAPSHOT"
12 |
13 | repositories {
14 | mavenCentral()
15 | }
16 |
17 | dependencies {
18 | testImplementation(platform("org.junit:junit-bom:5.9.1"))
19 | // testImplementation("org:junit.Assert.assertEquals")
20 | testImplementation("org.junit.jupiter:junit-jupiter")
21 | implementation(kotlin("stdlib-jdk8"))
22 | testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:1.7.0")
23 | implementation("org.apache.kafka:kafka-clients:3.6.1")
24 | implementation("org.slf4j:slf4j-api:1.7.36")
25 |
26 | }
27 |
28 | tasks.test {
29 | useJUnitPlatform()
30 | }
31 | kotlin {
32 | jvmToolchain(21)
33 | }
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Mamiololo01/kafka_Java_Gradle_POC/4271a4868e4c2cf220396ecfcacf6809d5d419fa/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Wed Dec 20 11:41:35 GMT 2023
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
5 | zipStoreBase=GRADLE_USER_HOME
6 | zipStorePath=wrapper/dists
7 |
--------------------------------------------------------------------------------
/settings.gradle.kts:
--------------------------------------------------------------------------------
1 | pluginManagement {
2 | plugins {
3 | kotlin("jvm") version "2.0.0-Beta2"
4 | }
5 | }
6 | plugins {
7 | id("org.gradle.toolchains.foojay-resolver-convention") version "0.5.0"
8 | }
9 | rootProject.name = "kafka_Java_POC_Gradle"
10 |
11 |
--------------------------------------------------------------------------------
/src/main/java/KafkaMessageConsumer.java:
--------------------------------------------------------------------------------
1 | import org.apache.kafka.clients.consumer.ConsumerConfig;
2 | import org.apache.kafka.clients.consumer.ConsumerRecords;
3 | import org.apache.kafka.clients.consumer.KafkaConsumer;
4 | import org.apache.kafka.common.serialization.StringDeserializer;
5 | import java.time.Duration;
6 | import java.util.Collections;
7 | import java.util.Properties;
8 |
9 | public class KafkaMessageConsumer {
10 | private static final String TOPIC_NAME = "test-topic";
11 | private static final String BOOTSTRAP_SERVERS = "localhost:29092";
12 | private static final String GROUP_ID = "test-group";
13 | // private final Consumer consumer;
14 | private int counter = 0;
15 | private final KafkaConsumer consumer;
16 |
17 | public static void main(String[] args) {
18 | // Set up Kafka consumer properties
19 | KafkaMessageConsumer consumer = new KafkaMessageConsumer();
20 | consumer.subscribeToMessage(TOPIC_NAME);
21 | }
22 | public KafkaMessageConsumer() {
23 | Properties consumerProps = new Properties();
24 | consumerProps.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,
25 | BOOTSTRAP_SERVERS);
26 | consumerProps.put(ConsumerConfig.GROUP_ID_CONFIG, GROUP_ID);
27 | consumerProps.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG,
28 | StringDeserializer.class.getName());
29 | consumerProps.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,
30 | StringDeserializer.class.getName());
31 | consumerProps.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
32 | // Create Kafka consumer
33 | consumer = new KafkaConsumer<>(consumerProps);
34 | }
35 | public void subscribeToMessage(String topic) {
36 | // Subscribe to the Kafka topic
37 | consumer.subscribe(Collections.singletonList(topic));
38 | // Poll for new messages
39 | while (counter==0) {
40 | ConsumerRecords records =
41 | consumer.poll(Duration.ofMillis(1000));
42 | records.forEach(record -> {
43 | // Process the received message
44 | counter++;
45 | System.out.println("Received message: " + record.value());
46 | });
47 | }
48 | }
49 | public int getCounter(){
50 | return counter;
51 | }
52 | }
--------------------------------------------------------------------------------
/src/main/java/KafkaMessageProducer.java:
--------------------------------------------------------------------------------
1 | import org.apache.kafka.clients.producer.KafkaProducer;
2 | import org.apache.kafka.clients.producer.Producer;
3 | import org.apache.kafka.clients.producer.ProducerConfig;
4 | import org.apache.kafka.clients.producer.ProducerRecord;
5 | import org.apache.kafka.common.serialization.StringSerializer;
6 | import java.util.Properties;
7 |
8 |
9 | public class KafkaMessageProducer {
10 | private static final String TOPIC_NAME = "test-topic";
11 | private static final String BOOTSTRAP_SERVERS = "localhost:29092";
12 | private final Producer producer;
13 | public void sendMessageToKafka(String message) {
14 | ProducerRecord record = new
15 | ProducerRecord<>(TOPIC_NAME, message);
16 | producer.send(record);
17 | }
18 | public KafkaMessageProducer() {
19 | Properties producerProps = new Properties();
20 | producerProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,
21 | BOOTSTRAP_SERVERS);
22 | producerProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,
23 | StringSerializer.class.getName());
24 | producerProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,
25 | StringSerializer.class.getName());
26 |
27 | // Create Kafka producer
28 | producer = new
29 | KafkaProducer<>(producerProps);
30 | }
31 | public static void main(String[] args) {
32 | // Set up Kafka producer properties
33 | KafkaMessageProducer producer = new KafkaMessageProducer();
34 | producer.sendMessageToKafka("Hello Kafka");
35 | }
36 | }
--------------------------------------------------------------------------------
/src/main/java/Main.java:
--------------------------------------------------------------------------------
1 | package kafka_demos_java;
2 |
3 | import java.util.Properties;
4 |
5 | public class Main {
6 | public static void main(String[] args) {
7 | //TIP Press with your caret at the highlighted text
8 | // to see how IntelliJ IDEA suggests fixing it.
9 | System.out.printf("Hello and welcome!");
10 |
11 | for (int i = 1; i <= 5; i++) {
12 | //TIP Press to start debugging your code. We have set one breakpoint
13 | // for you, but you can always add more by pressing .
14 | System.out.println("i = " + i);
15 | }
16 | }
17 | }
18 |
19 |
--------------------------------------------------------------------------------
/src/test/java/KafkaMessageIntegrationTest.java:
--------------------------------------------------------------------------------
1 | //import kafka_demos_java.Main;
2 | //import static.org.junit.Test;
3 | //import static org.junit.Assert.assertEquals;
4 | import org.junit.jupiter.api.Test;
5 |
6 | import static org.junit.jupiter.api.Assertions.assertEquals;
7 |
8 | public class KafkaMessageIntegrationTest {
9 |
10 |
11 | @Test
12 | public void testSendMessageAndConsumeFromKafka() {
13 |
14 | KafkaMessageConsumer consumer = new KafkaMessageConsumer();
15 |
16 |
17 | int initialCounter = consumer.getCounter();
18 |
19 | // Given
20 | KafkaMessageProducer producer = new KafkaMessageProducer();
21 |
22 | // When
23 | String message = "Hello, Kafka!";
24 | producer.sendMessageToKafka(message);
25 |
26 | // When
27 | String topic = "test-topic";
28 | consumer.subscribeToMessage(topic);
29 |
30 | // Then
31 | int finalCounter = consumer.getCounter();
32 | assertEquals(initialCounter + 1, finalCounter);
33 | }
34 | }
--------------------------------------------------------------------------------