excercise 2 done

This commit is contained in:
2025-12-12 22:48:54 +01:00
parent a60e3de86a
commit 27259a12e0
123 changed files with 2946 additions and 1 deletions

39
week2_TinsaeGhilay/Task5/.gitignore vendored Normal file
View File

@@ -0,0 +1,39 @@
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
.kotlin
### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store

8
week2_TinsaeGhilay/Task5/.idea/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View File

@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>

14
week2_TinsaeGhilay/Task5/.idea/misc.xml generated Normal file
View File

@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_25" default="true" project-jdk-name="25" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

6
week2_TinsaeGhilay/Task5/.idea/vcs.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/../.." vcs="Git" />
</component>
</project>

View File

@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.distributed</groupId>
<artifactId>Task5</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>25</maven.compiler.source>
<maven.compiler.target>25</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>3.9.1</version>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,48 @@
package org.distributed;
import org.apache.kafka.clients.consumer.*;
import java.time.Duration;
import java.util.Collections;
import java.util.Properties;
public class Consumer {
public static void main(String[] args) {
// Kafka configuration properties
Properties props = new Properties();
props.put(Tag.SERVER.label, Tag.SERVER_URL.label);
props.put(Tag.DESERIALISER_KEY.label, Tag.DESERIALISER.label);
props.put(Tag.DESERIALISER_VAL.label, Tag.DESERIALISER.label);
props.put(Tag.GID.label, Tag.GROUP.label);
// Creating KafkaConsumer instance
KafkaConsumer<Object, Object> consumer = new KafkaConsumer<>(props);
// subscribe to topic
consumer.subscribe(Collections.singletonList(Tag.TOPIC.label));
// Starting consuming the messages
while (true) {
try (consumer) { // it gave me no other way to close consumer
// Providing poll for new records from Kafka topic
ConsumerRecords<Object, Object> records = consumer.poll(Duration.ofMillis(100));
// Processing the received records
for (ConsumerRecord<Object, Object> record : records) {
// Printing the received message
System.out.println("Nerds Received a message: " + record.value());
}
} catch (Exception e) {
System.err.println("Consumer: " + e.getMessage());
}
}
}
}

View File

@@ -0,0 +1,28 @@
package org.distributed;
import java.util.Properties;
import org.apache.kafka.clients.producer.*;
public class Producer {
public static void main(String[] args) {
// Kafka configuration properties
Properties props = new Properties();
props.put(Tag.SERVER.label, Tag.SERVER_URL.label);
props.put(Tag.SERIALISER_KEY.label, Tag.SERIALISER.label);
props.put(Tag.SERIALISER_VAL.label, Tag.SERIALISER.label);
// Creating KafkaProducer instance
KafkaProducer<Object, Object> producer = new KafkaProducer<>(props);
// Topic and message that we want to send
String message = "Hello, Nerds Welcome to Kafka!";
// Sending our message to Kafka topic
producer.send(new ProducerRecord<>(Tag.TOPIC.label, message));
// Closing the producer
producer.close();
}
}

View File

@@ -0,0 +1,27 @@
package org.distributed;
public enum Tag {
// Server
SERVER("bootstrap.servers"),
SERVER_URL("localhost:9092"),
// deserialiser
DESERIALISER_KEY("key.deserializer"),
DESERIALISER_VAL("value.deserializer"),
DESERIALISER("org.apache.kafka.common.serialization.StringDeserializer"),
// serialiser
SERIALISER_KEY("key.serializer"),
SERIALISER_VAL("value.serializer"),
SERIALISER("org.apache.kafka.common.serialization.StringSerializer"),
// ids
GID("group.id"),
GROUP("consumer_GROUP"),
// Topic
TOPIC("nerds");
public final String label;
private Tag(String label) {
this.label = label;
}
}