#!/bin/bash
-IMAGE=juplo/spring-producer:2.0-SNAPSHOT
+IMAGE=juplo/spring-producer:2.0-json-SNAPSHOT
if [ "$1" = "cleanup" ]
then
}
group = 'de.juplo.kafka'
-version = '2.0-SNAPSHOT'
+version = '2.0-json-SNAPSHOT'
java {
toolchain {
}
dependencies {
- implementation 'org.apache.kafka:kafka-clients'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-web'
+ implementation 'org.springframework.kafka:spring-kafka'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
- testImplementation 'org.springframework.kafka:spring-kafka'
testImplementation 'org.springframework.kafka:spring-kafka-test'
testCompileOnly 'org.projectlombok:lombok'
testAnnotationProcessor 'org.projectlombok:lombok'
- kafka-3
producer:
- image: juplo/spring-producer:2.0-SNAPSHOT
+ image: juplo/spring-producer:2.0-json-SNAPSHOT
environment:
spring.kafka.bootstrap-servers: kafka:9092
spring.kafka.client-id: producer
<artifactId>spring-producer</artifactId>
<name>Spring Producer</name>
<description>A Simple Producer, based on Spring Boot, that sends messages via Kafka</description>
- <version>2.0-SNAPSHOT</version>
+ <version>2.0-json-SNAPSHOT</version>
<properties>
<java.version>21</java.version>
--- /dev/null
+package de.juplo.kafka;
+
+import lombok.Value;
+
+
+@Value
+public class AddNumberMessage implements SumupMessage
+{
+ private final int number;
+ private final int next;
+}
public ExampleProducer exampleProducer(
@Value("${spring.kafka.client-id}") String clientId,
ApplicationProperties properties,
- Producer<String, String> kafkaProducer,
+ Producer<String, SumupMessage> kafkaProducer,
ConfigurableApplicationContext applicationContext)
{
return
--- /dev/null
+package de.juplo.kafka;
+
+
+import lombok.Value;
+
+
+@Value
+public class CalculateSumMessage implements SumupMessage
+{
+ private final int number;
+}
private final String id;
private final String topic;
private final Duration throttle;
- private final Producer<String, String> producer;
+ private final Producer<String, SumupMessage> producer;
private final Thread workerThread;
private final Runnable closeCallback;
String id,
String topic,
Duration throttle,
- Producer<String, String> producer,
+ Producer<String, SumupMessage> producer,
Runnable closeCallback)
{
this.id = id;
@Override
public void run()
{
- long i = 0;
+ int i = 0;
try
{
for (; running; i++)
{
- send(Long.toString(i%10), Long.toString(i));
+ int number = i % 10;
+ SumupMessage message = (i % 7 == 0)
+ ? new CalculateSumMessage(number)
+ : new AddNumberMessage(number, i);
+
+ send(Long.toString(number), message);
if (throttle.isPositive())
{
}
}
- void send(String key, String value)
+ void send(String key, SumupMessage value)
{
final long time = System.currentTimeMillis();
- final ProducerRecord<String, String> record = new ProducerRecord<>(
+ final ProducerRecord<String, SumupMessage> record = new ProducerRecord<>(
topic, // Topic
key, // Key
value // Value
--- /dev/null
+package de.juplo.kafka;
+
+public interface SumupMessage
+{
+}
buffer-memory: 33554432
batch-size: 16384
compression-type: gzip
+ value-serializer: org.springframework.kafka.support.serializer.JsonSerializer
properties:
+ spring.json.type.mapping: >-
+ ADD:de.juplo.kafka.AddNumberMessage,
+ CALC:de.juplo.kafka.CalculateSumMessage
metadata.max.age.ms: 5000
request.timeout.ms: 5000
delivery.timeout.ms: 10000