Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

data processor messaging #11 #21

Merged
merged 2 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions core-back-end/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-webflux'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation group: 'org.keycloak', name: 'keycloak-spring-boot-adapter', version: '18.0.2'
implementation 'org.keycloak:keycloak-admin-client:23.0.3'
// implementation group: 'org.keycloak', name: 'keycloak-spring-boot-adapter', version: '23.0.3'
// implementation 'org.keycloak:keycloak-admin-client:23.0.3'
implementation 'org.springframework.kafka:spring-kafka'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'io.projectreactor:reactor-test'
Expand All @@ -31,3 +31,4 @@ dependencies {
tasks.named('test') {
useJUnitPlatform()
}

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

import jakarta.validation.Valid;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.serialization.ByteArraySerializer;
import org.apache.kafka.common.serialization.StringSerializer;
Expand Down
24 changes: 17 additions & 7 deletions data-processor/build.gradle
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
plugins {
id 'java'
id 'org.springframework.boot' version '3.2.0'
id 'io.spring.dependency-management' version '1.1.4'
}

group = 'org.student'
version = '1.0-SNAPSHOT'
group = "org.student"
version = "1.0-SNAPSHOT"

repositories {
mavenCentral()
java {
sourceCompatibility = '17'
}

configurations {
compileOnly {
extendsFrom annotationProcessor
}
}

dependencies {
testImplementation platform('org.junit:junit-bom:5.9.1')
testImplementation 'org.junit.jupiter:junit-jupiter'
implementation 'org.springframework.boot:spring-boot-starter:3.2.0'
implementation 'org.springframework.kafka:spring-kafka'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'io.projectreactor:reactor-test'
}

test {
tasks.named('test') {
useJUnitPlatform()
}
6 changes: 5 additions & 1 deletion data-processor/src/main/java/org/student/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package org.student;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
public static void main(String[] args) {
System.out.println("Hello world!");
SpringApplication.run(Application.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.student.messaging;

import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.serialization.ByteArraySerializer;
import org.apache.kafka.common.serialization.StringSerializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.core.ProducerFactory;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class KafkaConfig {

@Value("${spring.kafka.bootstrap-servers}")
private String bootstrapServers;

@Bean
public ProducerFactory<String, byte[]> producerFactory() {
Map<String, Object> configProp = new HashMap<>();
configProp.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
configProp.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
configProp.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, ByteArraySerializer.class);
return new DefaultKafkaProducerFactory<>(configProp);
}

@Bean
public KafkaTemplate<String, byte[]> kafkaTemplate() {
return new KafkaTemplate<>(producerFactory());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.student.messaging;

import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.annotation.TopicPartition;
import org.springframework.stereotype.Service;

@Service
public class MessageConsumer {
@KafkaListener(id = "data-processor-listener", topicPartitions = @TopicPartition(topic = "artifact-processor-topic", partitions = {"0"}), 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,18 @@
package org.student.messaging;

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

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

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

public void send(byte[] message) {
template.send(new ProducerRecord<>("artifact-processor-topic", 1, null, message));
}
}
11 changes: 11 additions & 0 deletions data-processor/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
spring:
kafka:
bootstrap-servers: localhost:9092
consumer:
group-id: core-file-garbage-group
auto-offset-reset: earlier
key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
value-deserializer: org.apache.kafka.common.serialization.ByteArrayDeserializer
producer:
key-serializer: org.apache.kafka.common.serialization.StringSerializer
value-serializer: org.apache.kafka.common.serialization.ByteArraySerializer
Loading