Skip to content

Commit

Permalink
core back-end messaging #10
Browse files Browse the repository at this point in the history
  • Loading branch information
Kasean committed Dec 29, 2023
1 parent 60a0f38 commit 81c78ec
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.serialization.ByteArrayDeserializer;
import org.apache.kafka.common.serialization.StringDeserializer;

import java.time.Duration;
import java.util.Arrays;
import java.util.Collections;
import java.util.Properties;

Expand All @@ -25,7 +27,8 @@ public MessageConsumer(String bootstrapServers, String groupId) {
}

public void consume(String topic) {
consumer.subscribe(Collections.singletonList(topic));
TopicPartition partition = new TopicPartition(topic, 0);
consumer.assign(Collections.singletonList(partition));

while (true) {
ConsumerRecords<String, byte[]> records = consumer.poll(Duration.ofMillis(100));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ public MessageProducer(String bootstrapServers) {
}

public void send(String topic, byte[] message) {
producer.send(new ProducerRecord<>(topic, message));
producer.send(new ProducerRecord<>(topic, 1, null, message));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.student.messaging;

public class KafkaTopics {
public static final String ARCHIVER_TOPIC = "archiver-topic";
public static final String DATA_PROCESSOR_TOPIC ="data-processor-topic";
public static final String ARTIFACT_PROCESSOR_TOPIC = "artifact-processor-topic";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.student.messaging.archiver;

import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.annotation.TopicPartition;
import org.springframework.stereotype.Service;
import org.student.messaging.KafkaTopics;

@Service
public class ArchiverListener {

@KafkaListener(id = "archiver-listener", topicPartitions = @TopicPartition(topic = KafkaTopics.ARCHIVER_TOPIC, partitions = {"1"}), groupId = "core-file-garbage-group")
public void consume(byte[] message) {
System.out.println("Consumed message: " + new String(message));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.student.messaging.archiver;

import org.apache.kafka.clients.producer.ProducerRecord;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import org.student.messaging.KafkaTopics;

@Service
public class ArchiverProducer {

private final KafkaTemplate<String, byte[]> template;

ArchiverProducer(KafkaTemplate<String, byte[]> template) {
this.template = template;
}

public void send(byte[] message) {
template.send(new ProducerRecord<>(KafkaTopics.ARCHIVER_TOPIC, 0, null, message));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.student.messaging.artifact_processor;

import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.annotation.TopicPartition;
import org.springframework.stereotype.Service;
import org.student.messaging.KafkaTopics;

@Service
public class ArtifactProcessorListener {
@KafkaListener(id = "artifact-processor-listener", topicPartitions = @TopicPartition(topic = KafkaTopics.ARTIFACT_PROCESSOR_TOPIC, partitions = {"1"}), groupId = "core-file-garbage-group")
public void consume(byte[] message) {
System.out.println("Consumed message: " + new String(message));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.student.messaging.artifact_processor;

import org.apache.kafka.clients.producer.ProducerRecord;
import org.springframework.kafka.core.KafkaTemplate;
import org.student.messaging.KafkaTopics;

public class ArtifactProcessorProducer {
private final KafkaTemplate<String, byte[]> template;

ArtifactProcessorProducer(KafkaTemplate<String, byte[]> template) {
this.template = template;
}

public void send(byte[] message) {
template.send(new ProducerRecord<>(KafkaTopics.ARTIFACT_PROCESSOR_TOPIC, 0, null, message));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.student.messaging.data_processor;

import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.annotation.TopicPartition;
import org.springframework.stereotype.Service;
import org.student.messaging.KafkaTopics;

@Service
public class DataProcessorListener {
@KafkaListener(id = "data-processor-listener", topicPartitions = @TopicPartition(topic = KafkaTopics.DATA_PROCESSOR_TOPIC, partitions = {"1"}), groupId = "core-file-garbage-group")
public void consume(byte[] message) {
System.out.println("Consumed message: " + new String(message));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.student.messaging.data_processor;

import org.apache.kafka.clients.producer.ProducerRecord;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import org.student.messaging.KafkaTopics;

@Service
public class DataProcessorProducer {
private final KafkaTemplate<String, byte[]> template;

DataProcessorProducer(KafkaTemplate<String, byte[]> template) {
this.template = template;
}

public void send(byte[] message) {
template.send(new ProducerRecord<>(KafkaTopics.DATA_PROCESSOR_TOPIC, 0, null, message));
}
}
2 changes: 1 addition & 1 deletion docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ services:
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
KAFKA_CREATE_TOPICS: "archiver-topic:1:1,data-processor-topic:1:1,artifact-processor-topic:1:1"
KAFKA_CREATE_TOPICS: "archiver-topic:2:1,data-processor-topic:2:1,artifact-processor-topic:2:1"
ports:
- "9092:9092"

Expand Down

0 comments on commit 81c78ec

Please sign in to comment.